C# 从“删除”按钮获取gridview中的链接按钮文本

C# 从“删除”按钮获取gridview中的链接按钮文本,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个具有分页功能的gridview 下面的代码是我的gridview的aspx代码 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" Width="768px" CellPadding="4" ForeColor="#333333" OnRowDeleting="OnRowDeleting" AllowPaging="True" PageSize="20"

我有一个具有分页功能的gridview

下面的代码是我的gridview的aspx代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  GridLines="None" Width="768px" CellPadding="4" ForeColor="#333333" OnRowDeleting="OnRowDeleting" AllowPaging="True" PageSize="20" OnPageIndexChanging="gridView_PageIndexChanging" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
             <asp:TemplateField HeaderText="SAP No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%"> 
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"
                        Text='<%#Eval("SAPNO") %>' runat="server"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="PARTNO" HeaderText="Part No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">  
             </asp:BoundField>
            <asp:BoundField DataField="PARTDESC" HeaderText="Description" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%"> 
             </asp:BoundField>
             <asp:BoundField DataField="MINQTY" HeaderText="Min Qty" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">  
             </asp:BoundField>
             <asp:BoundField DataField="QOH" HeaderText="QOH" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">  
             </asp:BoundField>
              <asp:BoundField DataField="CATEGORY" HeaderText="Category" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">  
             </asp:BoundField>
              <asp:BoundField DataField="EQUIPMENT" HeaderText="Equipment" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">  
             </asp:BoundField>
             <asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.JPG" ControlStyle-Height="20px" ControlStyle-Width="50px" HeaderText="Delete">
    <HeaderStyle Width="10%" HorizontalAlign="Left" /></asp:CommandField>
        </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />

    <PagerSettings  Mode="NextPreviousFirstLast" FirstPageText="<--" PreviousPageText="<" NextPageText=">"  LastPageText="-->" />
     <PagerStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
我尝试显示sapNo1的所有值,它将我置为空白,但对于partNo,它将在gridview中显示数据

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  GridLines="None" Width="768px" CellPadding="4" ForeColor="#333333" OnRowDeleting="OnRowDeleting" AllowPaging="True" PageSize="20" OnPageIndexChanging="gridView_PageIndexChanging" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
             <asp:TemplateField HeaderText="SAP No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%"> 
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"
                        Text='<%#Eval("SAPNO") %>' runat="server"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="PARTNO" HeaderText="Part No" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="10%">  
             </asp:BoundField>
            <asp:BoundField DataField="PARTDESC" HeaderText="Description" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%"> 
             </asp:BoundField>
             <asp:BoundField DataField="MINQTY" HeaderText="Min Qty" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">  
             </asp:BoundField>
             <asp:BoundField DataField="QOH" HeaderText="QOH" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%">  
             </asp:BoundField>
              <asp:BoundField DataField="CATEGORY" HeaderText="Category" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">  
             </asp:BoundField>
              <asp:BoundField DataField="EQUIPMENT" HeaderText="Equipment" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="20%">  
             </asp:BoundField>
             <asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.JPG" ControlStyle-Height="20px" ControlStyle-Width="50px" HeaderText="Delete">
    <HeaderStyle Width="10%" HorizontalAlign="Left" /></asp:CommandField>
        </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />

    <PagerSettings  Mode="NextPreviousFirstLast" FirstPageText="<--" PreviousPageText="<" NextPageText=">"  LastPageText="-->" />
     <PagerStyle HorizontalAlign="Center" BackColor="White" />
</asp:GridView>
有人知道这件事吗?
我真的很感谢你的帮助和评论

因为您的第一列是一个linkbutton,所以按原样检索,然后找到值

string sapNo1 = GridView1.Rows[index].Cells[0].Text; // wrong code to retrieve data from linkbutton in a gridview
将此修改为:

LinkButton linkbtn =(LinkButton)GridView1.Rows[index].FindControl("LinkButton1");
string sapNo1 = linkbtn.Text;


您无法像这样获得
TemplateField
中的控件属性,您需要找到如下控件:-

LinkButton LinkButton1 = (LinkButton)GridView1.Rows[index].Cells[0]
                                              .FindControl("LinkButton1");
在此之后,您只需获取此控件的属性:-

string sapNo1 = LinkButton1.Text;

您可以通过以下代码获得链接按钮说明

protected void Link_Button_Command(object sender, CommandEventArgs e)
    {
        try
        {
            LinkButton button = (LinkButton)sender;
            string name = button.ToString();

        }
        catch (Exception ex)
        {
            lbl_message.Text = ex.Message;
        }
    }

天啊!你发布的第一个答案很有帮助!非常感谢你的帮助!现在我明白了为什么我不能得到它:)不客气;我很乐意帮助你!
protected void Link_Button_Command(object sender, CommandEventArgs e)
    {
        try
        {
            LinkButton button = (LinkButton)sender;
            string name = button.ToString();

        }
        catch (Exception ex)
        {
            lbl_message.Text = ex.Message;
        }
    }