简体   繁体   中英

Button not firing event on ASP.NET control Programmatically

I'm creating a Button on a ComboxChanged Event,

    Button btn = new Button();
    btn.ID = "btnNum1";
    btn.Text = "Edit";
    btn.Click += btnTest_Click;        
    pnl.Controls.Add(btn);

The event code is as followed

 public void btnTest_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    Response.Write(btn.ID);        
    Response.Redirect("Page2.aspx");
}

I have tried, both Response.Write and Response.Redirect . None of them, works. Same page gets refreshed. Does any one has any idea.

Button has to be created Dynamically, hence I can't try the Page_Init

I have also tried the CommandArgument event that also didn't work. Any idea.

As Mt. Schneiders has mentioned in his comments under your question - if you dynamically add a control to a page, you must re-add it on the post-back. Just because you've added it as part of the event handler, does not mean that ASP.NET automatically creates the control on the post-back.

You need to store in the page the fact that the control was created - my personal preference would be to put the code in it's own function and set a ViewState value...

private void CreateButton()
{
  Button btn = new Button();
  btn.ID = "btnNum1";
  btn.Text = "Edit";
  btn.Click += btnTest_Click;        
  pnl.Controls.Add(btn);
  ViewState["buttonAdded"] = true;
}

And call the function from your ComboxChanged event.

Then, on the Page_Load , you need to check to see if the button was added previously...

protected void Page_Load(object sender, EventArgs e)
{
  if(Page.IsPostBack && ViewState["buttonAdded"] != null)
  {
    CreateButton()
  }
}

(Note, I've stated Page_Load rather than Page_Init because the ViewState is not created at the Page_Init stage of the ASP.NET page life cycle )

when u added ur dynamic event handler it will store in particular control view state so when the page post back happened it will lose that event handler so u can implement load view state method which will come with page events code should be like

protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);

    } 

Try this btn.Click += new EventHandler(btnTest_Click); .

Button btn = new Button();
btn.ID = "btnNum1";
btn.Text = "Edit";
btn.Click += new EventHandler(btnTest_Click);     
pnl.Controls.Add(btn);

Assume this is the code that create the button :

 If ddl.SelectedIndex > 0 Then
        Dim b As New Button
        b.ID = "mybutton"
        b.Text = "i'm a button"
        panel.Controls.Add(b)
        AddHandler b.Click, AddressOf Button_Click

    End If

Note the AddHandler ok it is exactly what you're looking for then

  Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim b As Button = CType(sender, Button)
    Dim l As Label = CType(Me.FindControl("testlabel"), Label)
    l.Text = b.Text & " my id is " & b.ID.ToString

End Sub

that's all :)

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