我有一个ASP.Net页面(Default.aspx),它将UserControl(ucontrol.ascx)动态加载到UpdatePanel中。通过使用UpdatePanel,我们相信我们能够阻止整个页面被回发。
我的问题是,在UserControl中,我们有一些表单控件,比如输入和按钮;加载UserControl后,单击UserControl内的任何按钮将导致UpdatePanel被消隐。在调查期间,我发现,从未到达UserControl的代码隐藏中的Button1_Click()内的断点。请参阅下面的代码,这是对 [参考] 1的引用。这是一项紧迫的任务,请帮助。
谢谢! 威廉
Default.aspx的:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<span>This is hosting page</span><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add UserControl" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="PartHolder" runat="server">
</asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
loadparts();
}
private void loadparts(){
Control formpart = LoadControl("ucontrol.ascx");
formpart.ID = "formpart";
PartHolder.Controls.Add(formpart);
}
}
ucontrol.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucontrol.ascx.cs" Inherits="ucontrol" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
This is Web User Control in UpdatePanel<br />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
ucontrol.ascx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ucontrol : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
TextBox1.Text = "";
TextBox1.Focus();
}
}
答案 0 :(得分:2)
这里发生了什么:
loadparts()
且用户控件未加载到控制树中。您需要做的是确保将用户控件加载到页面的控制树中,即使您单击用户控件的按钮也是如此。这就是安德鲁建议你将loadparts()放入Page_Load事件的原因。我可以看到它不适合您的网页流量,因此您必须找到另一种方法在正确的时间加载它。
答案 1 :(得分:0)
尝试将loadparts()方法放在
中protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{
loadparts();
}
}
答案 2 :(得分:0)
这对我有用:
protected void Page_Load(object sender, EventArgs e)
{
if(isPostBack)
{
loadparts();
}
}