0

I have a bunch of dynamically created checkboxes in code-behind like so:

CheckBox[] checks = new CheckBox[n];

Where n is an integer computed at run-time. I have added all of these checkboxes to a grid, so they are all visible in my ASP.NET webpage, but now I want to add them all as triggers to an update panel. Thus, I need to do this in code-behind:

<asp:UpdatePanel id="upPanel" runat="server">
    <ContentTemplate>
        // ...Contents of UpdatePanel...
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID=[Insert CheckBox Unique ID here] EventName="Click" />

            //... n AsyncPostBackTriggers, one for each CheckBox ...

        <asp:AsyncPostBackTrigger ControlID=[Insert CheckBox Unique ID here] EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

But I realize that my dynamically created CheckBoxes have no UniqueID, so I can't set the ControlID of the AsyncPostBackTrigger. Is there a way to set the unique id of an ASP.NET control in code-behind?

I tried this so far:

for (int i=0; i<n; i++)
{
    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
    trigger.ControlID = checks[i].UniqueID;
    trigger.EventName = "Click";
    upPanel.Triggers.Add(trigger);
}

But in the debugger, I see that checks[i].UniqueID = "", which doesn't really work.

user3685285
  • 6,066
  • 13
  • 54
  • 95
  • What happens when you try to set the `UniqueID` property? – John Saunders Sep 09 '14 at 17:38
  • checks[i].UniqueID = "checkbox_" + Convert.ToString(i); It says that the UnieuqID property is read-only, and cannot be edited like that. – user3685285 Sep 09 '14 at 17:46
  • 1
    Try the [`ID`](http://msdn.microsoft.com/en-us/library/system.web.ui.control.id(v=vs.110).aspx) property. – John Saunders Sep 09 '14 at 17:49
  • Great, that worked! But now I have a different problem. I wanted to use this method to get rid of postbacks, but it seems that the panel doesn't update as expected. I'll leave that for a different question. Thanks! – user3685285 Sep 09 '14 at 18:05

1 Answers1

1

You can set the ID property of the created controls.

John Saunders
  • 160,644
  • 26
  • 247
  • 397