如何使用DataBinding设置asp:CheckBox控件的ID

时间:2022-08-24 13:08:09

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

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

<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>

but that gives me a:

但这给了我一个:

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" />

分析器错误消息:控件的ID属性只能使用标记中的ID属性和简单值进行设置。示例:

Anyone have an idea how to hack around that?

任何人都知道如何破解它?

2 个解决方案

#1


Anyone have an idea how to hack around that?

任何人都知道如何破解它?

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.

你不能,你不能。您可以将所需数据存储在ID之外的某个位置。至少,可以使用兄弟HiddenField。

<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>    

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

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

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

如果你真的想把WebForms混为一谈,你可以把你的数据放到CssClass中......但这太疯狂了。 ;)

#2


Not sure what you are intending to do here, but you can just go straight to the html controls ie <input type="checkbox" id="<%# some expresssion %>" />

不知道你打算在这里做什么,但你可以直接进入html控件,即”/>

Or you don't do this and query for it on the server side. I.e. find the row you want and then use FindControls() to get the checkbox for that row.

或者你不这样做,并在服务器端查询它。即找到你想要的行,然后使用FindControls()来获取该行的复选框。

#1


Anyone have an idea how to hack around that?

任何人都知道如何破解它?

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.

你不能,你不能。您可以将所需数据存储在ID之外的某个位置。至少,可以使用兄弟HiddenField。

<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>    

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

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

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

如果你真的想把WebForms混为一谈,你可以把你的数据放到CssClass中......但这太疯狂了。 ;)

#2


Not sure what you are intending to do here, but you can just go straight to the html controls ie <input type="checkbox" id="<%# some expresssion %>" />

不知道你打算在这里做什么,但你可以直接进入html控件,即”/>

Or you don't do this and query for it on the server side. I.e. find the row you want and then use FindControls() to get the checkbox for that row.

或者你不这样做,并在服务器端查询它。即找到你想要的行,然后使用FindControls()来获取该行的复选框。