Welcome Guest Search | Active Topics | Sign In | Register

Usercontrol + EO.Web.Dialog + Dynamically creating controls = Event doesn't wire up Options
Tbone
Posted: Thursday, August 9, 2007 7:44:28 AM
Rank: Newbie
Groups: Member

Joined: 7/17/2007
Posts: 5
Hi.

We've been struggling a lot of hours now with this problem.
The problem outline goes like this:

We've got an aspx page which in turn loads a usercontrol (inherited from the EO.Web.Dialog control) which displays a dialog when added to the page. - So far so good.

Next, in this custom usercontrol that inherits from the Dialog control, I'm adding code to create controls dynamically depending different factors.

Simplified sample code from the overrided OnLoad method:
Quote:

Button[] names;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
base.SetProperties();

System.Collections.Generic.List<Section> forumSections = Forums.GetForumsByForumGroupID(this.sectionID);

names= new Button[forumSections.Count];

for (int ii = 0; ii != forumSections.Count; ii++)
{
names[ii] = new Button();
names[ii].Text = forumSections[ii].Name;
names[ii].Click += new EventHandler(names_Click);

this.Controls.Add(names[ii]);
}
}

(I've tried the same code in the Load method, and in the Render method)

This is my current overrided Rendermethod, simplified:
Quote:

protected override void Render(HtmlTextWriter writer)
{
for (int ii = 0; ii != lbCategoryNames.Length; ii++)
{
names[ii].RenderControl(writer);
writer.Write("<br/>");
}
}


So far, so good. The controls render when the Render method runs the code and the Buttons are displayed in my dialog accordingly.

However, the registered event occuring in the first codesnippet, sample below, doesn't seem to connect to the eventhandler..
Quote:

names[ii].Click += new EventHandler(names_Click);


When I click the button nothing happens, at all.
We've been struggling with this issue for quite some time, and no matter what we do, there's always a new problem when using a similar approach.

When I set a debugger breakpoint inside the names_Click method, I can see that the code never gets there. Even though I've bound all the buttons to the eventhandler I can't seem to get it to run the method when the button is clicked.

Could we please get some assistance here, or are we out on deep water?
Are the dialogs made to be able to contain dynamic controls (and their events)
eo_support
Posted: Thursday, August 9, 2007 8:14:59 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,095
Hi Tobbe,

I am not sure why you want to inherit from the Dialog but overrides it's Render method without calling the base class's Render to render the dialog. But either way, the easiest way to see where the problem is is to view the rendered HTML source. You should see the button get an onclick event handler attached to it.

As for the question regarding whether the dialog is made to be able to contain dynamic controls. The answer is of course yes. From the dialog point of view, there is absolutely no distinguish between "static" and "dynamic" controls. The difference is who dynamically creates them. When the controls are "static" inside your .aspx file, it is actually code generated by ASP.NET compiler that dynamically creates them. When the controls are "dynamic", it is your code that dynamically creates them. So in fact all controls are always dynamically created, either by code generated by ASP.NET, or by code hand-written by you. This applies to every ASP.NET control, not just our control.

What's special about our Dialog is, while it is a container control, its content should be specified through ContentTemplate property, not through Controls property. So you may want to give that a try. You can put a standard ASP.NET Panel inside ContentTemplate and then use that Panel's Controls property to add your buttons.

Code: HTML/ASPX
<eo:Dialog runat="server" id="Dialog1">
    <ContentTemplate>
        <asp:Panel id="Panel1" runat="server"></asp:Panel>
    </ContentTemplate>
</eo:Dialog>


Once you have that, you can add your button like this:

Code: C#
//You must get the Panel this way because it is inside
//ContentTemplate, not directly on your page
Panel panel = Dialog1.ContentContainer.FindControl("Panel1");

//Now dynamically creates a button and add it to the panel
panel.Controls.Add(button);


It should be easier if you can put the Dialog into a User Control (.uscx) and encapsulate your logics inside that .uscx instead of inheriting from our dialog control because this way there is much less chance for you to accidently alter the dialog's behavior. In any case, you shouldn't need to touch the Render method.

Thanks
Tbone
Posted: Friday, August 10, 2007 2:28:31 AM
Rank: Newbie
Groups: Member

Joined: 7/17/2007
Posts: 5
I've got it to work using a slightly different approach (though, still need to use the Render method to render the controls in my usercontrols)

Thanks for the support, now I've got some UpdatePanel asp.net ajax problems instead, while dynamically creating controls.. The UpdatePanel doesn't work when I dynamically create the controls.. I must've missed something :)

Cheers
eo_support
Posted: Friday, August 10, 2007 6:35:01 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,095
Hi Tobbe,

You can try to create your controls in Page_Init event instead of Page_Load event. Controls that are dynamically created in Page_Load often have problem with view state because view state is loaded after Page_Init, but before Page_Load. Such problems may not surface on view state but surface on a lot other strange ways.

Thanks


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.