2015-08-19 55 views
0

我有一個帶分頁功能的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中的數據。

有人對此有任何想法嗎? 我真的很感謝你的幫助和評論!

回答

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; 
+0

omg!你發佈的第一個答案是有幫助的!非常感謝您的幫助!現在我明白爲什麼我不能得到它:) – Handsome

+1

不客氣,我一直很樂意幫助你! –

1

你不能得到禮物裏面TemplateField這樣的控件的屬性,你需要找到這樣的控制: -

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

之後,您可以簡單地獲取此控件的屬性: -

string sapNo1 = LinkButton1.Text;