Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why ASCx disappear if clicking button to Save?

Posted on 2010-08-27
8
Medium Priority
?
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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