使用Button从GridView获取Id

时间:2016-09-07 18:12:44

标签: asp.net gridview

我有一个网格我喜欢这个:

<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>

如您所见,我希望在此行中获得Id的值:

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

但是我不知道这有什么问题,我试着用这种方法来解决它:

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

但我刚收到"<%Eval('Id') %>"而不是我的身份证MyID.Value

任何帮助都非常感谢。此致

2 个答案:

答案 0 :(得分:1)

你这样使用它:

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

看起来你正在使用常规按钮OnClick,你需要删除它并使用CommandName中的gvIndex_RowCommand

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

答案 1 :(得分:0)

首先缺少Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons); mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

#

在Gridview代码

  

添加DataKeyNames =&#34; Id&#34;

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

然后点击LinkBut​​ton

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