0
votes

I am binding the data to GridView control. One of the last column has the Image URL link.

     <asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" Width="100%" OnRowCommand="gvccase_RowCommand" RowStyle-CssClass="rowHover">……..


<asp:TemplateField>
   <ItemTemplate>
   <asp:ImageButton ID="lnbEdit" AlternateText="edit" ImageUrl="~/images/edit_icon.gif"
                                                runat="server" CommandName="cmdEdit" />
                                        </ItemTemplate>
                                        <HeaderTemplate>
                                            Edit
                                        </HeaderTemplate>
                                    </asp:TemplateField>

When the user clicks on the last column the OnRowCommnd event(protected void gvccase_RowCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e)) is called.

How to read which row the user clicked? Column one has Case#. I would like to read the Case # in the “OnRowCommand”. How to do it?

OR All I want to accomplish is.... when the user clicks on a particular row I want to take them to the details page.

Thank you, Smith

3

3 Answers

0
votes

from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx:

  void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

  }
0
votes

Why not do something like:

<asp:GridView runat="server" ID="gvMajor" EmptyDataText="Your search terms did not match any records">
    <Columns>
        <asp:BoundField DataField="MajorCode" HeaderText="Major Code" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="lbModify" runat="server" CommandArgument='<%# Eval("MajorId") %>' OnClick="lbModify_Click" ImageUrl="~/Modify.png"   />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Then in code-behind:

protected void lbModify_Click(object sender, EventArgs e)
{
    var editLink = ((ImageButton)sender);
    //editLink.CommandArgument == MajorId
}

just make sure that your binding the gridview to the correct datasource like so:

    DataTable dt = new DataTable();
    dt.Columns.Add("MajorId");
    dt.Columns.Add("MajorCode");

    //start loop here if needed
    DataRow dr = dt.NewRow();

    dr["MajorId"] = 1;
    dr["MajorCode"] = "MAJOR CODE 1";
    dt.Rows.Add(dr);
    //end loop

    gvMajor.DataSource = dt;
    gvMajor.DataBind();
0
votes

You can try this.

HTML

  <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowCommand="GridView1_RowCommand">
      <Columns>
          <asp:TemplateField>
              <ItemTemplate>
                  <asp:Label ID="lblDatos" Text='<%#Eval("nombre")%>' runat="server" >
                  </asp:Label>
              </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField>
              <ItemTemplate>
                  <asp:ImageButton ID="lnbEdit" AlternateText="edit" CommandArgument='<%#Eval("link")%>'
                                            runat="server" CommandName="cmdEdit" />
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
  </asp:GridView>    

CODE BEHINDE

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
            this.llenar();

    }

    void llenar()
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("nombre");
        dt.Columns.Add("link");
       DataRow dr= dt.NewRow();
       DataRow d1 = dt.NewRow();

       dr["nombre"] = "google";
       d1["nombre"] = "amazon";
       dr["link"]  = "http://google.com";
       d1["link"] = "http://amazon.com";
       dt.Rows.Add(dr);
       dt.Rows.Add(d1);

        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();


    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "cmdEdit")
        {
            Response.Redirect(e.CommandArgument.ToString());

        }
    }
}