<asp:GridView ID="GridView1" runat="server" AllowPaging="True" >
                                 <Columns>
                                   <asp:TemplateField>
                                    <ItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                   <asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("name") %>' OnClick="LinkButton2_Click"></asp:LinkButton>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                   </asp:TemplateField>
                                 </Columns>
                        </asp:GridView>我想获取linkButton的text值!
谢谢各位大哥大姐各位高手!小弟在线等

解决方案 »

  1.   

    ((Button)grdPersonList.Rows[0].Cells[0].FindControl("LinkButton2")).Text
      

  2.   

    ((LinkButton)e.Rows.FindControl("LinkButton2")).Text 
      

  3.   

    ((LinkButton)e.Rows.FindControl("LinkButton2")).Text 
      

  4.   

    ((LinkButton)e.Rows.FindControl("LinkButton2")).Text 
      

  5.   

    问题描述的不清楚,你想要在什么情况下获取LinkButton控件的Text属性值呢?
    单击LinkButton的时候?
      

  6.   

    利用FindControl()方法找控件啊!在网上查!
      

  7.   


    就是点击后获得他的text值
      

  8.   

    那就好办了!下面的代码是我测试时弄的一个。
    GridView控件的设置信息如下,关键点是哪个LinkButton控件的CommandName属性的设置。<asp:GridView ID="ProductsGridView" runat="server"
        AllowPaging="True" AutoGenerateColumns="False"
        DataSourceID="ProductsSqlDataSource" 
        onrowcommand="ProductsGridView_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="Product Name">
                <ItemTemplate>
                    <asp:LinkButton ID="ProductNameLinkButton" runat="server"
                        Text='<%# Eval("ProductName") %>' 
                        CommandName="ReadProductName"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>有没有发现我的这个和你那个代码的区别吗?除了设置了LinkButton.CommandName属性以外,我并没有为LinkButton的Click事件绑定处理程序。
    其实,你那种绑定应该也没什么效果吧!那个LinkButton的Click事件最后还是会转发给GridView的。
    所以,你那个处理代码应该是不会被执行的。
    我的做法是,为GridView编写RowCommand事件的处理程序。protected void ProductsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ReadProductName")
        {
            if (e.CommandSource != null)
            {
                LinkButton ProductNameLinkButton = (LinkButton)e.CommandSource;
                this.Response.Write(ProductNameLinkButton.Text);
            }
        }
    }通过e.CommandName可以判断是否就是LinkButton的单击事件,这里的CommandName其实就是前面设置的那个CommandName。
    如果是LinkButton的话,也不需要使用FindControl()来查找控件了。
    直接可以通过e.CommandSource获取引发事件的控件了。将其转换成LinkButton,就可以直接获取绑定的Text属性值了。
      

  9.   

    第一种方法是:Button btn_name =grdPersonList.Rows[0].Cells[0].FindControl("LinkButton2")).Text as Button ;
    对于这种对于这种嵌入在控件里的控件,,千万不能用直接用ID ‘LinkButton2’去寻找那个控件,因为他在服务器上的ID和他在客户端的ID是不同。第二种方法:对于这种嵌入在控件里的控件, 它有两个很重要的参数:commandName 和 commandArgs 。你可以用这种方式。
      

  10.   

    ((Button)grdPersonList.Rows[0].Cells[0].FindControl("LinkButton2")).Text