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 CheckBox checked (selected) on GridView Edit 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;asp:CheckBox ID=&#34;Cashpay&#34; Text=&#34;Cash&#34; runat=&#34;server&#34; CssClass=&#34;bg-info&#34; /&gt; &lt;asp:CheckBox ID=&#34;Cardpay&#34; Text=&#34;Card&#34; runat=&#34;server&#34; CssClass=&#34;bg-info&#34; /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &nbsp;&nbsp;&nbsp; &lt;Columns&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TemplateField HeaderText=&#34;Payment Mode&#34;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ItemTemplate&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Label ID=&#34;lblId&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ItemTemplate&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/asp:TemplateField&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TemplateField HeaderText=&#34;Payment Mode&#34;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ItemTemplate&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Label ID=&#34;lblpay&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;payment_mode&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ItemTemplate&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/asp:TemplateField&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:TemplateField HeaderText=&#34;Payment Mode&#34;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ItemTemplate&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Button Text=&#34;Edit&#34; runat=&#34;server&#34; OnClick=&#34;btnEdit_Click&#34; /&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ItemTemplate&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/asp:TemplateField&gt; &nbsp;&nbsp;&nbsp; &lt;/Columns&gt; &lt;/asp:GridView&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; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient Imports System.Configuration </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) { &nbsp;&nbsp;&nbsp; if (!this.IsPostBack) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindGrid(); &nbsp;&nbsp;&nbsp; } } private void BindGrid() { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand("SELECT Id, payment_mode FROM UpdatePaymentMode", con); &nbsp;&nbsp;&nbsp; SqlDataAdapter da = new SqlDataAdapter(cmd); &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; da.Fill(dt); &nbsp;&nbsp;&nbsp; GridView1.DataSource = dt; &nbsp;&nbsp;&nbsp; GridView1.DataBind(); } protected void btnEdit_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; Button btn = sender as Button; &nbsp;&nbsp;&nbsp; GridViewRow grow = btn.NamingContainer as GridViewRow; &nbsp;&nbsp;&nbsp; string value = (grow.FindControl("lblpay") as Label).Text; &nbsp;&nbsp;&nbsp; if (value == "Cash") &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = false; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (value == "Card,Cash") &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = true; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (value == "Cash,Card") &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = true; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if (value == "Card") &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = false; &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If Not Me.IsPostBack Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindGrid() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Private Sub BindGrid() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = New SqlCommand("SELECT Id, payment_mode FROM UpdatePaymentMode", con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridView1.DataSource = dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GridView1.DataBind() &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim btn As Button = TryCast(sender, Button) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim grow As GridViewRow = TryCast(btn.NamingContainer, GridViewRow) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim value As String = (TryCast(grow.FindControl("lblpay"), Label)).Text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If value = "Cash" Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If value = "Card,Cash" Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If value = "Cash,Card" Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If value = "Card" Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cardpay.Checked = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cashpay.Checked = False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp; End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/J1rSti7.gif" alt="" width="249" height="344" /></p>https://www.aspforums.net:443/Threads/668086/Set-CheckBox-checked-selected-on-GridView-Edit-Button-Click-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/668086/Set-CheckBox-checked-selected-on-GridView-Edit-Button-Click-using-C-and-VBNet-in-ASPNet/Wed, 31 Jul 2019 03:23:03 GMTSet CheckBox checked (selected) on GridView Edit Button Click using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme,</a></p> <p>Refer below link i have updated code.</p> <p><a href="https://www.aspforums.net/Threads/668086/how-to-set-checked-value-in-two-different-checkbox-on-edit-button-click-in-aspnet-c/Replies/4#Replies">https://www.aspforums.net/Threads/668086/how-to-set-checked-value-in-two-different-checkbox-on-edit-button-click-in-aspnet-c/Replies/4#Replies</a></p>https://www.aspforums.net:443/Threads/668086/Set-CheckBox-checked-selected-on-GridView-Edit-Button-Click-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/668086/Set-CheckBox-checked-selected-on-GridView-Edit-Button-Click-using-C-and-VBNet-in-ASPNet/Wed, 31 Jul 2019 04:12:42 GMT