C# 更改GridView中创建的行中复选框的属性

C# 更改GridView中创建的行中复选框的属性,c#,asp.net,c#-4.0,gridview,C#,Asp.net,C# 4.0,Gridview,我有一个包含复选框的gridview <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="GridView1_RowCreated" BackColor="#F1EFC5" CssClass="SearchEveryWhere_Table" Width="500px"> <HeaderStyle BackColor="#5391DD" Fore

我有一个包含复选框的gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="GridView1_RowCreated"
    BackColor="#F1EFC5" CssClass="SearchEveryWhere_Table" Width="500px">
    <HeaderStyle BackColor="#5391DD" ForeColor="White" />
    <Columns>
      <asp:BoundField DataField="CityCode" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell hideElement" />
      </asp:BoundField>
      <asp:BoundField DataField="Switch" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"  Visible="false">
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell hideElement" />
      </asp:BoundField>
      <asp:BoundField DataField="Page_Number" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"  HeaderText="Page">
           <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell" />
      </asp:BoundField>
      <asp:BoundField DataField="Name" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"  HeaderText="Title">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell" />
      </asp:BoundField>
      <asp:TemplateField HeaderText="Access">
          <ItemTemplate>
               <asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Access")) %>' ID="chkStatus" ClientIDMode="Static" />
           </ItemTemplate>
           <HeaderStyle HorizontalAlign="Center" />
           <ItemStyle HorizontalAlign="Center" />
       </asp:TemplateField>
   </Columns>
 </asp:GridView>
问题是当我想获取
ID
时,我在
chkStatus\u OnCheckedChanged
中获取
chkStatus

rotected void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
    try
    {
        CheckBox chk = sender as CheckBox;
        string ID = chk.ID;
如何更改
chkStatus
ID
?如果我无法将该字符串保存在属性中,而该属性可以在
chkStatus\u OnCheckedChanged

谢谢你更正了我的代码

在OnRowDatabound中添加

((CheckBox)e.Row.Cells[4].FindControl("chkStatus")).Attributes.Add("MyData", string.Format("{0}_{1}_{2}", CurrentREcord.CityCode, CurrentREcord.Page_Number, CurrentREcord.Switch.ToString());
在复选框选中/取消选中事件中,使用此选项获取数据

string data = chkStatus.Attributes["myData"];

您可以使用checkBox.Attributes.Add方法将值存储在自定义属性中,也可以在复选框附近插入HiddenField控件,并将所需的值存储在其value属性中(更好)。

我不会接触ID属性,因为这是必需的(并在回发时重建)相反,我将使用复选框的属性。。查看此链接了解如何使用attibutes。以及如何在检查更改1时获取
chkStatus\u中的
HiddenField
值?谢谢
string data = chkStatus.Attributes["myData"];