Solved

Why ASCx disappear if clicking button to Save?

Posted on 2010-08-27
8
630 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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