【问题标题】:Get link button text in gridview from delete button从删除按钮获取gridview中的链接按钮文本
【发布时间】:2015-08-19 05:43:15
【问题描述】:

我有一个带有分页功能的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 中显示数据。

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案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;
    

    【讨论】:

    • 天哪!您发布的第一个答案很有帮助!非常感谢您的帮助!现在我明白为什么我不能得到它:)
    • 不客气;很高兴为您提供帮助!
    【解决方案2】:

    你不能像这样获取TemplateField中存在的控件属性,你需要像这样找到控件:-

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

    在此之后,您可以简单地获取此控件的属性:-

    string sapNo1 = LinkButton1.Text;
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-13
        • 2011-02-25
        • 2016-12-07
        • 2014-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多