2

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.

9
  • Does it even hit the Button Click Event? Commented Apr 8, 2014 at 10:40
  • I guess thats not happening. But don't know why based on the code. It seems right. Commented Apr 8, 2014 at 10:43
  • 1
    Are you recreating the button on each postback? Commented Apr 8, 2014 at 10:45
  • The creation part is in, comboBox Change event. So i don't have an idea. Either its getting created again or not? Commented Apr 8, 2014 at 10:49
  • 2
    Does the button disappear on the page after you clicked it? If yes, it is not getting recreated. That should be done for the Click event to work. Commented Apr 8, 2014 at 10:52

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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);

    } 

1 Comment

Thanks for the answer, but I still can't see anything happening. Same effect.??
0

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);

1 Comment

Noting. Pages gets refresh, Neither Response.write nor Redirect
0

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 :)

1 Comment

Your code looks more like my code, but in VB.net. Can you do it in C#. Sorry for too much to ask

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.