简体   繁体   中英

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:

    {
        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"

Tried googling the error, it is regarding 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."

This is my aspx code:

<%@ 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...)

Please, could someone tell me how to make this working...

EDIT: sadrzaj is a div that contains all this elemnts I'm adding. I'm adding them in a function that is being called from 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?

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" ). You have to add the ID to the control to add.

In the first piece of code you have created onclick client side (so javascript) using

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.

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 +=

I suggest also to not to use this.Controls directly: it's referred to page. You can add a PlaceHolder control or a Panel control in the Content tag and add Controls to it. If you use this and not a Panel or PlaceHolder , try to use this.Form.Controls.Add

You can referer to:

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

https://support.microsoft.com/en-us/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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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