从删除按钮获取gridview中的链接按钮文本

时间:2015-08-19 05:43:16

标签: 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" 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>

在后端代码中,我试图检索第一列和特定行,但它将我抛空。下面的代码是我如何检索。

int index = Convert.ToInt32(e.RowIndex);
string sapNo1 = GridView1.Rows[index].Cells[0].Text; 
string partNo = GridView1.Rows[index].Cells[1].Text; 
string partDesc = GridView1.Rows[index].Cells[3].Text;

我尝试显示sapNo1的所有值,它让我空白但是对于partNo它将在gridview中显示数据。

有人对此有任何想法吗? 非常感谢您的帮助和评论!

3 个答案:

答案 0 :(得分:1)

由于您的第一列是链接按钮,因此请检索它,然后找到该值。

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;

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

答案 1 :(得分:1)

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

LinkButton LinkButton1 = (LinkButton)GridView1.Rows[index].Cells[0]
                                              .FindControl("LinkButton1");

在此之后,您只需获取此控件的属性: -

string sapNo1 = LinkButton1.Text;

答案 2 :(得分:0)

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

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;
        }
    }