2010-08-18 91 views
0

我有一個GridView,它的templatefield中包含一個linkbutton。我想知道的是,當我點擊鏈接按鈕時,如何將linkbutton的文本轉換爲名爲「name」的字符串?Gridview和LinkBut​​ton

+3

什麼代碼,你嘗試過這麼遠嗎? – Rob 2010-08-18 08:31:20

回答

6

的Click事件提供事件處理程序:

<asp:GridView id="myGrid" runat="server"> 
<columns> 
    <asp:templatefield> 
     <itemtemplate> 
     <asp:LinkButton id="MyButton" 
      Text="SuperText" 
      OnClick="MyButton_Click" 
      runat="server"/> 
     </itemtemplate> 
    </asp:templatefield> 
</columns> 
</asp:GridView> 

在事件處理程序中使用下面的代碼:

protected void MyButton_Click(object sender, EventArgs e) 
    { 
     LinkButton btn = sender as LinkButton; 
     if(btn != null) 
      string name = btn.Text; // SuperText 

    } 
+0

非常感謝Pavel。這是我正在尋找的。 :) – pRAVeEN 2010-08-20 05:28:48

+0

請標記答案是否正確,如果它是你正在尋找的 - 所以其他人將能夠看到它。 :) – 2010-08-20 06:14:46