简体   繁体   English

将事件处理程序添加到动态创建的按钮

[英]adding event handler to a dynamically created button

I have created a button in c# codebehind and am trying to add event listener to it, but the problem is if I use: 我已经在c#代码隐藏中创建了一个按钮,并尝试向其添加事件侦听器,但是问题是如果使用:

    {
        HtmlGenericControl dodaj = new HtmlGenericControl("button");
        dodaj.InnerText = "something";
        dodaj.Attributes.Add("runat", "server");
        dodaj.Attributes.Add("type", "submit");
        dodaj.Attributes.Add("onclick","addKosarica");
        newDivInfo.Controls.Add(dodaj);
        sadrzaj.Controls.Add(newDivInfo);
        this.Controls.Add(sadrzaj);
    }

    protected void addKosarica(object sender, EventArgs e)
    {
        Response.Redirect("www.google.com");  //just to see if it triggers
    }

I get: 我得到:

"Uncaught ReferenceError: addKosarica is not defined at HTMLButtonElement.onclick" “未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义addKosarica”

Tried googling the error, it is regarding javascript... 尝试谷歌搜索错误,这是关于JavaScript的...

Then after googling some more, I tried: 然后在进一步搜索之后,我尝试了:

    {
        Button dodaj = new Button;
        dodaj.Text = "something";
        dodaj.Click += new EventHandler("addKosarica");
        newDivInfo.Controls.Add(dodaj);
        sadrzaj.Controls.Add(newDivInfo);
        this.Controls.Add(sadrzaj);
    }

    protected void addKosarica(object sender, EventArgs e)
    {
        Response.Redirect("www.google.com");//just to see if it triggers
    }

and i get 我得到

"Control 'ctl07' of type 'Button' must be placed inside a form tag with runat=server." “必须将类型为“按钮”的控件“ ctl07”放置在带有runat = server的表单标记中。”

This is my aspx code: 这是我的aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="11001.aspx.cs" Inherits="_11001" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

</asp:Content>

(its not much because i add everything dynamically and everything works except button onclick handler...) (不多是因为我动态地添加了所有内容,并且除了按钮onclick处理程序之外,其他所有内容都可以正常工作...)

Please, could someone tell me how to make this working... 拜托,有人能告诉我如何使它工作吗?

EDIT: sadrzaj is a div that contains all this elemnts I'm adding. 编辑:sadrzaj是一个div,其中包含我要添加的所有这些元素。 I'm adding them in a function that is being called from Page_Load() 我将它们添加到正在从Page_Load()调用的函数中

Sorry but you are very confused between a button server side and a button client side. 抱歉,您对按钮服务器端和按钮客户端端感到非常困惑。

First question is: why don't you add the button in aspx? 第一个问题是:为什么不在aspx中添加按钮?

Do you need to loop on a collection and create many similar button? 您是否需要循环访问集合并创建许多类似的按钮? You can use Repeater. 您可以使用中继器。

Then, reading your code... You have a master page (I hope containing a tag form runat="server" ). 然后,阅读代码...您拥有一个母版页(我希望包含一个标签形式runat="server" )。 You have to add the ID to the control to add. 您必须将ID添加到要添加的控件中。

In the first piece of code you have created onclick client side (so javascript) using 在第一段代码中,您使用以下命令创建了onclick客户端(因此javascript)

dodaj.Attributes.Add("onclick","addKosarica");

So you can remove: 因此,您可以删除:

protected void addKosarica(object sender, EventArgs e)
{
   Response.Redirect("www.google.com");//just to see if it triggers
}

and add a function addKosarica() in aspx file. 并在aspx文件中添加函数addKosarica()。

In the second piece of code the correct code is: 在第二段代码中,正确的代码是:

dodaj.Click += myButton_Click;

and the right event is 正确的事件是

protected void myButton_Click(object sender, EventArgs e)

and you can create automatically in Visual Studio clicking tab twice after += 并且您可以在Visual Studio中自动创建标签,在+ =之后单击两次标签

I suggest also to not to use this.Controls directly: it's referred to page. 我还建议不要直接使用this.Controls:它是指页面。 You can add a PlaceHolder control or a Panel control in the Content tag and add Controls to it. 您可以在Content标记中添加PlaceHolder控件或Panel控件,然后向其添加控件。 If you use this and not a Panel or PlaceHolder , try to use this.Form.Controls.Add 如果您使用this而不是PanelPlaceHolder ,请尝试使用this.Form.Controls.Add

You can referer to: 您可以参考:

https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx https://msdn.microsoft.com/zh-CN/library/kyt0fzt1.aspx

https://support.microsoft.com/en-us/help/317794/how-to-dynamically-create-controls-in-asp-net-by-using-visual-c--net https://support.microsoft.com/zh-cn/help/317794/how-to-dynamically-create-controls-in-asp-net-by-using-visual-c--net

protected void Page_Load(object sender, EventArgs e)
        {
            Button dodaj = new Button();
            dodaj.Text = "Click Me!";
        dodaj.Attributes.Add("runat", "server");
        dodaj.Attributes.Add("type", "submit");
        dodaj.Attributes.Add("onclick", "addKosarica");
        newDivInfo.Controls.Add(dodaj);
        dodaj.Click += Dodaj_Click1;
    }

    private void Dodaj_Click1(object sender, EventArgs e)
    {
        Response.Write("Ok");
    }

aspx code aspx代码

<div runat="server" id="newDivInfo">

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

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