[英]Retrieving gridview linkbutton value.
美好的一天
我有包含鏈接按鈕的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);
}
}
就目前而言,我沒有價值。
您已經有了LinkButton,只需使用Text
屬性
if (e.CommandName == "ProdCode")
{
System.Diagnostics.Debug.WriteLine("This is the value " +
((LinkButton)e.CommandSource).Text);
}
我不確定,但是似乎您將值存儲在LinkButton的文本中。 為什么不使用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);
}
}
當然,您必須復制數據,但我認為它是更可取且更方便的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.