开发者

How can I get inside event_Click from instance of UserControl that created dynamicly from Button?

开发者 https://www.devze.com 2023-03-10 06:00 出处:网络
I have UserControl: <%@ Control Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"wControl1.ascx.cs\" Inherits=\"TestProjectSite.wControl1\" %>

I have 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...
        }

And 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);
        }

The problem is in the event of Button12_Click.

the instance of the control is created when i press the button(Button2). but when i press again in the button of UserControl(wControl) he doesn't go to codeBehind event开发者_开发百科Click.... only if I use the create of the userControl in Page_Load event it will be OK.. but I want it from the event of Button2...

What can I do??

THANKS..


If I understand correctly you are trying to understand why your Button12_Click event is not being called after you create it during Button2's click?

The reason is that when the post back occurs for the Button12 click, the button does not exist in the control heirarchy anymore so the event handler does not get hooked up and therfore will not fire.

You have several options:

  1. Always have button12 on the page but only make it visible when button2 is clicked.
  2. Add Button12 as you are but store off in either viewstate or session that the button as been created and then make sure to recreate the button during the load event.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号