如何使用DataBinding设置asp的ID:CheckBox控件 [英] How to use DataBinding to set the ID of asp:CheckBox controls

查看:345
本文介绍了如何使用DataBinding设置asp的ID:CheckBox控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些ASP,我想看起来像这样:

I have some ASP that I want to look kinda of like this:

<asp:DataGrid ID="dgEnum" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:CheckBox ID="<%# some big DataBinder expression %>" runat="server" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

但是给我一个:


解析器错误消息:控件的ID属性只能使用标签中的ID属性和简单值进行设置。示例:< asp:Button runat =serverid =Button1/>

Parser Error Message: The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

任何人都有一个想法如何破解?

Anyone have an idea how to hack around that?

推荐答案


一个想法如何破解?

Anyone have an idea how to hack around that?

你不能,而不是。您可以在ID之外的某个地方存储所需的数据。至少,兄弟姐妹 HiddenField 可以

You can't, and you don't. You can store the required data somewhere besides the ID. At the very least, a sibling HiddenField could be used.

<script runat="server">
    void Checkbox_CheckedChanged(object sender, EventArgs e) {
       var c = (Checkbox)sender;
       if (!c.Checked) {
           return;
       }
       var hfv = (HiddenField)c.Parent.FindControl("hfvMyData");
       var myData = hfv.Value;
       /* Do something with myData */
    }
</script>

<asp:DataGrid ID="dgEnum" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>
               <asp:CheckBox runat="server" OnCheckedChanged="Checkbox_CheckedChanged" />
               <asp:HiddenField id="hfvMyData" runat="server" Value='<%# Eval("MyData") %>' />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>    

其他选项可能是DataKeys,服务器端(可能是缓存或会话数据)索引列表,或ViewState索引列表。

Other options could be DataKeys, a server side (perhaps cached or Session data) indexed list, or a ViewState indexed list.

如果您真的想混合WebForms,可以将数据放入CssClass ...但这只是疯狂。 ;)

If you really wanted to bastardize WebForms, you can put your data into CssClass...but that's just crazy. ;)

这篇关于如何使用DataBinding设置asp的ID:CheckBox控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆