0

I have dynamically created a column within a gridview for a button to appear in each row. And am trying to get an onclick event to work.

 ButtonField test = new ButtonField();     
 test.Text = "Details";
 test.ButtonType = ButtonType.Button;
 test.CommandName = "test";
 GridView1.Columns.Add(test);

My asp basic as everything is dynamically added to the gridview:

<asp:GridView ID="GridView1"  runat="server"> </asp:GridView>

This appends the button(s) fine however I can't seem to find a parameter to add an on-click event on the test button field.

I've tried:

   void viewDetails_Command(Object sender, GridViewRowEventArgs e)
    {
        if (test.CommandName == "test")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(),     "alertMessage", "alert('Event works')", true);
        }
    }

This doesn't run as I assume it's not bound to anything however I cant see where I would bind this event function to? Just using an alert message to test the onclick works!

Any help would be great!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dan6657
  • 117
  • 2
  • 11

1 Answers1

0

You need to implement the RowCommand event.

Markup:

<asp:GridView ID="GridView1"  runat="server" OnRowCommand="GridView1_RowCommand">
</asp:GridView>

Code Beside:

public partial class DynamicGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var test = new ButtonField();
            test.Text = "Details";
            test.ButtonType = ButtonType.Button;
            test.CommandName = "test";
            GridView1.Columns.Add(test);


            GridView1.DataSource = new[] {
                new {Id= 1, Text = "Text 1"  },
                new {Id= 2, Text = "Text 2"  },
            };
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "test")
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Event works')", true);
        }
    }
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • How is this different from yours @dan6657? – hardkoded Sep 28 '17 at 12:36
  • It was just the OnRowCommand="GridView1_RowCommand" and e.Command name I think - Thanks! – dan6657 Sep 28 '17 at 12:52
  • Awesome @dan6657, I'll highlight that in the answer – hardkoded Sep 28 '17 at 12:52
  • Another quick one If I may, if this button is being set in every row in the column with some data in a different column but same row, would I be able to access the same row data with same row button, if you get the what I mean? I was thinking about foreach/for loops however they'd just iterate over the set and print it out based on whatever value x is at in the loop. Edit: I could do something like a rowID and then if button x is in rowID x give me the data in that row? – dan6657 Sep 28 '17 at 14:13
  • @dan6657 check this out https://stackoverflow.com/questions/6503339/get-row-index-on-asp-net-rowcommand-event – hardkoded Sep 28 '17 at 14:21