在后面的代码中获取 rad 网格内每个选中标记的复选框的值

get the value of each check marked check box inside rad grid in code behind

在我的 radgrid 中,我使用复选框 select 从多个选项中选择所需的值。在我后面的代码中,我想获取每个选中行的值。但是我的循环不工作。

我也不确定我是否对 rad 网格中的复选框使用了正确的方法。请推荐.

<telerik:RadGrid ID="rg_get_profession" runat="server"
PageSize="100"
AllowSorting="true"
Width="100%"
AllowPaging="True"
ShowGroupPanel="false"
AutoGenerateColumns="false"
GridLines="Both"
HeaderStyle-HorizontalAlign="Left" >
<MasterTableView AutoGenerateColumns="false" AllowFilteringByColumn="false" ShowFooter="false">
    <Columns>
        <telerik:GridTemplateColumn UniqueName="TempCol1" HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" />
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn DataField="p_id" Display="false" Visible="true" AutoPostBackOnFilter="false" CurrentFilterFunction="Contains" />
        <telerik:GridBoundColumn DataField="p_name" HeaderText="Name" AutoPostBackOnFilter="false" CurrentFilterFunction="Contains" />     
    </Columns>
</MasterTableView>
</telerik:RadGrid>

我想要做的是为数据库中的每个检查行插入“p_id”。但是我的循环不工作。请建议我如何实现这一目标。

后面的代码>>

foreach (GridDataItem item in rg_get_profession.Items)
{
    CheckBox chkbox = item.FindControl("TempCol1") as CheckBox;
    if(chkbox != null && chkbox.Checked)
    {
        string id = item.Cells[1].Text;
    }
}

调试代码时,尽管我在 rad 网格复选框中选中了多个项目,但执行并未进入 if 块。 我试着做 ::

foreach (GridDataItem item in rg_get_profession.MasterTableView.Items)
    {
        CheckBox chkbox = item.FindControl("CheckBox1") as CheckBox;
        if (chkbox != null && chkbox.Checked)
        {           
            string id_ = item.Cells[1].Text;            
        }
    }

现在,当我在复选框中打勾时,我的执行进入循环 if (chkbox != null && chkbox.Checked) 但我无法访问 p_id 的值. string id_ = item.Cells[1].Text; 没有给出 p_id 的值。

如何访问 p_id 的值,这些值被勾选了?请提出建议。

我将 item.Cells[1].Text; 更改为 item["p_id"].Text;。 获取数据字段值的代码片段 ::

foreach (GridDataItem item in rg_get_profession.MasterTableView.Items)
    {
        CheckBox chkbox = item.FindControl("CheckBox1") as CheckBox;
        if (chkbox != null && chkbox.Checked)
        {           
            string id_ = item["p_id"].Text;            
        }
    }

我可以得到勾选标记p_id的值。