【问题标题】:ASP.net can't set checkboxes value!ASP.net 无法设置复选框值!
【发布时间】:2011-03-10 01:14:50
【问题描述】:
CheckBox newBox = new CheckBox();
newBox.Text = dtCommon[i].userName;
newBox.CssClass = "cbox";
newBox.Attributes["value"] = dtCommon[i].id.ToString();
ApprovalSelectPanel.Controls.Add(newBox);

呈现为:

<input id="ctl00_mainContent_ctl00" type="checkbox" name="ctl00$mainContent$ctl00" checked="checked" />

如何获得价值属性?我的 JQuery 需要访问它!

【问题讨论】:

    标签: asp.net checkbox


    【解决方案1】:

    我敢打赌它正在设置属性,但在包含跨度上(查找一个元素)。

    您想改用InputAttributes 属性:

    newBox.InputAttributes["value"] = dtCommon[i].id.ToString();
    

    【讨论】:

      【解决方案2】:
       newBox.Attributes.Add("yourAttributeName", "yourAttributeValue");
      

      编辑:对不起,我忘了复选框的作用有点不同,所以你需要这样做:

      newBox.InputAttributes.Add("yourAttributeName", "yourAttributeValue");
      

      如果您想访问复选框控件周围的span,则原件可以使用,或者您可以这样做:

      newBox.LabelAttributes.Add("yourAttributeName", "yourAttributeValue");
      

      【讨论】:

      • 仍然没有渲染任何值属性
      【解决方案3】:

      你可以试试newBox.Attributes.Add("Value", dtCommon[i].id.ToString());

      【讨论】:

        【解决方案4】:

        如果您需要在复选框上存储一个值,我建议使用除值之外的其他内容,例如“MyValue”。您仍然可以在稍后的处理过程中使用 .Attributes 方法获取此“MyValue”。在 jquery 中,您可以使用 .attr('MyValue') 来获取值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-06-26
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          • 2012-04-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多