saucer(思归)解决的关于:页面A.aspx 动态载入用户自定义控件a.ascx,但a.ascx中的按钮提交后,不执行按钮的事件,而是直接刷新页面
2008-04-17 15:00 Virus-BeautyCode 阅读(1028) 评论(0) 编辑 收藏 举报1. TestA.ascx:
<%@ Register TagPrefix="uc" TagName="MyControl" Src="A.ascx" %>
<form runat="server">
<uc:MyControl id="mc" runat="server"/>
<hr>
<asp:Button id="btn" runat="server" Text="Refresh" />
</form>
2. a.ascx:
<asp:TextBox id="tb1" runat="server" />
<asp:LinkButton id="LinkButton1" runat="server" Text="Load" OnClick="LinkButton1_Click"/>
<script language="C#" runat="server">
private void LinkButton1_Click(object sender, System.EventArgs e)
{
string s = tb1.Text.Trim();
if (!s.Equals(String.Empty))
LoadUserControl(s);
}
string LoadedControl
{
get
{
object o = ViewState["LoadedControl"];
if (o == null)
return "";
else
return (string)ViewState["LoadedControl"];
}
set
{
ViewState["LoadedControl"] = value;
}
}
int LoadedControlID
{
get
{
object o = ViewState["LoadedControlID"];
if (o == null)
return 0;
else
return (int)ViewState["LoadedControlID"];
}
set
{
ViewState["LoadedControlID"] = value;
}
}
void LoadUserControl(string strPathName)
{
if (strPathName.Equals(String.Empty))
return;
Control parent = Page.FindControl("ContentPane");
if (LoadedControl != strPathName)
{
if (parent.Controls.Count > 0)
parent.Controls.Clear();
LoadedControlID++;
}
LoadedControl = strPathName;
Control c = LoadControl(strPathName);
if (c != null)
{
c.ID = "ID" + LoadedControlID.ToString() ;
parent.Controls.Add(c);
}
}
void Page_Load(Object sender, EventArgs e)
{
if (!LoadedControl.Equals(String.Empty))
LoadUserControl(LoadedControl);
}
</script>
3. b.ascx:
b.ascx: <%=DateTime.Now%>
<asp:Button id="btn" runat="server" Text="Button in B.ascx" OnClick="Button1_Click" />
<script language="C#" runat="server">
private void Button1_Click(object sender, System.EventArgs e)
{
Page.Response.Write("BUtton in B is Clicked at " + DateTime.Now.ToString());
}
</script>
4. c.ascx:
c.ascx: <%=DateTime.Now%>
<asp:Button id="btn" runat="server" Text="Button in C.ascx" OnClick="Button1_Click" />
<script language="C#" runat="server">
private void Button1_Click(object sender, System.EventArgs e)
{
Page.Response.Write("BUtton in C is Clicked at " + DateTime.Now.ToString());
}
</script>