c#
  • asp.net
  • gridview
  • 2013-02-04 47 views 2 likes 
    2

    美好的一天檢索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); 
        } 
    
    
    
    } 
    

    因爲目前我沒有得到價值。

    回答

    3

    你已經擁有LinkBut​​ton的,只需使用Text財產

    if (e.CommandName == "ProdCode") 
    { 
        System.Diagnostics.Debug.WriteLine("This is the value " + 
                 ((LinkButton)e.CommandSource).Text); 
    } 
    
    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); 
        } 
    } 
    

    你要複製的數據變化圖,但我認爲這是最好更方便。

    相關問題