简体   繁体   English

如何从UI控件获取DataRow到ASP.NET中的ascx.cs?

[英]How to get DataRow from UI control to ascx.cs in ASP.NET?

I have a DataTable that is bound to a DataGrid. 我有一个绑定到DataGrid的DataTable。 A CheckBox is created in a data row. 在数据行中创建一个CheckBox。 When I check the checkbox the code behind is hit, but I don't know how to get at the DataRow. 当我选中复选框时,后面的代码被命中,但是我不知道如何获得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. 我真的只需要单击的ID和CheckBox状态。 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. 然后,您可以获取绑定到该行的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM