views:

365

answers:

5

I put some Image Buttons into my gridview, but I cannot capture the click event. Neither creating a click event, nor creating an OnRowCommand handler in the gridview works.

Clicking the buttons simply postbacks to the current page.

I add my buttons like this:

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string status = DataBinder.Eval(e.Row.DataItem, "visitstatusuid").ToString();

        string visitUID = DataBinder.Eval(e.Row.DataItem, "visituid").ToString();

        Color backColor = Color.White;
        Color foreColor = Color.Black;

        ImageButton b;

        switch (status)
        {
            case "U": // Unallocated
                backColor = ColorTranslator.FromHtml("#B2A1C7");
                b = new ImageButton();
                b.Width = Unit.Pixel(25);
                b.Height = Unit.Pixel(30);
                b.AlternateText = "Book";
                b.ImageUrl = "../../Images/New/booking.gif";
                b.ToolTip = "Booking";
                b.CommandName = "Booking";
                b.CommandArgument = visitUID;
                b.CausesValidation = false;

                e.Row.Cells[(e.Row.Cells.Count - 3)].Controls.Add(b);

etc.

A: 

Unless you are adding an event handler elsewhere you would need to set AutoEventWireup="true" in the page directive of your aspx file.

That being said I prefer explicitly wiring events so rather than use AutoEventWireup add this line to your OnInit method:

gridview1.RowDataBound += this.gridview1_RowDataBound;
Andrew Hare
It is already set to true :(
SLC
I'm afraid this doesn't work either :(
SLC
A: 

You'll need to attach the handler when the button is created:

b.Click += MyButtonClickEventHandler;

Edit:
Instead of creating the button in the OnRowDataBound handler, use OnRowCreated.
This ensures the button is recreated on postbacks.

Example:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) {
    BindData();
  }
}

protected void BindData()
{
  // Do your databinding here.
}

protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
  var b = new ImageButton();
  b.AlternateText = "Click Me!";
  // Etc.

  b.Click += MyButton_Click;
  // Add the button to the column you want.
}

protected void MyButton_Click(object sender, ImageClickEventArgs e)
{
  // Do your thing...
}
Sani Huttunen
Indeed, I tried b.Click += new ImageClickEventHandler(gridviewButtonClick); and breakpointed it, but the breakpoint does not hit when I click the button.
SLC
When clicking the button a postback occurs.Do you recreate the button on postback?
Sani Huttunen
A: 

For your approach using RowDataBound to work, you need to rebind the grid on every page load, and ensure you do it no later than OnLoad in the lifecycle, in order for the click event to be registered in time.

An alternative approach I have had success with is to create a new method for doing the DataGrid button setup, e.g.

void PerformConditionalGridFormatting()
{
    foreach (GridViewRow row in gvCaseList.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
             ... Add your buttons to the cells here
        }
    }
}

Then you call the method every time you perform a manual databind, and also on every postback i.e. in your OnLoad handler do:

if (Page.IsPostBack) PerformConditionalGridFormatting();

The advantage of this approach is that you don't have to databind on every postback, which saves resources.

James McCormack
A: 

create a RowCommand event handler for the gridview and check the command name to see if it's your button triggering it

something to the effect of

void gridview1_RowCommand(object sender, args e)
{

if (e.CommandName == "Booking")
{
// call your desired method here
}

}
lincolnk
A: 

Put the binding event of gird in to not post back

Vandana SIngh