Hi,
This is my scenario, I'm can't figure out why the UpdatePanel doesn't update on a child control event.
I have a WebUserControl MyUserControl.ascx that looks like this:
1<asp:UpdatePanel ID="upnlMain" runat="server" ChildrenAsTriggers="True" UpdateMode="Conditional">
2 <ContentTemplate>
3 <asp:Panel ID="pnlContainer" runat="server">
4 </asp:Panel>
5 <asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
6 </ContentTemplate>
7</asp:UpdatePanel>
In code behind I'm dynamically loading a dropdownbox as follows:
1protected void Page_Load(object sender, EventArgs e)
2 {
3 DropDownList ddl =new DropDownList();
4 ddl.Items.Add(new ListItem("aaa","aaa"));
5 ddl.Items.Add(new ListItem("bbb","bbb"));
6 ddl.Items.Add(new ListItem("ccc","ccc"));
7 ddl.AutoPostBack =true;
8 ddl.SelectedIndexChanged +=new EventHandler(ddl_SelectedIndexChanged);
9this.pnlContainer.Controls.Add(ddl);
10 }
1112void ddl_SelectedIndexChanged(object sender, EventArgs e)
13 {
14this.lblTest.Text = ((DropDownList)sender).SelectedItem.Text;
15 }
In my Default.aspx web page I have one single palceholder:
1<asp:PlaceHolder ID="phHolder" runat="server"></asp:PlaceHolder>The idea is to load MyUserControl.ascx web user control dynamically into the placeholder, making sure the UpdatePanel of the user controls gets updated on dropdown list event.
I'm adding this WebUserControl:
1protected void Page_Load(object sender, EventArgs e)
2 {
3 UserControl control = (UserControl)LoadControl("~\\MyUserControl.ascx");
4 controlElement.ID ="MyControl";
5 controlElement.EnableViewState =true;
6this.phHolder.Controls.Add(control);
7 }
Everything works fine except the the updatepanel's update, for some reason, unknown to me, the drowpdown list does a full postback, ignoring the updatepanel that contains it. I'm not sure if I am doing something wrong or what I'm trying to achieve is not doable.
Any help is greatly appreciated.
Thanks,
Virgil
Hi virgilhretcanu,
In the UserControl you need to add the "ddl_SelectedIndexChanged" event to the updatePanel's Trigger Collection. Try like this:
protectedvoid Page_Load(object sender,EventArgs e){
DropDownList ddl =newDropDownList();ddl.ID ="ddl1";
ddl.Items.Add(newListItem("aaa","aaa"));ddl.Items.Add(newListItem("bbb","bbb"));
ddl.Items.Add(newListItem("ccc","ccc"));ddl.AutoPostBack =true;
ddl.SelectedIndexChanged +=newEventHandler(ddl_SelectedIndexChanged);this.pnlContainer.Controls.Add(ddl);
AsyncPostBackTrigger trigger =newAsyncPostBackTrigger();trigger.ControlID = ddl.ID;
trigger.EventName ="SelectedIndexChanged";this.upnlMain.Triggers.Add(trigger);}
void ddl_SelectedIndexChanged(object sender,EventArgs e){
this.lblTest.Text = ((DropDownList)sender).SelectedItem.Text;}
Hope this will help you.
Thanks & Regards,
Rajak Shaik.
// Please Mark as Answer if this post helps you.
It works like a charm. Thank you very much Rajak
No comments:
Post a Comment