【问题标题】:How can I get inside event_Click from instance of UserControl that created dynamicly from Button?如何从 Button 动态创建的用户控件实例中获取内部事件 Click?
【发布时间】:2011-06-02 21:44:10
【问题描述】:

我有 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...
        }

页面.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...

我能做什么??

谢谢..

【问题讨论】:

    标签: c# asp.net events user-controls onclick


    【解决方案1】:

    如果我理解正确,您想了解为什么在 Button2 的单击期间创建 Button12_Click 事件后没有调用它?

    原因是当 Button12 单击发生回发时,该按钮不再存在于控件层次结构中,因此事件处理程序不会被连接,因此不会触发。

    您有多种选择:

    1. 页面上始终显示 button12,但仅在单击 button2 时才可见。
    2. 按原样添加 Button12,但在视图状态或会话中存储按钮已创建,然后确保在加载事件期间重新创建按钮。

    【讨论】:

    • 好的,我认为数字 2 更好...但是还有更多选择吗?保存在视图状态中好吗?
    • 我还不能解决这个问题......也许你有一些示例代码给我看(通过这里提供的代码)?
    猜你喜欢
    • 2011-04-05
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多