0

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?
}
4

2 回答 2

1

我真的只需要点击的 ID 和 CheckBox 状态。以下对我有用。

 <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; 
}
于 2013-07-02T19:11:21.807 回答
-1

您需要将数据网格的数据源保持在会话或视图状态中。完成此操作后,您将需要知道选中复选框时单击的行。然后,您可以获得绑定到该行的对象。

于 2013-07-02T19:02:43.650 回答