Dynamically loaded web user control hides on postback
Solution 1
Dynamically created controls must be recreated on every postback
, when you realise that each postback creates a new instance of the Page
class, and within this instance you must re-create all of the controls each and every time, this becomes more obvious.
Here is a good article on this
Another post on this that i answered
To maintain state between the postbacks, you can use ViewState
or ControlState
MSDN Control State vs View State Example
Solution 2
protected void ddlnew_SelectedIndexChanged(object o, EventArgs e)
{
ViewState["ddlnew_value"]=ddlnew.selectdeitem;
}
than in page load pu this
If(IsPostBack)
{
if(ViewState["ddlnew_value"]!=null)
{
ddlnew.selecteditem=ViewState["ddlnew_value"];
}
}
this should work
Solution 3
In ASP.NET, dynamically loaded controls require a bit of attention because of their behavior across postbacks. You must Maintain Viewstate for Dynamic controls across the postback or check that on which control's is generating postback you will load the control or not..
Check these articles ( Specially MSDN reference ) :
An Extensive Examination of User Controls - MSDN
Loading UserControl Dynamically in UpdatePanel
Maintain Viewstate for Dynamic controls across the postback
G.S Bhangal
Updated on June 18, 2022Comments
-
G.S Bhangal almost 2 years
i have a web custom control
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication5.WebUserControl1" %> <asp:DropDownList ID="ddlnew" AutoPostBack="true" runat="server" onselectedindexchanged="ddlnew_SelectedIndexChanged"> <asp:ListItem Text="text1" /> <asp:ListItem Text="text1" /> <asp:ListItem Text="text2" /> <asp:ListItem Text="text1" /> <asp:ListItem Text="text2" /> <asp:ListItem Text="text2" /> </asp:DropDownList>
and on the Default.aspx page
<asp:Button Text="PostBack" ID="btnPost" runat="server" onclick="btnPost_Click" />
and on the Default.aspx.cs
protected void btnPost_Click(object sender, EventArgs e) { WebUserControl1 uc = (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx"); PlaceHolder.Controls.Add(uc); }
and when we select any item from dropdown it postback and the control hide from the page
so please help how it can be prevented from hide
thanks
-
G.S Bhangal over 12 yearshow should i maintain the data that i entered before the control postback?
-
Richard Friend over 12 years
-
G.S Bhangal over 12 yearsI am using user control which have asp.net controls in it that have viewstate. Now if i reload the control something like showascx uc = (showascx)Page.LoadControl("~/showascx.ascx"); PlaceHolder.Controls.Add(uc); How it will maintain the data again in the new instance. Please specify
-
Richard Friend over 12 yearsAs long as you add the controls at the correct point in the page life cycle, this will be taken care of for you. see stackoverflow.com/questions/5432413/…
-
Richard Friend over 12 years
-
G.S Bhangal over 12 yearsThanks a lot, thats really usefull