Solved

Why ASCx disappear if clicking button to Save?

Posted on 2010-08-27
8
626 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

743 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

9 Experts available now in Live!

Get 1:1 Help Now