2
votes

I'm trying to create a simple web page with a navigation bar and some usercontrols (ascx) programmatically loaded. All controls are inside an update panel.

When I click on a link button (from the navigation bar) I do the following things:

  1. I save the current usercontrol using viewstate.
  2. Than I reload the current usercontrol.

My 'page_load' always reloads the current control.

Always assigning the same ID to the programmatically loaded control allows me to save the usercontrol viewstate. So everything look good except one little thing: the usercontrol viewstate in not available during the usercontrol Page_Load!

Look below for (* HERE). The 'txtTest.Text' value is always "0" (also during postback).

It seems that the user control viewstate is restored after the (usercontrol) Page_Load. How is it possible?

--- "DEFAULT.ASPX": ---

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="sm" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="pnlMain" runat="server">
            <ContentTemplate>
                <div class="links">
                    <asp:LinkButton ID="lnkButton1" runat="server" OnClick="lnkButton1_Click" Text="Link 1"></asp:LinkButton>
                    <asp:LinkButton ID="lnkButton2" runat="server" OnClick="lnkButton2_Click" Text="Link 2"></asp:LinkButton>
                </div>
                <br />
                <asp:Panel ID="pnlCtrl" runat="server"></asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

--- "DEFAULT.ASPX.CS": ---

private string CtrlAscx
{
    get
    {
        if (ViewState["CtrlAscx"] == null)
        {
            ViewState["CtrlAscx"] = String.Empty;
        }

        return ViewState["CtrlAscx"].ToString();
    }
    set
    {
        ViewState["CtrlAscx"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    loadMyControl();
}

private void loadMyControl()
{
    if (!String.IsNullOrEmpty(CtrlAscx))
    {
        pnlCtrl.Controls.Clear();
        Control c = LoadControl(CtrlAscx);
        c.ID = CtrlAscx + "ID";         // this line is mandatory in order to mantain the usercontrol viewstate
        pnlCtrl.Controls.Add(c);
    }
}

protected void lnkButton1_Click(Object sender, EventArgs e)
{
    CtrlAscx = "Control1.ascx";
    loadMyControl();
}

protected void lnkButton2_Click(Object sender, EventArgs e)
{
    CtrlAscx = "Control2.ascx";
    loadMyControl();
}

-- "CONTROL1.ASCX" --

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Control1.ascx.cs" Inherits="WebTest.Control1" %>
Control1: <asp:TextBox id="txtTest" runat="server" Text="0"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" />

-- "CONTROL1.ASCX.CS" --

public partial class Control1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (txtTest.Text == "0")        // * HERE
        {
            txtTest.Text = "1";
        }
    }
}
1

1 Answers

0
votes

Try the EnableViewState="true" attribure on txtTest as well as on your custom user control when creating it :

.
.
c.EnableViewState = true;
pnlCtrl.Controls.Add(c);