在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单元中的值?
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单元中的值?
解决方案 »
- 未将对象引用设置到对象的实例。
- VS2012启动调试问题
- 关于查询问题。
- 在ASP.net中插入FLASH之后总是不能正常的显示。提示是“未加载影片”
- 继承自WebControl与继承自UserControl的控件有何别
- 急!存储过程的结果放到DataSet中怎么不对?
- 局域网有两台机子,一台装WINDOWS 2000 SERVER ,另一台装 WINDOWS XP PRO ,为什么 两台机子不能互相访问,需要做那些工作??
- 模糊查询中为什么"C++"查不出来,但是C可以查出来,声明不是因为空格的问题.
- 一个问题?
- 求援:以下错误何解?[manifest definition ]
- 获得GridView中的单元格中的值
- 为什么只能单向引发事件
int index = Convert.ToInt32(e.CommandArgument);
LinkButton lbtn = e.CommandSource as LinkButton ;
if( lbtn !=null)
{TextBox txt = lbtn.NamingContainer.FindControl("txtName") as TextBox;}
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>");
}
}
//或
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>");
}
如果你的按钮是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();
//........................ }
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[1].Text == "1")
{
e.Row.Cells[1].Text = "有效";
}
后台代码如下:
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>请问,为什么会出现这样的错误呢?
<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("订票成功!");
}
}
因为你的按钮类型不是Button,而是一个超链接按钮 LinkButton,所以必须改成下面这种方式LinkButtonbtn = e.CommandSource as LinkButton;
可是改为“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>
<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;
现在就肯定可用了。 结贴给分,:)
提示错误:未将对象引用设置到对象的实例。前台代码:
<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>");
}
}请问应该怎样修改?
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;你再试一试吧
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;
{
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;
}