Gridview命令和选定行

时间:2014-10-08 15:55:25

标签: c# asp.net gridview

我正在尝试获取所选行数据并将其传输到同一页面上的标签。但是我无法使用Gridview.SelectedRow来使用我的CommandName。我已经尝试了一切......

我收到错误。你调用的对象是空的。在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 个答案:

答案 0 :(得分: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;
      }
}
相关问题