John
John

Reputation: 1

Get Id from GridView using Button

I have a Grid Where I have Id like this:

<div class="row ">
            <asp:GridView ID="gvIndex" CssClass="table table-bordered table-responsive"  runat="server" AllowPaging="True" PageSize="10" AutoGenerateColumns="False" AutoGenerateEditButton="False" OnRowCommand="gvIndex_RowCommand">

                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Número" HtmlEncode="false"> //This is my Id Field
                        <FooterStyle Width="200px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Nombre" HeaderText="Nombre" HtmlEncode="false">
                        <FooterStyle Width="400px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Direccion" HeaderText="Dirección" HtmlEncode="false">
                        <FooterStyle Width="400px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Puesto" HeaderText="Puesto" HtmlEncode="false">
                        <FooterStyle Width="400px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Fecha" HeaderText="Fecha" HtmlEncode="false">
                        <FooterStyle Width="175px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="CorreoElectronico" HeaderText="Correro Electrónico" HtmlEncode="false">
                        <FooterStyle Width="350px" />
                    </asp:BoundField>

                    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Edición">
                        <ItemTemplate>

                            <asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<% Eval('Id') %>" CommandName="SelectRow"  CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton> //There I want to get value of Id
                            <%--<asp:HyperLink ID="EditarIngreso" BorderStyle="None" OnClick="EditarIngreso_Click" Text="Editar"  runat="server">HyperLink</asp:HyperLink>--%>
                            <%--<a id="EditarIngreso" runat="server" style="text-decoration: underline; border: none">Editar</a>--%>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

            </asp:GridView>
        </div>

As you can see, I want to get value of Id in this line:

 <asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<% Eval('Id') %>" CommandName="SelectRow"  CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton> 

But I don't know what is wrong with this, I try to get it with this method:

protected void gvIndex_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            MyID.Value = e.CommandArgument.ToString();
        }

But I just receiving "<%Eval('Id') %>" instead my Id into MyID.Value

Any Help is very appreciate. Regards

Upvotes: 0

Views: 2928

Answers (2)

Manish Goswami
Manish Goswami

Reputation: 865

First missing #

<asp:LinkButton ID="EditarIngreso" OnClick="EditarIngreso_Click" CommandArgument="<%# Eval('Id') %>" CommandName="SelectRow"  CssClass="btn btn-primary" runat="server">Editar</asp:LinkButton> 

In Gridview Tag

add DataKeyNames= "Id"

<asp:Gridview id="gvIndex"  DataKeyNames= "Id" />

Then OnClick of LinkButton

protected void EditarIngreso_Click(object sender,EventArgs e)
{
LinkButton btn = sender as LinkButton ;
GridViewRow row = btn.NamingContainer as GridViewRow;

string pk = gvIndex.DataKeys[row.RowIndex].Values[0].ToString();
  // here PK is your ID 
  // Convert it to Int32
}

Upvotes: 0

VDWWD
VDWWD

Reputation: 35544

You use it like this:

<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("Id") %>' runat="server" Text="Button" CommandName="SelectRow" />

And it looks you are using a regular button OnClick also, you need to remove that and use the CommandName in gvIndex_RowCommand

    protected void gvIndex_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "SelectRow")
        {
            MyID.Value = e.CommandArgument.ToString();
        }
    }

Upvotes: 1

Related Questions