【问题标题】:Checkbox itemtemplate templatefield text after databound数据绑定后的复选框项模板模板字段文本
【发布时间】:2010-07-27 18:42:57
【问题描述】:

我的 gridview 中有以下复选框:

<asp:TemplateField HeaderText="Active">
    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "Active")%>
        <asp:CheckBox ID="Active" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

而且它工作得很好。我用一个布尔值填充它。问题是它在gridview中显示字符串文本,例如:

真 [x] 错误的 [ ] 真[x]

这么久...我只想显示复选框。我在 rowDataBound 事件中试过这个:

if (result.Active)
   {
       ((CheckBox)e.Row.FindControl("Active")).Checked = true;
       ((CheckBox)e.Row.FindControl("Active")).Text = string.Empty;
   }

但它不起作用。有办法吗?

谢谢,

佩德罗·杜索

【问题讨论】:

    标签: asp.net gridview itemtemplate


    【解决方案1】:

    你为什么不直接使用 CheckBoxField 而不是 TemplateField?

    <asp:CheckBoxField DataField="Active" HeaderText="Active" />
    

    如果您因为插入/编辑而必须使用 TemplateField,那么您应该可以这样做

    <asp:TemplateField>
         <ItemTemplate>
              <asp:CheckBox id="CheckBoxActive" runat="server" Checked='<%#Eval("Active") %>' />
         </ItemTemplate>
    </asp:TemplateField>
    

    【讨论】:

    • 非常感谢,这个解决方案运行良好;是的,我使用 templateField 因为插入...
    • 再问一个问题,如果我需要获取已检查的零钱,我该怎么做?只需在 aspx 中声明 de OnCheckedChange,然后他们在 .cs 中创建方法?
    • 是的!这就是你所要做的
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多