在GridView中更改RowCreated中CheckBox的属性

时间:2012-07-03 07:46:40

标签: c# asp.net c#-4.0 gridview

我有一个包含CheckBox的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>

我想要将此CheckBox的更改ID改为这样的值:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            sp_GetPageAccess08_New_Result CurrentREcord = (sp_GetPageAccess08_New_Result)e.Row.DataItem;
            e.Row.Cells[4].FindControl("chkStatus").ID = "chkStatus_" + CurrentREcord.CityCode + "_" + CurrentREcord.Page_Number + "_" + CurrentREcord.Switch.ToString();
            if (CurrentREcord.Access == false)
            {
                e.Row.BackColor = System.Drawing.Color.FromArgb(252, 212, 243);
            }
        }
    }
    catch (Exception ex)
    {

    }
}

问题是,当我想ID chkStatus chkStatus_OnCheckedChanged时,rotected void chkStatus_OnCheckedChanged(object sender, EventArgs e) { try { CheckBox chk = sender as CheckBox; string ID = chk.ID;

ID

如何更改chkStatus的{​​{1}}?如果我不能将该字符串保存在可以进入chkStatus_OnCheckedChanged的属性中?

感谢

2 个答案:

答案 0 :(得分:1)

更正我的代码

在你的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"];

答案 1 :(得分:0)

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