【问题标题】:UpdatePanel not working on first clickUpdatePanel 在第一次单击时不起作用
【发布时间】:2013-07-04 04:56:14
【问题描述】:

我有一个带有 ScriptManager 控件、一些 LinkBut​​ton 控件和一个 PlaceHolder 控件的 ASPX 页面。每个 LinkBut​​ton 都会在 PlaceHolder 控件中添加 (OnPageLoad) 一个特定的用户控件。每个 UserControl 都有一个 ScriptManagerProxy 控件和一个 UpdatePanel (UpdateMode=Conditional),其中包含一些 CheckBox (AutoPostback=true) 控件和一个 GridView。

问题是当你点击一个 CheckBox 控件时,它被选中了,但是什么也没有发生。当您再次单击它时,它会被取消选中,并且会导致整个页面的完整回发。 CheckBox 控件的后续单击按应有的方式异步工作。

用户控制 1:标记

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl1.ascx.cs" Inherits="UserControls_UserControl1" %>


<div>

<asp:ScriptManagerProxy ID="smp1" runat="server" />

<asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
<ContentTemplate>

<h1>CheckBox Test 1</h1>

<asp:Panel ID="pnlOptions" runat="server" Visible="true">
    <asp:Panel ID="pnlCheckTest1" runat="server">
        <asp:CheckBox ID="chkCheckTest1" 
                      AutoPostBack="true"
                      Text="Test 1" 
                      OnCheckedChanged="chkCheckTest1_CheckChanged"
                      runat="server" />
    </asp:Panel>
    <asp:Panel ID="pnlCheckTest2" runat="server">
        <asp:CheckBox ID="chkCheckTest2" 
                      AutoPostBack="true"
                      Text="Test 2" 
                      OnCheckedChanged="chkCheckTest2_CheckChanged"
                      runat="server" />
    </asp:Panel> 
</asp:Panel>

<br /><br />

<div>
<asp:Label ID="lblTestCheck1" runat="server" />
</div>

</ContentTemplate>
</asp:UpdatePanel>

</div>

用户控制 1:代码

public partial class UserControls_UserControl1 : System.Web.UI.UserControl
{

    protected void chkCheckTest1_CheckChanged(object sender, EventArgs e)
    {
        lblTestCheck1.Text = "Tiny";
    }

    protected void chkCheckTest2_CheckChanged(object sender, EventArgs e)
    {
        lblTestCheck1.Text = "Large";
    }
}

用户控制 2:标记

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl2.ascx.cs" Inherits="UserControls_UserControl2" %>


<div>

<asp:ScriptManagerProxy ID="smp1" runat="server" />

<asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
<ContentTemplate>

<h1>CheckBox Test 2</h1>

<asp:Panel ID="pnlOptions" runat="server" Visible="true">
    <asp:Panel ID="pnlCheckTest1" runat="server">
        <asp:CheckBox ID="chkCheckTest1" 
                      AutoPostBack="true"
                      Text="Test 1" 
                      OnCheckedChanged="chkCheckTest1_CheckChanged"
                      runat="server" />
    </asp:Panel>
    <asp:Panel ID="pnlCheckTest2" runat="server">
        <asp:CheckBox ID="chkCheckTest2" 
                      AutoPostBack="true"
                      Text="Test 2" 
                      OnCheckedChanged="chkCheckTest2_CheckChanged"
                      runat="server" />
    </asp:Panel> 
</asp:Panel>

<br /><br />

<div>
<asp:Label ID="lblTestCheck2" runat="server" />
</div>

</ContentTemplate>
</asp:UpdatePanel>

</div>

用户控制 2:代码

public partial class UserControls_UserControl2 : System.Web.UI.UserControl
{

    protected void chkCheckTest1_CheckChanged(object sender, EventArgs e)
    {
        lblTestCheck2.Text = "Small";
    }

    protected void chkCheckTest2_CheckChanged(object sender, EventArgs e)
    {
        lblTestCheck2.Text = "Big";
    }
}

主页:标记

<%@ Page Title=""  Language="C#" AutoEventWireup="true"CodeFile="CheckBoxTest.aspx.cs" Inherits="CheckBoxTest" %>
<html>
<head id="head1" runat="server">
<title>Check Box Test</title>
</head>

<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server" />


<div id="Div1" runat="server">
    <asp:LinkButton ID="lbCheckTest1" runat="server" Text="Check Test 1" 
            onclick="lbCheckTest1_Click" />
    <br />
    <asp:LinkButton ID="lbCheckTest2" runat="server" Text="Check Test 2" 
            onclick="lbCheckTest2_Click" />
</div>

<br /><br /><br /><br />

<div id="Div2" runat="server">
    <asp:PlaceHolder 
        ID="phTable"
        runat="server" />        
</div>
</form>
</body>
</html>

主页:代码

public partial class CheckBoxTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            try
            {
                Control ctrl = LoadControl(Session["CurrentControl"] as String);
                phTable.Controls.Add(ctrl);
            }
            catch
            {
                // ...
            }
        }
    }

    protected void lbCheckTest1_Click(object sender, EventArgs e)
    {
        Session["CurrentControl"] = "~/UserControls/UserControl1.ascx";
        Control ctrl = LoadControl(Session["CurrentControl"] as String);
        phTable.Controls.Clear();
        phTable.Controls.Add(ctrl);
    }

    protected void lbCheckTest2_Click(object sender, EventArgs e)
    {
        Session["CurrentControl"] = "~/UserControls/UserControl2.ascx";
        Control ctrl = LoadControl(Session["CurrentControl"] as String);
        phTable.Controls.Clear();
        phTable.Controls.Add(ctrl);
    }
}

【问题讨论】:

    标签: asp.net asp.net-ajax


    【解决方案1】:

    事实证明,该问题与 UpdatePanel 无关。问题在于动态加载的用户控件。

    ViewState 是动态加载控件的问题。最好在执行生命周期的 OnPageInit 事件期间加载它们。此外,为确保 ViewState 正常工作,请为控件分配一个有效 ID;

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                try
                {
                    phTable.Controls.Clear();
                    Control ctrl = LoadControl(Session["CurrentControl"] as String);
                    ctrl.ID = Session["CurrentControlID"].ToString();
                    phTable.Controls.Add(ctrl);
                }
                catch
                {
                    // Handle Error...
                }
            }
        }
    
    
    protected void lbCheckTest1_Click(object sender, EventArgs e)
    {
        Session["CurrentControl"] = "~/UserControls/UserControl1.ascx";
        Session["CurrentControlID"] = "UserControl1";        
        Control ctrl = LoadControl(Session["CurrentControl"] as String);
        ctrl.ID = Session["CurrentControlID"].ToString();
        phTable.Controls.Clear();
        phTable.Controls.Add(ctrl);
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      <asp:UpdatePanel runat="server"><%--your updatepanel--%>
          <ContentTemplate>
          <%--something that you want to update on checkbox check change event--%>
          </ContentTemplate>
          <Triggers>
              <asp:AsyncPostBackTrigger ControlID="your_checkbox_control_id" EventName="CheckedChanged" />
              <%--add AsyncPostBackTrigger for checkbox event --%>
          </Triggers>
      </asp:UpdatePanel>
      

      【讨论】:

      • CheckBox 位于 UpdatePanel 内,因此会自动触发异步回发。
      • 我将复选框放在更新面板之外并将其设置为触发器,但这并没有改变任何东西(不明白为什么会这样)。我把上面所有的代码都放上来了,大家可以试试。
      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      相关资源
      最近更新 更多