1

我有带有 itemTemplate 的 gridview 包含在 onCheckChanged 处触发的复选框。我想捕捉gridview所在的gridview列和行。

我该怎么做 ?

public void PM_chkbx_Chacked_Changed(object sender, EventArgs e)
{
     //What to write here?
}
4

2 回答 2

2

我在 gridview 中使用了datakeys并且能够获取行和特定列。这里 gridview id 是grdrequestlist

public void PM_chkbx_Chacked_CheckedChanged(object sender, EventArgs e)
{
//here you can get row on whcih you checkbox chnged event called

GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
string requestid = grdrequestlist.DataKeys[Row.RowIndex].Value.ToString();
string cellvalue=Row.Cells[1].Text;
}

这个gridview的.aspx页面设计在这里你可以在Columns部分添加你的列。

<asp:GridView ID="grdrequestlist" runat="server" DataKeyNames="RequestId" >
<Columns>
 <asp:TemplateField HeaderText="Actions" >
                 <ItemTemplate>
 <asp:CheckBox ID="PM_chkbx_Chacked" AutoPostBack="true" runat="server" oncheckedchanged="PM_chkbx_Chacked_CheckedChanged" />
                 </ItemTemplate>
                </asp:TemplateField>
                    </Columns>
</asp:GridView>

希望这对您有所帮助。

于 2012-11-22T10:12:31.020 回答
0

我找到了解决方案:

public void PM_chkbx_Chacked_Changed(object sender, EventArgs e)
    {
        GridViewRow gvr = ((GridViewRow)((Control)sender).Parent.Parent);
        int index_row = gvr.RowIndex;
        string role_name = gv_permissions_management.DataKeys[index_row].Value.ToString();
        string fileld_name = ((DataControlFieldCell)((Control)sender).Parent).ContainingField.HeaderText;
        ((DataControlFieldCell)((Control)sender).Parent).BackColor = Color.Red; //Color the Cell

     }
于 2012-11-22T13:34:01.497 回答