views:

243

answers:

2

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>

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

Anyone have an idea how to hack around that?

A: 

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

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.

David McEwing
The upshot is I need to store info in the ID tag. See this: http://stackoverflow.com/questions/1180443/how-to-programmatically-create-and-use-a-list-of-checkboxes-from-asp-net
BCS
+1  A: 

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.

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

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

Mark Brackett
That's probably the correct way to do it. Unfortuenetly I rather suspect that my use case would muck it up because `c.Parent.FindControl` isn't going to work until I rebuild the form and I need to use the checkbox data before I rebuild it. -- OTOH I might be able to use some client side JS to play with the hidden field...
BCS
@BCS "I need to use the checkbox data before I rebuild [the form]" - I don't quite understand that. You didn't have the checkbox data the least time you built it, why do you need it now? you just need to create the checkboxes after Postback the same as before. Then, ASP.NET will take care of calling Checked_Changed on anything that's checked. *Then* you can remove (or rebuild) elements of the form if you want.
Mark Brackett