ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Set DropDownList Selected value on GridView Row Button Click using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div class=&#34;container&#34;&gt; &lt;p style=&#34;font-size: x-large; font-weight: bolder&#34;&gt; Category&lt;/p&gt; &lt;asp:DropDownList ID=&#34;cat&#34; runat=&#34;server&#34; class=&#34;form-control input-lg&#34; Style=&#34;width: 100%; margin-bottom: 10px;&#34; AppendDataBoundItems=&#34;true&#34; AutoPostBack=&#34;true&#34;&gt; &lt;/asp:DropDownList&gt; &lt;p style=&#34;font-size: x-large; font-weight: bolder&#34;&gt; Sub-Category&lt;/p&gt; &lt;asp:DropDownList ID=&#34;DropDownList1&#34; runat=&#34;server&#34; class=&#34;form-control input-lg&#34; Style=&#34;width: 100%; margin-bottom: 10px;&#34; AutoPostBack=&#34;true&#34; AppendDataBoundItems=&#34;true&#34;&gt; &lt;/asp:DropDownList&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:GridView ID=&#34;gvDepartments&#34; runat=&#34;server&#34; EmptyDataText=&#34;No Records Found&#34; AutoGenerateColumns=&#34;False&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Category&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblcat&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Category&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Business Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblBname&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Bussiness_name&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Action&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Button ID=&#34;btnEdit&#34; runat=&#34;server&#34; Text=&#34;Edit&#34; OnClick=&#34;btnEdit_Click&#34; CssClass=&#34;btn btn-primary&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { cat.Items.Add(new ListItem(&#34;Select&#34;, &#34;0&#34;)); DropDownList1.Items.Add(new ListItem(&#34;Select&#34;, &#34;0&#34;)); DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Category&#34;, typeof(string)), new DataColumn(&#34;Bussiness_name&#34;, typeof(string)) }); dt.Rows.Add(&#34;Doctors&#34;, &#34;Dentists&#34;); dt.Rows.Add(&#34;Doctors&#34;, &#34;Cardiologists&#34;); dt.Rows.Add(&#34;Daily Needs&#34;, &#34;Grocery&#34;); dt.Rows.Add(&#34;Daily Needs&#34;, &#34;Chemists&#34;); cat.DataSource = dt; cat.DataTextField = &#34;Category&#34;; cat.DataValueField = &#34;Category&#34;; cat.DataBind(); DropDownList1.DataSource = dt; DropDownList1.DataTextField = &#34;Bussiness_name&#34;; DropDownList1.DataValueField = &#34;Bussiness_name&#34;; DropDownList1.DataBind(); gvDepartments.DataSource = dt; gvDepartments.DataBind(); } } protected void btnEdit_Click(object sender, EventArgs e) { Button btn = (sender) as Button; GridViewRow row = (GridViewRow)btn.NamingContainer; string category = (row.FindControl(&#34;lblcat&#34;) as Label).Text; string suCategory = (row.FindControl(&#34;lblBname&#34;) as Label).Text; if (cat.Items.FindByValue(category) != null) { cat.ClearSelection(); cat.Items.FindByValue(category).Selected = true; } if (DropDownList1.Items.FindByValue(suCategory) != null) { DropDownList1.ClearSelection(); DropDownList1.Items.FindByValue(suCategory).Selected = true; } }</pre> <p>&nbsp;</p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handle Me.Load If Not Me.IsPostBack Then cat.Items.Add(New ListItem(&#34;Select&#34;, &#34;0&#34;)) DropDownList1.Items.Add(New ListItem(&#34;Select&#34;, &#34;0&#34;)) Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Category&#34;, GetType(String)), New DataColumn(&#34;Bussiness_name&#34;, GetType(String))}) dt.Rows.Add(&#34;Doctors&#34;, &#34;Dentists&#34;) dt.Rows.Add(&#34;Doctors&#34;, &#34;Cardiologists&#34;) dt.Rows.Add(&#34;Daily Needs&#34;, &#34;Grocery&#34;) dt.Rows.Add(&#34;Daily Needs&#34;, &#34;Chemists&#34;) cat.DataSource = dt cat.DataTextField = &#34;Category&#34; cat.DataValueField = &#34;Category&#34; cat.DataBind() DropDownList1.DataSource = dt DropDownList1.DataTextField = &#34;Bussiness_name&#34; DropDownList1.DataValueField = &#34;Bussiness_name&#34; DropDownList1.DataBind() gvDepartments.DataSource = dt gvDepartments.DataBind() End If End Sub Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Dim btn As Button = TryCast((sender), Button) Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow) Dim category As String = (TryCast(row.FindControl(&#34;lblcat&#34;), Label)).Text Dim suCategory As String = (TryCast(row.FindControl(&#34;lblBname&#34;), Label)).Text If cat.Items.FindByValue(category) IsNot Nothing Then cat.ClearSelection() cat.Items.FindByValue(category).Selected = True End If If DropDownList1.Items.FindByValue(suCategory) IsNot Nothing Then DropDownList1.ClearSelection() DropDownList1.Items.FindByValue(suCategory).Selected = True End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/ZEhXGHh.gif" alt="" width="263" height="417" /></p>https://www.aspforums.net:443/Threads/124556/Set-DropDownList-Selected-value-on-GridView-Row-Button-Click-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/124556/Set-DropDownList-Selected-value-on-GridView-Row-Button-Click-using-C-and-VBNet-in-ASPNet/Fri, 06 Sep 2019 03:26:49 GMT