简体   繁体   中英

asp.net Update Panel inside a user control not working

I am working on an asp.net/C# web application,

In the main page:

I have a place Holder inside of an update panel

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>
    </Triggers>
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server">
        </asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

I use this to dynamicaly load a web user control when I click on "LinkButton1"

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="Link1_Click">Load</asp:LinkButton></li>

in the code behind:

protected void Link1_Click(object sender, EventArgs e)
{
    PlaceHolder1.Controls.Clear();
    (MyControl) CC = (MyControl)LoadControl("Controls/MyControl.ascx");
    PlaceHolder1.Controls.Add(CC);
}

In the control I am loading I placed an update panel which is triggered by a check box in the control.

In MyControl:

<asp:UpdatePanel ID="MapRefresh" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="CheckBox1" EventName="CheckedChanged" />            
    </Triggers>
    <ContentTemplate>
        //Here I place the things I want to update
    </ContentTemplate>
</asp:UpdatePanel>

The checkbox is declared outside the update panel

<asp:CheckBox ID="CheckBox1" Checked="true" Text="View" runat="server" AutoPostBack="True" oncheckedchanged="CheckBox1_CheckedChanged" />

in the code behind:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    //Do the update on the controls that are in the update panel
}

The problem:

From the main page, when I click on the LinkButton1 The control load correctly and everything is fine. But When I click on the checkbox of the control, it does not update the inner update panel like i want it to. The control just disappear from the main page.

Any help is greatly appreciated. Thanks in advance and I hope I was clear

You need to create the user control in the page onload event and make it hidden. Recommend you create the control there and use the link button to make it visible and set other properties.

   protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        //-- Create your controls here
    }

Try my code behide

Default.cs

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["isLoad"] == null) return;
        if (Session["isLoad"].ToString() == "1")
        {
            PlaceHolder1.Controls.Clear();
            Control CC = LoadControl("MyControl.ascx");
            PlaceHolder1.Controls.Add(CC);
        }
    }

    protected void Link1_Click(object sender, EventArgs e)
    {
        Session["isLoad"] = "1";
        Response.Redirect(Request.RawUrl);
    }
}

MyControl.cs

public partial class MyControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        //Do the update on the controls that are in the update panel
        if(CheckBox1.Checked)
            lblMessage.Text = "Hello";
        else
            lblMessage.Text = "Goodbye";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM