Custom GridView with Button - Click Event Not Firing

Discussion in 'ASP .Net' started by ADN, Apr 22, 2008.

  1. ADN

    ADN Guest

    Hi,

    I am currently extending the GridView control and would like to add a
    button to the GridView so that it will automatically render one button
    at the top of the grid. I have a click event for that button, but
    everytime the button is clicked, the page posts back and the click
    event does not fire (probably because the button gets created every
    single time there's a postback). What is the best way for me to create
    this button and then make the click event fire?

    I would like it so that I can drag my custom GridView onto an .aspx
    page and not have to worry about handling any events on that page so
    that it is all handled by my gridview. Is that even possible?

    <code>
    public class MyGridView : GridView
    {
    Button btnMyButton;

    protected override void OnInit(EventArgs e)
    {
    btnMyButton = new Button();
    btnMyButton.ID = "btnMyButton";
    btnMyButton.Text = "Click Here";
    btnMyButton.EnableViewState = true;
    btnMyButton.Click += new
    System.EventHandler(MyGridView_MyButtonClick);
    base.OnInit(e);
    }

    protected override void Render(HtmlTextWriter __writer)
    {
    btnMyButton.RenderControl(__writer);
    base.Render(__writer);
    }


    public void MyGridView_MyButtonClick(object sender, EventArgs
    e)
    {
    //=== Button Click Event code goes here....
    }
    }
    </code>
    ADN, Apr 22, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    829
    Purvi T
    Oct 19, 2004
  2. Ken Cox [Microsoft MVP]

    Button Click event not firing

    Ken Cox [Microsoft MVP], Apr 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,534
    Andrea Williams
    Apr 28, 2004
  3. Amy
    Replies:
    0
    Views:
    974
  4. GauravGupta
    Replies:
    4
    Views:
    2,098
    Shilpa1234
    Jan 19, 2012
  5. Max2006
    Replies:
    2
    Views:
    1,564
    Walter Wang [MSFT]
    Sep 19, 2007
Loading...

Share This Page