在GridView中的“RowCommand”事件里,怎样获得GridView单元中的值?代码如下:
if (e.CommandName == "Reserve")
            {
                BLL_FlightTb Flight = new BLL_FlightTb();
                string flightid = ((TextBox)(GridView.Rows[e.RowIndex].Cells[10].Controls[0])).Text.ToString().Trim();
                int remain = Convert.ToInt32(((TextBox)(GridView.Rows[e.RowIndex].Cells[10].Controls[0])).Text.ToString());
                if (Flight.Flight_Update_Remain(flightid, remain) > 0)
                {
                    MsgBox("订票成功!");
                }
            }
所有红色的地方都报错,知道是e.RowIndex错了,这个“e”不是那个“e”?怎样获得GridView单元中的值?

解决方案 »

  1.   

    设置CommandArgument取值
    int index = Convert.ToInt32(e.CommandArgument);
    LinkButton lbtn = e.CommandSource as LinkButton ;
    if( lbtn !=null)
    {TextBox txt = lbtn.NamingContainer.FindControl("txtName") as TextBox;}
      

  2.   

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:TextBox ID="ID" runat="server" Text='<%#Eval("ID") %>'>
                </asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:TextBox ID="Name" runat="server" Text='<%#Eval("Name") %>'>
                </asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Reserve" CommandArgument='<%#Container.DataItemIndex  %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>
     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Reserve")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                int id = Convert.ToInt32((GridView1.Rows[index].FindControl("ID") as TextBox).Text);
                string name = (GridView1.Rows[index].FindControl("Name") as TextBox).Text;
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('" + id + "----" + name + "')</script>");
            }
        }
      

  3.   


    //或
            if (e.CommandName == "Reserve")
            {
                Button btn = e.CommandSource as Button;
                int id = Convert.ToInt32((btn.NamingContainer.FindControl("ID") as TextBox).Text);
                string name = (btn.NamingContainer.FindControl("Name") as TextBox).Text;
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('" + id + "----" + name + "')</script>");
            }
      

  4.   

    首先要获得点击的行号,
    如果你的按钮是LinkButton的话,代码如下:
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Reserve")
            {
                LinkButton lbn = (LinkButton)e.CommandSource;
                int index = ((GridViewRow)lbn.NamingContainer).RowIndex;//获得行号
                string flightid = ((TextBox)(GridView1.Rows[index].Cells[10].Controls[0])).Text.ToString().Trim();
                //........................        }
        }
      

  5.   

    如何在GridView的RowCommand事件中获取当前的GridViewRow http://www.cnblogs.com/Jinglecat/archive/2007/07/05/806460.html
      

  6.   

    gridview里控件的command属性值设置为select。
      

  7.   


        protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[1].Text == "1")
            {
                e.Row.Cells[1].Text = "有效";
            }
      

  8.   


    后台代码如下:
     protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "Reserve")
                {
                    Button btn = e.CommandSource as Button;
      int flightid = Convert.ToInt32((btn.NamingContainer.FindControl("FlightID") as TextBox).Text);    
    异常信息:未将对象引用设置到对象的实例。
                    string price = (btn.NamingContainer.FindControl("Price") as TextBox).Text;
                    Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert('"+flightid+"----"+price+"')</script>");
                }
            }
    前台“代码如下”:
    <asp:GridView ID="GridView" runat="server" Visible="False" CellPadding="2" 
            ForeColor="#333333" AutoGenerateColumns="False" CellSpacing="2" 
            onrowcommand="GridView_RowCommand">
        <FooterStyle  BackColor="#990000" Font-Bold="true"  ForeColor="White" />
        <Columns>
        <asp:BoundField  DataField="FlightID"  HeaderText="航班号"  ReadOnly="true"/>
        <asp:BoundField DataField="FlightTime" HeaderText="航班时间" />
        <asp:BoundField  DataField="FlightStart" HeaderText="起始地点"/>
        <asp:BoundField  DataField="FlightEnd" HeaderText="到达地点"/>
        <asp:BoundField  DataField="Launch" HeaderText="起飞时间"/>
        <asp:BoundField  DataField="Land" HeaderText="到达时间"/>
        <asp:BoundField DataField="MidStation" HeaderText="中间站" />
        <asp:BoundField DataField="Capacity" HeaderText="乘客量" />
        <asp:BoundField DataField="Remain" HeaderText="空票数" />
        <asp:BoundField DataField="Price" HeaderText="票价" />
        <asp:ButtonField ButtonType="Link" Text="订票"  CommandName="Reserve"/>
        </Columns>
        <RowStyle ForeColor="#000066"  />
        <SelectedRowStyle  BackColor="#669999" Font-Bold="true"  ForeColor="White"/>
        <PagerStyle  BackColor="White"  ForeColor="#000066" HorizontalAlign="Left"/>
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
    请问,为什么会出现这样的错误呢?
      

  9.   

    <asp:TemplateField HeaderText="航班号">
    <ItemTemplate>
    <asp:TextBox ID="txtFlightID" runat="server" Text='<%#Eval("FlightID")%>'></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="空票数">
    <ItemTemplate>
    <asp:TextBox ID="txtRemain" runat="server" Text='<%#Eval("Remain")%>'></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:ButtonField ButtonType="Link" Text="订票" CommandName="Reserve" CommandArgument='<%# Container.DataItemIndex %>'/>
    if (e.CommandName == "Reserve")
    {
    string FlightID=null;
    string Remain= null;
    FlightID= ((TextBox)GridView.Rows[Convert.ToInt32(e.CommandArgument.ToString())].FindControl("txtFlightID")).Text;
    Remain= ((TextBox)GridView.Rows[Convert.ToInt32(e.CommandArgument.ToString())].FindControl("txtRemain")).Text;
    if (Flight.Flight_Update_Remain(FlightID,Convert.ToInt32(Remain)) > 0)
    {
      MsgBox("订票成功!");
    }
    }
      

  10.   


    因为你的按钮类型不是Button,而是一个超链接按钮 LinkButton,所以必须改成下面这种方式LinkButtonbtn = e.CommandSource as LinkButton;
      

  11.   


    可是改为“button”后,还是报错啊。改后的代码如下:
    <asp:GridView ID="GridView" runat="server" Visible="False" CellPadding="2" 
            ForeColor="#333333" AutoGenerateColumns="False" CellSpacing="2" 
            onrowcommand="GridView_RowCommand">
        <FooterStyle  BackColor="#990000" Font-Bold="true"  ForeColor="White" />
        <Columns>
        <asp:BoundField  DataField="FlightID"  HeaderText="航班号"  ReadOnly="true"/>
        <asp:BoundField DataField="FlightTime" HeaderText="航班时间" />
        <asp:BoundField  DataField="FlightStart" HeaderText="起始地点"/>
        <asp:BoundField  DataField="FlightEnd" HeaderText="到达地点"/>
        <asp:BoundField  DataField="Launch" HeaderText="起飞时间"/>
        <asp:BoundField  DataField="Land" HeaderText="到达时间"/>
        <asp:BoundField DataField="MidStation" HeaderText="中间站" />
        <asp:BoundField DataField="Capacity" HeaderText="乘客量" />
        <asp:BoundField DataField="Remain" HeaderText="空票数" />
        <asp:BoundField DataField="Price" HeaderText="票价" />
        <asp:ButtonField  ButtonType="Button" Text="订票"  CommandName="Reserve"/>    </Columns>
        <RowStyle ForeColor="#000066"  />
        <SelectedRowStyle  BackColor="#669999" Font-Bold="true"  ForeColor="White"/>
        <PagerStyle  BackColor="White"  ForeColor="#000066" HorizontalAlign="Left"/>
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
      

  12.   

    会提示:“CommandArgument='<%# Container.DataItemIndex %>'/”不是有效属性的啊?
      

  13.   

    你把这一句代码换一下
    <asp:ButtonField ButtonType="Button" Text="订票" CommandName="Reserve"/> </Columns>
    <asp:TemplateField HeaderText="订票">
        <ItemTemplate>
             <asp:Button ID="btnOk" CommandName="Reserve" runat="server" Text="确定" />
        </ItemTemplate>
    </asp:TemplateField>后台cs代码还是使用:Button btn = e.CommandSource as Button;
    现在就肯定可用了。 结贴给分,:)
      

  14.   


    提示错误:未将对象引用设置到对象的实例。前台代码:
    <asp:GridView ID="GridView" runat="server" Visible="False" CellPadding="2" 
            ForeColor="#333333" AutoGenerateColumns="False" CellSpacing="2" 
            onrowcommand="GridView_RowCommand">
        <FooterStyle  BackColor="#990000" Font-Bold="true"  ForeColor="White" />
        <Columns>
        <asp:BoundField  DataField="FlightID"  HeaderText="航班号"  ReadOnly="true"/>
        <asp:BoundField DataField="FlightTime" HeaderText="航班时间" />
        <asp:BoundField  DataField="FlightStart" HeaderText="起始地点"/>
        <asp:BoundField  DataField="FlightEnd" HeaderText="到达地点"/>
        <asp:BoundField  DataField="Launch" HeaderText="起飞时间"/>
        <asp:BoundField  DataField="Land" HeaderText="到达时间"/>
        <asp:BoundField DataField="MidStation" HeaderText="中间站" />
        <asp:BoundField DataField="Capacity" HeaderText="乘客量" />
        <asp:BoundField DataField="Remain" HeaderText="空票数" />
        <asp:BoundField DataField="Price" HeaderText="票价" />
        <asp:TemplateField >
        <ItemTemplate>
        <asp:Button ID="btnOk" CommandName="Reserve"  runat="server"  Text="确定"/>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        <RowStyle ForeColor="#000066"  />
        <SelectedRowStyle  BackColor="#669999" Font-Bold="true"  ForeColor="White"/>
        <PagerStyle  BackColor="White"  ForeColor="#000066" HorizontalAlign="Left"/>
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        </asp:GridView>后台代码:
    protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "Reserve")
                {                Button btn = e.CommandSource as Button;
                    int flightid = Convert.ToInt32((btn.NamingContainer.FindControl("FlightID") as TextBox).Text);
                    string price = (btn.NamingContainer.FindControl("Price") as TextBox).Text;
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('" + flightid + "----" + price + "')</script>");
                }
            }请问应该怎样修改?
      

  15.   

    int flightid = Convert.ToInt32((btn.NamingContainer.FindControl("FlightID") as TextBox).Text);
    string price = (btn.NamingContainer.FindControl("Price") as TextBox).Text;
    上面两条代码都是要求你的列类型是模板列,比如
    <asp:TemplateField HeaderText="订票">
    <ItemTemplate>
         <TextBox runat="server" ID="FlightID" Text='<%# Eval("FlightID")%>' />
    </ItemTemplate>
    </asp:TemplateField>
    情况下才能使用的,而你现在使用的列类型是默认的数据绑定列,采用这种方式就不行,因为在你的列中没有id为FlightID的文本框,所以肯定是找不到的,怎么办呢?    //1、先找到被点击的按钮所在的GridView数据行
        Button btn = e.CommandSource as Button;
        //btn.parent表示按钮所在单元格,btn.parent.parent就表示按钮所在的行
        GridViewRow currentRow = (GridViewRow)btn.parent.parent;    //2、找到行中你要获取的单元格(根据下标),航班号是第一个单元格,下标为0
        //这个属性名到底是Text还是Value,我不记得了,但肯定是二者之一
        int flightid = Convert.ToInt32(currentRow.Cells[0].Text);
        string price = currentRow.Cells[9].Text;你再试一试吧
      

  16.   

    FindControl("FlightID") as TextBox你前台 <asp:BoundField DataField="FlightID" HeaderText="航班号" ReadOnly="true"/>这不是一个textbox!!两个不是对应的! 找不到控件的   所以报错!!你改成模板列   放一个textbox 就对应上了楼主是不是从来不知道模板列?
      

  17.   


                    Button btn = e.CommandSource as Button;
                    GridViewRow currentrow = (GridViewRow)btn.Parent.Parent;异常信息:未将对象引用设置到对象的实例。
                    int flightid = Convert.ToInt32(currentrow.Cells[0].Text);
                    string price = currentrow.Cells[9].Text;
      

  18.   

    <asp:Button ID="btnOk" CommandName="Reserve" runat="server" onclick="Button1_Click"  Text="确定"/> protected void Button1_Click(object sender, EventArgs e)
      {
      Button btn= (Button)sender;
      GridViewRow gr=(GridViewRow)btn.NamingContainer;
      int Index = ((GridViewRow)(btn.NamingContainer)).RowIndex;//获得行号
      string s= GridView1.Rows[Index].Cells[0].Text;
      string a = ((Label)GridView1.Rows[Index].Cells[0].FindControl("Label1")).Text;
      }