异常信息:指定的参数已超出有效值的范围。

sssolweb_005 2010-12-26 01:37:49
异常信息提示:指定的参数已超出有效值的范围。

protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Reserve")
{
int rowindex = Convert.ToInt32(e.CommandArgument);
string flightid = ((TextBox)(GridView.Rows[rowindex].Cells[1].Controls[0])).Text.ToString().Trim();


异常信息提示:指定的参数已超出有效值的范围。

这是为什么呢?已经查看过rowindex的值为“0”,而且还存在一条记录,请问这是为什么呢?

应该怎样获取Gridview中的值呢?


GridView前台代码:
<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" CommandName="Reserve" Text="订票"/>
</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>
}
}
...全文
237 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
SK_Aqi 2010-12-28
  • 打赏
  • 举报
回复
得写RowCommand事件才可以@!
西门喷血 2010-12-28
  • 打赏
  • 举报
回复
可以使用模板列 直接在模板列的控件里面写onclick事件
后台转换为控件 取得值
sssolweb_005 2010-12-28
  • 打赏
  • 举报
回复
ycproc 2010-12-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sssolweb_005 的回复:]

引用 8 楼 kevin_520_kevin 的回复:
CommandArgument值设置了么?


什么是模板列呢?
[/Quote]

现自学 了解gridview 基本属性
sssolweb_005 2010-12-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 kevin_520_kevin 的回复:]
CommandArgument值设置了么?
[/Quote]

什么是模板列呢?
kevin_520_kevin 2010-12-26
  • 打赏
  • 举报
回复
CommandArgument值设置了么?
koukoujiayi 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sssolweb_005 的回复:]
int rowindex = Convert.ToInt32(e.CommandArgument);
这样不就已经获得当前行的索引了???
[/Quote]
你是非模版列,是没有CommandArgument这个属性的!
只有模版列才有这个属性!
wyq29 2010-12-26
  • 打赏
  • 举报
回复
楼主 你先学习一下 什么是gridview模板列

gridview 你只学习了10%不到 就会一招BoundField,以后没办法干活啊。 很多东西给你写出来 你还是不明白



sssolweb_005 2010-12-26
  • 打赏
  • 举报
回复
int rowindex = Convert.ToInt32(e.CommandArgument);
这样不就已经获得当前行的索引了???
sssolweb_005 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;//取得触发命令的当前行
int index = row.RowIndex;

CommandName="Reserve" CommandArgument='<%# Container.DataItemIndex %>'/>
[/Quote]

这一句报错啊。

异常信息如下:无法将类型为“System.Web.UI.HtmlControls.HtmlForm”的对象强制转换为类型“System.Web.UI.WebControls.GridViewRow”。
sssolweb_005 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;//取得触发命令的当前行
int index = row.RowIndex;

CommandName="Reserve" CommandArgument='<%# Container.DataItemIndex %>'/>
[/Quote]

添加“CommandArgument”,可是电脑提示不存在总金额各属性啊。。。


wuyq11 2010-12-26
  • 打赏
  • 举报
回复
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;//取得触发命令的当前行
int index = row.RowIndex;

CommandName="Reserve" CommandArgument='<%# Container.DataItemIndex %>'/>
子夜__ 2010-12-26
  • 打赏
  • 举报
回复

<asp:ButtonField ButtonType="Button" CommandName="Reserve" Text="订票"/>
添加CommandArgument
<asp:ButtonField ButtonType="Button" CommandName="Reserve"  CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" Text="订票"/>



或者直接e.items.rowindex...


62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧