I have a DataTable that is bound to a DataGrid. A CheckBox is created in a data row. When I check the checkbox the code behind is hit, but I don't know how to get at the DataRow.
<asp:DataGrid ID="dgCaseStatusTypes"
runat="server"
AutoGenerateColumns="False"
CellPadding="5"
DataKeyField="InmateCaseStatusID"
OnItemCommand="dgCaseStatusTypes_ItemCommand">
<Columns>
<asp:BoundColumn DataField="Code" HeaderText="Code"></asp:BoundColumn>
<asp:BoundColumn DataField="Text" HeaderText="Text"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Prebook Visible" >
<ItemTemplate>
<asp:CheckBox id="chkBox1"
runat="server"
AutoPostBack="true"
checked= '<%# Eval("IsPreBookVisibleBool") %>'
OnCheckedChanged="OnCheckedChanged_Event"
></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
protected void OnCheckedChanged_Event(object sender, System.EventArgs e)
{
CheckBox cb = sender as CheckBox;
//how to get the DataRow that created this control?
}
I really just needed the ID of what was clicked, and the CheckBox state. The following worked for me.
<asp:DataGrid ID="dgCaseStatusTypes"
runat="server"
AutoGenerateColumns="False"
CellPadding="5"
DataKeyField="InmateCaseStatusID"
OnItemCommand="dgCaseStatusTypes_ItemCommand">
<Columns>
<asp:TemplateColumn HeaderText="ID Label" Visible="false">
<ItemTemplate>
<asp:Label id="IDLabel"
runat="server"
AutoPostBack="true"
Text='<%# Eval("InmateCaseStatusID") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Code" HeaderText="Code"></asp:BoundColumn>
<asp:BoundColumn DataField="Text" HeaderText="Text"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Prebook Visible" >
<ItemTemplate>
<asp:CheckBox id="chkBox1"
runat="server"
AutoPostBack="true"
checked= '<%# Eval("IsPreBookVisibleBool") %>'
OnCheckedChanged="OnCheckedChanged_Event"
></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
protected void OnCheckedChanged_Event(object sender, System.EventArgs e)
{
CheckBox cb = sender as CheckBox;
bool isChecked = cb.Checked;
DataGridItem dgi = cb.NamingContainer as DataGridItem;
Label lbl = dgi.FindControl("IDLabel") as Label;
string Id = lbl.Text as string;
}
You will need to keep your datagrid's datasource in session or viewstate. Once you do this you will need to know the row you clicked on when you checked the checkbox. You can then get the object you bound to that row.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.