Search code examples
asp.netupdatepanelcode-behindunique-id

Setting the UniqueId of an ASP.NET control in code behind


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.


Solution

  • You can set the ID property of the created controls.