Wednesday, March 21, 2012

trouble finding a sample using usercontrols and updatepanels

Hi all,

We have created a menu system primairy based on inserting usercontrols inside one single designed page with several areas's. This works very nice for our sites. Of coarse postback occurs on every click or change.

Some functionality inside usercontrols do a lot of postback, so I would be nice to use the update panel for this type of change.

When trying to implement this I ran into all sorts of trouble, so my first step is to hit this forum. From what i find i'm not the only one having some trouble using usercontrols in combination with the update panel.

So question: Does anyone have a working sample which i could have a look at. Probably can't use it 1 on 1 but well it would a nice starting point.

thanks,

-- jan willem

ps. Does anyone notice .. that all the the ajax stuff (including of coarse atlas) looks REAL nice but when u start to implement it it does realy really takes a lot of time learning to program again :-) Well it's new (ehh well kinda), better and customers do love it.

I wrote a sample to have a test with Ajax and User Control.

http://www.box.net/public/static/q4ltbl5an9.zip

It's simple and straight forward:

1. One user control is linkbutton group, auto generated during runtime;

2. Another user control is to display information according to the user control above;

3. Event subscription is used for information delivery.

4. The link which clicked should be set into different style.

Hope it's helpful.

This was researched days before, now I'm running into the problem of Atlas in live.com. It seems doesn't work with EFP at all!


Thanks for the sample it is usefull!

The only trouble is that the atlas controls (scriptmanager, updatepanel) are still created in the page and not in the usercontrol.

I've created a very simplistic sample of what i'm looking for. Just a page and a usercontrol. I put them in the link below.

http://www.bataviagroep.nl/sampleAtlasUC.zip

- default.aspx --> no atlas, just page and usercontrol doing what it should do .. handling click
- atlas-page.aspx --> same as above, all in one page using atlas and working fine (i added the picture so it's very clear that only the notfication changes

- default-atlas.apsx (same as the first default.aspx) but with the atlas inserted in the usercontrol. This one throws the following exeption:

"The UpdatePanel 'UpdatePanel1' was not present when the page's InitComplete event was raised. This is usually caused when an UpdatePanel is placed inside a template."

For those who don't like downloading stuff from a different server:

default.aspx
<body>
<formid="form1"runat="server">
<div>
<asp:PanelID="panel1"runat="server"/>
</div>
</form>
</body
code behind:
ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

panel1.Controls.Add(Page.LoadControl("~/uc-atlas.ascx"))

EndSub

The usercontrol uc.ascx
<asp:LinkButtonID="LinkButton1"runat="server">click A</asp:LinkButton>
<asp:LinkButtonID="LinkButton2"runat="server">click B</asp:LinkButton>
<br/><br/>
<asp:LabelID="Label1"runat="server"Text="Label">not clicked</asp:Label
code behind:

ProtectedSub LinkButton1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles LinkButton1.Click

Label1.Text ="clicked A"

EndSubProtectedSub LinkButton2_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles LinkButton2.Click

Label1.Text =

"clicked B"EndSub

The same user control but then with atlas stuff added:

<atlas:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="true">

</atlas:ScriptManager>

<asp:LinkButtonID="LinkButton1"runat="server">click A</asp:LinkButton>

|

<

asp:LinkButtonID="LinkButton2"runat="server">click B</asp:LinkButton>

<

br/>

<

br/>

<

atlas:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:LabelID="Label1"runat="server"Text="Label">not clicked</asp:Label></ContentTemplate><Triggers><atlas:ControlEventTriggerControlID="LinkButton1"EventName="Click"/><atlas:ControlEventTriggerControlID="LinkButton2"EventName="Click"/></Triggers>

</

atlas:UpdatePanel>

code behind is the same as above.

--> the above doesn't work and throws an expection.

I would love a sample or idea how to fix this

thanks,

-- jan willem

ps. all the code is in the zipfile


load the custom user control in the Page_Init event, it will work then

wow that worked ...

great that was th easy asnwer i kept missing thanks!

--jw


You might try putting a Placeholder control inside the content template of the UpdatePanel, use FindControl() on the UpdatePanel to locate the Placeholder, and use Placeholder.Controls.Add() to add your dynamically created user control from LoadControl().

The key here is that the container for the user control must exist before OnInit(), and the only way to accomplish this is the declaratively put something there first.

I have gotten this scenario to work. Adding anything directly to the UpdatePanel after OnInitComplete() will not work.

No comments:

Post a Comment