【问题标题】:Change property of CheckBox in RowCreated in GridView更改 GridView 中 RowCreated 中 CheckBox 的属性
【发布时间】:2012-07-03 07:46:40
【问题描述】:

我有一个包含 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_OnCheckedChanged 中获得chkStatus

rotected void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
    try
    {
        CheckBox chk = sender as CheckBox;
        string ID = chk.ID;

如何更改IDchkStatus?如果我无法将该字符串保存在可以进入chkStatus_OnCheckedChanged 的属性中?

谢谢

【问题讨论】:

  • 我不会触及 ID 属性,因为这是必需的(并在回发时重建)相反,我会使用复选框的属性。查看此链接以使用属性。 codeproject.com/Tips/88324/…

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


【解决方案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"];

【讨论】:

  • 以及如何在chkStatus_OnCheckedChanged1 中获得HiddenField 值?谢谢
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多