【问题标题】:asp.net Update Panel inside a user control not working用户控件内的asp.net更新面板不起作用
【发布时间】:2011-11-29 21:13:13
【问题描述】:

我正在开发一个 asp.net/C# Web 应用程序,

在主页:

我在更新面板中有一个占位符

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

当我点击“LinkBut​​ton1”时,我使用它来动态加载网络用户控件

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

在后面的代码中:

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

在我正在加载的控件中,我放置了一个由控件中的复选框触发的更新面板。

在我的控制中:

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

复选框在更新面板外声明

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

在后面的代码中:

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

问题:

从主页上,当我单击 LinkBut​​ton1 时,控件加载正确,一切正常。 但是当我单击控件的复选框时,它不会像我想要的那样更新内部更新面板。该控件刚刚从主页上消失。

非常感谢任何帮助。 提前感谢,我希望我很清楚

【问题讨论】:

  • 你的 asp:ScriptManager 在哪里?
  • @gillyb:请详细说明。这条评论没用。
  • 我的脚本管理器在主页中。

标签: c# asp.net updatepanel


【解决方案1】:

您需要在页面 onload 事件中创建用户控件并使其隐藏。建议您在那里创建控件并使用链接按钮使其可见并设置其他属性。

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

【讨论】:

    【解决方案2】:

    试试我的隐藏代码

    默认.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";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多