2014-10-08 22 views
0

我想獲取選定的行數據並將其轉移到同一頁上的標籤。但是我無法讓Gridview.SelectedRow與我的CommandName一起工作。我已經嘗試了所有的東西....Gridview命令和選定的行

我得到的錯誤。你調用的對象是空的。在Label2.Text

這裏是我的代碼:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) 
{ 
    // If multiple ButtonField column fields are used, use the 
    // CommandName property to determine which button was clicked. 
    if (e.CommandName == "Grab") 
    { 
     Label2.Text = GridView1.SelectedRow.Cells[2].Text; 
     Label3.Text = GridView1.SelectedRow.Cells[3].Text; 
     Label4.Text = GridView1.SelectedRow.Cells[4].Text; 
     Label5.Text = GridView1.SelectedRow.Cells[5].Text; 
    } 
} 
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" CssClass="td" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="574px" onrowcommand="GridView1_RowCommand"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
     <asp:TemplateField HeaderText="Action"> 
      <ItemTemplate> 
       <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/edit.png" OnClick="ImageButton2_Click" /> 
       <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Select" ImageUrl="~/images/delete.png" onclientclick=" return confirm('Are you want to Delete this Vehicle?');" /> 
       <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/images/refre.png" CommandName="Grab" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> 
     <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" /> 
     <asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" /> 
     <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> 
     <asp:BoundField DataField="Submodel" HeaderText="Submodel" SortExpression="Submodel" /> 
     <asp:BoundField DataField="ISENABLED" HeaderText="ISENABLED" SortExpression="ISENABLED" /> 
    </Columns> 
    <FooterStyle BackColor="#CCCC99" /> 
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> 
    <RowStyle BackColor="#F7F7DE" /> 
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 
    <SortedAscendingCellStyle BackColor="#FBFBF2" /> 
    <SortedAscendingHeaderStyle BackColor="#848384" /> 
    <SortedDescendingCellStyle BackColor="#EAEAD3" /> 
    <SortedDescendingHeaderStyle BackColor="#575357" /> 
</asp:GridView> 

回答

1

嘗試這樣的事情。由於您試圖訪問command事件中的值,而不是在OnSelectedIndexChanged事件中,因此您需要先觸發觸發命令事件的行。

if (e.CommandName == "Grab") 
{ 
     GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer; 
     if (row != null) 
     { 
      Label2.Text = row.Cells[2].Text; 
      Label3.Text = row.Cells[3].Text; 
      Label4.Text = row.Cells[4].Text; 
      Label5.Text = row.Cells[5].Text; 
     } 
} 
+0

非常感謝! – 2014-10-08 16:15:46

+0

很高興它適合你。 – 2014-10-08 16:17:36

+0

哈...我試圖讓一個按鈕顯示一些基於選定行的文本。這總是回到後面,使用OnSelectedIndexChanged解決了這個問題。 – 2018-02-07 18:25:40