Solved

Why ASCx disappear if clicking button to Save?

Posted on 2010-08-27
8
627 Views
Last Modified: 2012-06-27
i added ascx control in tab control with C# codes. if you click any tabs. ASCX control load. tab control in update pane. Alos ASCX control includes button if you click button you can add some value to database but ASCX disappear. i think that it is reloaded. How can i solve it? i loaded ascx control if i click tab control. i have a button on ASCX. i clicked button ASCX disapper..
 protected void ASPxPageControl1_ActiveTabChanged(object source, DevExpress.Web.ASPxTabControl.TabControlEventArgs e)
        {

          if (ASPxPageControl1.ActiveTabPage.Name == "Ali Sp. Reqs")
                PhAliSpReqs.Controls.Add(UserControlHelper.LoadControl(this.Page, "~/EngWebUserControl/AliSpReqs.ascx"));
      else if (ASPxPageControl1.ActiveTabPage.Name == "Test")
                PhTest.Controls.Add(UserControlHelper.LoadControl(this.Page, "~/EngWebUserControl/Test.ascx"));

Open in new window

ASCX:
public partial class Test : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) {

    }

    public void Button1_Click(object sender, EventArgs e)
    {
        if (txtTest.Text != String.Empty)
        {
            Label1.Text = "Hello!";
        }
        else
            Label1.Text = "Error";
    }
}

Open in new window

if i clicked tab EVERY THING IS GOOD. But click button on ascx. ASCx control disappear. How can i solve it!!!
0
Comment
Question by:programmerist
  • 2
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:programmerist
ID: 33540567
ARE YOU THERE?
0
 
LVL 6

Expert Comment

by:thaytu888888
ID: 33540631
- When you click tab, it cause the page do asyn post back to the server, run Page_Load and run the ASPxPageControl1_ActiveTabChanged, then your ascx is added to the page, but when you click button on ascx, the page do asyn post back to the server, run Page_Load, but this time ASPxPageControl1_ActiveTabChanged will not fired.
- You need to check and load your ascx again when you click button, i advise you do it on OnInit event.
0
 

Author Comment

by:programmerist
ID: 33540858
@thaytu888888: is not clear sorry :))))
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33541170
Hi,

I am trying to explain you the reason behind along with thaytu888888 statement.

 The main reason is that  on button click your whole that tab panel loads again. So the dynamically loaded ascx controls vanish out on button click. So it would be nice if you can load those user controls again in OnInit.

Regards,
VSS
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33541178
Hi,

See same type example of loading controls dynamically at link:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_26383559.html

but not exactly as yours.

Regards,
VSS
0
 
LVL 9

Accepted Solution

by:
abhinayp86 earned 500 total points
ID: 33543114
Since u r loading the controls Runtime , u need to save the viewstate of ur controls during postbacks.
I am also doing similar thing in my website.

Here is a sample how to save the state of ur runtime controls. (it is the same with userctrls too)

without  "protected override void LoadViewState(object savedState)" ur runtime controls are likely to be lost during postbacks.


**** c# ***



protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

            CreateCtrls();



    }

    private void CreateCtrls()

    {

        DropDownList ddl1 = new DropDownList();

        ddl1.ID = "ddl1";

        ddl1.Items.Add("asdf0");

        ddl1.Items.Add("asdf1");

        ddl1.Items.Add("asdf2");

        ddl1.Items.Add("asdf3");

        pnl.Controls.Add(ddl1);

        DropDownList ddl2 = new DropDownList();

        ddl2.ID = "ddl2";

        ddl2.Items.Add("lkjh0");

        ddl2.Items.Add("lkjh1");

        ddl2.Items.Add("lkjh2");

        ddl2.Items.Add("lkjh3");

        pnl.Controls.Add(ddl2);



        ViewState["ControlsAdded2"] = true;//This is also imp to save and load viewstates

    }

    protected void btn1_Click(object sender, EventArgs e)

    {



    }

    protected override void LoadViewState(object savedState)

    {

        base.LoadViewState(savedState);

        if (ViewState["controlsadded1"] == null)

        {

            CreateCtrls();

        }

    }



**** aspx ***

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Panel runat="server" ID="pnl">

        </asp:Panel>

        <asp:Button runat="server" ID="btn1" onclick="btn1_Click" Text="click me" />

    </div>

    </form>

</body>

Open in new window

0
 
LVL 6

Expert Comment

by:thaytu888888
ID: 33549445
- When u click button on ascx control, u need to add the ascx again, that's all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now