我有UserControl:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="wControl1.ascx.cs" Inherits="TestProjectSite.wControl1" %>
<asp:Button ID="Button12" runat="server" Text="Button12" onclick="Button12_Click" OnClientClick="true" />
C#:
public string Name { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Button12.Text = Name;
}
protected void Button12_Click(object sender, EventArgs e)
{
//...
//The problem is here... it doesn't get inside...
}
Page .ASPX:
<p>
<asp:TextBox ID="TextBox1" Width="500" Height="34" runat="server"
Font-Size="22px"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Height="32" Text="חפש..."
OnClick="Button2_Click" Font-Size="20px" />
</p>
<P>
<asp:PlaceHolder ID="plhItems" runat="server" >
</P>
C#:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Func(TextBox1.Text);
}
public void Func(string text)
{
wControl1 eControl = (wControl1)Page.LoadControl("wControl1.ascx");
eControl.Name = text;
plhItems.Controls.Add(eControl);
}
问题出在Button12_Click。
按下按钮(Button2)时会创建控件实例。 但是当我再次按下UserControl(wControl)的按钮时,他不会转到codeBehind eventClick ....只有当我在Page_Load事件中使用userControl的创建它才会好...但我希望它来自事件Button2 ......
我该怎么办?
感谢..
答案 0 :(得分:1)
如果我理解正确,您试图理解为什么在Button2点击期间创建Button12_Click事件后没有调用它?
原因是当Button12单击发回时,控件层次结构中的按钮不再存在,因此事件处理程序不会被连接起来,因此不会触发。
您有几种选择: