Get Selected Checkbox Value from Gridview in Asp.net C#
In this Blog as Work with in gridview Selected Checkbox ID Value can Get In C#.
ASPX Code:
<div style="width: 104%; height: 243px; max-height: 243px; overflow: auto;">
<asp:GridView ShowHeader="false"
AutoGenerateColumns="false" ID="chklCategory"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server"
AutoPostBack="true"
ID="Checkbox_Category" Text='<%#bind("Name") %>' />
<asp:HiddenField runat="server"
ID="hidCheck"
Value='<%# bind("Id") %>' />
</ItemTemplate>
<ItemStyle
HorizontalAlign="Left"
/>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# Code:
public DataTable DTCatIds()
{
//string str = "";
DataTable dt = new DataTable();
dt.Columns.Add("Id");
foreach (GridViewRow row in
chklCategory.Rows)
{
CheckBox Checkbox = (CheckBox)row.FindControl("Checkbox_Category");
if (Checkbox.Checked)
{
HiddenField hidCheck = (HiddenField)row.FindControl("hidCheck");
//str += hidCheck.Value + ",";
dt.Rows.Add(hidCheck.Value);
dt.NewRow();
}
}
return dt;
//return str;
}
---------------------------------------------------------------
public String DTCatIds()
{
string str = "";
foreach (GridViewRow row in chklCategory.Rows)
{
CheckBox Checkbox = (CheckBox)row.FindControl("Checkbox_Category");
if (Checkbox.Checked)
{
HiddenField hidCheck = (HiddenField)row.FindControl("hidCheck");
str += hidCheck.Value + ",";
}
}
return str;
}
Comments
Post a Comment
Thank You for your Comment