检索gridview linkbutton值。

时间:2013-02-04 11:52:57

标签: c# asp.net gridview

美好的一天

我的gridview包含一个链接按钮。如何在单击链接按钮时检索链接按钮的值(文本)。

这就是我所做的。

<asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lnkProdCode" CommandName="ProdCode" runat="server" Width="115%" Text='<%# DataBinder.Eval(Container.DataItem,"CODE") %>' style="color:Black;font-size:smaller;"></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>

protected void gridReport_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ProdCode")
    {   
        GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
        string valueLink = selectedRow.Cells[0].Text;
        System.Diagnostics.Debug.WriteLine("This is the value " + valueLink);
    }



}

因为目前我没有得到这个价值。

2 个答案:

答案 0 :(得分:3)

您已拥有LinkBut​​ton,只需使用Text属性

if (e.CommandName == "ProdCode")
{   
    System.Diagnostics.Debug.WriteLine("This is the value " + 
                                       ((LinkButton)e.CommandSource).Text);
}

答案 1 :(得分:3)

我不确定,但似乎您将自己的价值存储在LinkBut​​ton的文本中。为什么不使用CommandArgument属性?

<asp:TemplateField>
   <ItemTemplate>
            <asp:LinkButton ID="lnkProdCode" CommandName="ProdCode" runat="server" Width="115%" Text='<%# DataBinder.Eval(Container.DataItem,"CODE") %>' CommandArgument='<%# DataBinder.Eval(Container.DataItem,"CODE") %>' style="color:Black;font-size:smaller;"></asp:LinkButton>
   </ItemTemplate>
</asp:TemplateField>

然后你就可以做到这一点:

protected void gridReport_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ProdCode")
    {   
        System.Diagnostics.Debug.WriteLine("This is the value " + e.CommandArgument);
    }
}

对于cource,您必须复制数据,但我认为它更好,更方便。

相关问题