2013-07-04 41 views
0

我有一個帶有ScriptManager控件,一些LinkBut​​ton控件和一個PlaceHolder控件的ASPX頁面。每個LinkBut​​ton將在PlaceHolder控件中添加(OnPageLoad)特定的用戶控件。每個UserControl都有一個ScriptManagerProxy控件和一個UpdatePanel(UpdateMode = Conditional),它包含一些CheckBox(AutoPostback = true)控件和一個GridView。UpdatePanel無法在第一次點擊

問題是,當您單擊一個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); 
    } 
} 

回答

0

嘗試這種

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

CheckBox位於UpdatePanel內部,因此它會自動觸發異步回傳。 –

+0

把複選框放在側面更新面板 –

+0

我把複選框放在更新面板之外,並將其設置爲觸發器,並沒有改變任何東西(不明白爲什麼它會真的)。我把上面所有的代碼放在你可以嘗試的地方。 –

0

事實證明,這個問題與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); 
} 
相關問題