2

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

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
G.S Bhangal
  • 3,060
  • 4
  • 23
  • 48

4 Answers4

4

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

And another

To maintain state between the postbacks, you can use ViewState or ControlState

MSDN Control State vs View State Example

Community
  • 1
  • 1
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • how should i maintain the data that i entered before the control postback? – G.S Bhangal Nov 30 '11 at 09:17
  • Use either `ViewState` or `ControlState` - http://msdn.microsoft.com/en-us/library/1whwt1k7(v=VS.80).aspx – Richard Friend Nov 30 '11 at 09:21
  • I 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 – G.S Bhangal Nov 30 '11 at 09:24
  • As long as you add the controls at the correct point in the page life cycle, this will be taken care of for you. see http://stackoverflow.com/questions/5432413/dynamically-create-controls-and-save-the-controls-values-across-postback-asp-n/5432613#5432613 – Richard Friend Nov 30 '11 at 10:08
  • also see http://stackoverflow.com/questions/5434521/adding-a-user-control-to-a-page-programatically-while-preserving-controls-alread/5434572#5434572 – Richard Friend Nov 30 '11 at 10:21
  • Thanks a lot, thats really usefull – G.S Bhangal Nov 30 '11 at 10:34
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

unarity
  • 2,339
  • 2
  • 21
  • 17
1

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

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

1) You need to enable view state on the page.

2) Like others in this post mentioned you need to recreate the dynamically loaded user control.

Here's an example:

protected void Page_Load(object sender, EventArgs e)
{
    Render();
}

Render Method:

private void Render()
{
    var list = Helpers.Content.Lists.GetListByTitle();
    if (list != null && list.Items.Count > 0)
    {
        this.divContainer.Controls.Clear();
        var i = 1;
        list.Items.ForEach(question =>
                               {
                                   this.divContainer.Controls.Add(RenderQuestion(question, i));
                                   i++;
                               });
    }
}

Render Question Method:

    private Control RenderQuestion(Entities.Modules.Item question, int count)
    {
        // Create question control
        var q = (Custom.Widgets.ActionPlans.New.Question)LoadControl("~/Custom/Widgets/ActionPlans/New/Question.ascx");

        // Render question
        q.Render(question, count);
        return q;
    }

Hope this helps.