Solved

Why ASCx disappear if clicking button to Save?

Posted on 2010-08-27
8
632 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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