简体   繁体   English

通过HtmlTextWriter呈现的动态创建的控件未触发事件

[英]Dynamically created Control rendered via HtmlTextWriter is not fireing events

I have a simple ASP.Net page with a button and a Literal Control on it. 我有一个简单的ASP.Net页面,上面有一个按钮和一个文字控件。 On button click i am generating a new dynamic control (imagebutton) and rendering it via HtmlTextWriter to the Literal. 在单击按钮时,我正在生成一个新的动态控件(图像按钮),并通过HtmlTextWriter将其呈现到Literal。 On control creation i am also adding an onClick Event which is not getting fired. 在创建控件时,我还要添加一个不会触发的onClick事件。 Here is my code: 这是我的代码:

aspx aspx

    <asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" />
    <asp:Literal ID="lit" runat="server"></asp:Literal>

cs cs

public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        ImageButton dynBtn = new ImageButton();
        dynBtn.ID = "1";
        dynBtn.ImageUrl = "http://cdn.mysitemyway.com/etc-mysitemyway/icons/legacy-previews/icons/matte-blue-and-white-square-icons-symbols-shapes/118240-matte-blue-and-white-square-icon-symbols-shapes-power-button.png";
        dynBtn.Click += new ImageClickEventHandler(dynBtn_click);
        lit.Text = RenderControl(dynBtn);
    }

    private void dynBtn_click(object sender, ImageClickEventArgs e)
    {
        Page.Response.Write("hello!");
    }

    private string RenderControl(Control control)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter writer = new HtmlTextWriter(sw);

        control.RenderControl(writer);
        return sb.ToString();
    }
}

The control is getting created without any errors but the event is not fireing. 控件已创建,没有任何错误,但事件未触发。

尝试这个

lit.Controls.Add(dynBtn);

Adding your ImageButton to an ASP.NET container would make your life easier, and would be more in line with the way of doing things in the framework. 将您的ImageButton添加到ASP.NET容器将使您的生活更轻松,并且更符合框架中的工作方式。 You can add your button to different types of Web controls, depending on the type of HTML container that you want. 您可以将按钮添加到不同类型的Web控件,具体取决于所需的HTML容器的类型。

Option 1 - PlaceHolder 选项1-PlaceHolder

A PlaceHolder is not rendered as an HTML element. PlaceHolder不会呈现为HTML元素。 Its output contains only the controls that have been added to it. 其输出仅包含已添加到其中的控件。 Among the 3 container controls presented here, it is the most similar to the Literal control. 在此处介绍的3个容器控件中,它与Literal控件最相似。

<asp:PlaceHolder ID="container" runat="server" />

Option 2 - Panel 选项2-面板

A Panel is rendered as as div element. 面板呈现为div元素。

<asp:Panel ID="container" runat="server" />

Option 3 - Label 选项3-标签

A Label is rendered as a span element. Label呈现为span元素。

<asp:Label ID="container" runat="server" />

Adding the control 添加控件

The following statement can be used in code-behind to add the ImageButton to any of the 3 types of ASP.NET containers mentioned above: 可以在代码隐藏中使用以下语句将ImageButton添加到上述3种类型的ASP.NET容器中的任何一种:

container.Controls.Add(dynBtn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM