arrest warrant
arrest warrant

Reputation: 354

click on button in asp.net

I am creating a button dynamically and place it in a placeholder as below

<asp:Button ID="generateTableSchema" runat="server" Text="Generate Table" OnClick="generate_Click" />

 protected void generate_Click(object sender, EventArgs e)
    {
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.OnClick = hello();
PlaceHolder1.Controls.Add(button);
}

but onclick event is not firing.

this is the error i am getting

System.Web.UI.WebControls.Button.OnClick(System.EventArgs)' is inaccessible due to its protection level

hello is as below...

public void hello()
    {
        Label1.Text = "heellllllllllo";
    }

What's wrong here????

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        }
        else
        {
            button.Click += ButtonClick;
        }
    }

@Daren u mean like this...

Upvotes: 0

Views: 1484

Answers (6)

arrest warrant
arrest warrant

Reputation: 354

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        }
        else if(Page.IsPostBack && Label11.Text=="yes")
        {
            Button button = new Button();
            button.Text = "Generate Table";
            button.ID = "generateTable";
            button.Click += ButtonClick;
            PlaceHolder1.Controls.Add(button);
        }
    }

setting

        Label11.Text = "yes";

in generate_click.

protected void ButtonClick(object sender, EventArgs e)
    {
        Label1.Text = "heeellllllo";
    }

Upvotes: 0

Darren Wainwright
Darren Wainwright

Reputation: 30727

Because you are adding the button programmatic ally you have to add the event handler.

So this would work..

EDIT Wrapped the button INSIDE Page_Load

protected void Page_Load(object sender, EventArgs e)
{
 Button button = new Button();
 button.Text = "Generate Table";
 button.ID = "generateTable";
 button.Click += hello;    /// THIS is the handler
 PlaceHolder1.Controls.Add(button);
}

ButtonClick would be the name of your method.

    protected void hello(Object sender, EventArgs e)
    {
     // ...
    }

Also, as you're generating this at runtime you need to makesure this gets called on postbacks too.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460058

The event is called Click. You need to add the event handler with the correct signature:

button.Click += new EventHandler(hello);

and the signature is:

protected void hello(Object sender, EventArgs e)
{
    // ...
}

How to: Add an Event Handler Using Code

Note that you need to recreate dynamical controls on every postback.

Upvotes: 1

Sean Airey
Sean Airey

Reputation: 6372

Change button.OnClick = hello(); to:

button.Click += new EventHandler(hello);

And change the definition for hello() to:

protected void hello(object sender, EventArgs e)
{
    Label1.Text = "heeellllllo";
}

Upvotes: 1

Amiram Korach
Amiram Korach

Reputation: 13286

The OnClick is a protected method. You should use the event Click.

button.Click += new EventHandler(Click);

public void hello(object sender, EventArgs e)
{
    Label1.Text = "heellllllllllo";
}

By the way, make sure you create and add the control in every postback, otherwise the event won't work.

Upvotes: 1

Ren&#233; Wolferink
Ren&#233; Wolferink

Reputation: 3548

You assigning the result of executing hello().

Try assigning:

button.OnClick = hello;

-- edit--

apparantly, that does not clarify your error.

Add the handler to the event handler in stead:

button.Click += hello;

Upvotes: 0

Related Questions