Answered by:
On button click - Get selected Value in grdiview

Question
-
User-588203678 posted
Hi,
I have a button outside gridview, The gridview has few columns
Onclick of button I need to get selected checkbox value i.e IDs 2,3 if 2 and 3 are selected
Select ID Name
2 sam
3 Sunil
protected void btn_Click1(object sender, EventArgs e) { string strSelectedID = ""; grdRisk.DataSource = SqlDataSource1; grdRisk.DataBind(); string data = ""; foreach (GridViewRow gvrow in grdRisk.Rows) { CheckBox chk = (CheckBox)gvrow.FindControl("chkEdit"); if (chk != null & chk.Checked) { Label lblIDS = gvrow.FindControl("lblID") as Label; strSelectedID += lblIDS.Text + ","; } }
Tuesday, June 28, 2016 5:42 PM
Answers
-
User1559292362 posted
Hi guhananth1,
Onclick of button I need to get selected checkbox value i.e IDs 2,3 if 2 and 3 are selectedBased on your description, I create a demo as below for your reference.
ASPX.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetCheckedId.aspx.cs" Inherits="WebFormDemo.GridViewDemo.GetCheckedId" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="150" /> <asp:TemplateField HeaderText="Name" ItemStyle-Width="150"> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPDOTNETDEMOConnectionString %>" SelectCommand="SELECT [Id], [Name] FROM [GridViewTable1]"></asp:SqlDataSource> <br /> <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" /> <u>Selected Rows</u> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Code Behind
using System; using System.Collections.Generic; using System.Web.UI.WebControls; namespace WebFormDemo.GridViewDemo { public partial class GetCheckedId : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { this.GridView1.DataSource = SqlDataSource1; this.GridView1.DataBind(); } } protected void GetSelectedRecords(object sender, EventArgs e) { List<string> ids = new List<string>(); foreach (GridViewRow row in GridView1.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox); if (chkRow.Checked) { ids.Add(row.Cells[1].Text); } } } Label1.Text = string.Join(",",ids.ToArray()); } } }
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 29, 2016 5:15 AM -
User-1034726716 posted
Hi Sumit,
I moved the code to Not IsPostBack in page load,still the checkbox values are not captured
if(!PageIsPostBack) rdRisk.DataSource = SqlDataSource1; grdRisk.DataBind();
You need to wrap those within curly braces:
if(!IsPostBack){ //code for databind grdRisk.DataSource = SqlDataSource1; grdRisk.DataBind(); }
And make sure to remove your codes for binding your GridView in your Button Click handler.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 29, 2016 5:31 AM
All replies
-
User-1034726716 posted
Calling DataBind() on Button click event will cause the GridView to reset on postbacks. This will also reset the state of the CheckBox. You would need to bind your GridView in Page_Load event within Not IsPostback block.
Tuesday, June 28, 2016 6:05 PM -
User-1672470423 posted
Move you binding code to Page_Load and check if it works then
grdRisk.DataSource = SqlDataSource1;
grdRisk.DataBind();grdRisk.DataSource = SqlDataSource1; grdRisk.DataBind();
Tuesday, June 28, 2016 6:19 PM -
User-1034726716 posted
Move you binding code to Page_Load and check if it works then
grdRisk.DataSource = SqlDataSource1;
grdRisk.DataBind();You would still need to put that code within Not IsPostback block:
if(!IsPostBack){ //code for databind }
Tuesday, June 28, 2016 6:51 PM -
User-588203678 posted
Hi Sumit,
I moved the code to Not IsPostBack in page load,still the checkbox values are not captured
if(!PageIsPostBack) rdRisk.DataSource = SqlDataSource1; grdRisk.DataBind();
Wednesday, June 29, 2016 12:41 AM -
User1559292362 posted
Hi guhananth1,
Onclick of button I need to get selected checkbox value i.e IDs 2,3 if 2 and 3 are selectedBased on your description, I create a demo as below for your reference.
ASPX.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetCheckedId.aspx.cs" Inherits="WebFormDemo.GridViewDemo.GetCheckedId" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkRow" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="150" /> <asp:TemplateField HeaderText="Name" ItemStyle-Width="150"> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPDOTNETDEMOConnectionString %>" SelectCommand="SELECT [Id], [Name] FROM [GridViewTable1]"></asp:SqlDataSource> <br /> <asp:Button ID="btnGetSelected" runat="server" Text="Get selected records" OnClick="GetSelectedRecords" /> <u>Selected Rows</u> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Code Behind
using System; using System.Collections.Generic; using System.Web.UI.WebControls; namespace WebFormDemo.GridViewDemo { public partial class GetCheckedId : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { this.GridView1.DataSource = SqlDataSource1; this.GridView1.DataBind(); } } protected void GetSelectedRecords(object sender, EventArgs e) { List<string> ids = new List<string>(); foreach (GridViewRow row in GridView1.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox); if (chkRow.Checked) { ids.Add(row.Cells[1].Text); } } } Label1.Text = string.Join(",",ids.ToArray()); } } }
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 29, 2016 5:15 AM -
User-1034726716 posted
Hi Sumit,
I moved the code to Not IsPostBack in page load,still the checkbox values are not captured
if(!PageIsPostBack) rdRisk.DataSource = SqlDataSource1; grdRisk.DataBind();
You need to wrap those within curly braces:
if(!IsPostBack){ //code for databind grdRisk.DataSource = SqlDataSource1; grdRisk.DataBind(); }
And make sure to remove your codes for binding your GridView in your Button Click handler.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 29, 2016 5:31 AM