我似乎总是遇到这个问题。我在View外面有一个按钮,它调用一个需要OrderNumber
的函数。我一直收到错误,
ArgumentOutOfRangeException was unhandled by user code
在调试模式下,或者在浏览器中,
Specified argument was out of the range of valid values.
这就是我访问它的方式:
string sOrderNumber = (Order_DetailsView.Rows[0].Cells[0].Controls[0] as TextBox).Text;
int orderNumber = Int32.Parse(sOrderNumber);
我也尝试了((TextBox)Order_DetailsView.Rows[0].Cells[0].Controls[0]).Text
以及Rows[i].Cells[i].Controls[i]
中我可以理解的每个索引组合。
这是DetailsView:
<asp:DetailsView ID="Order_DetailsView" runat="server" AutoGenerateRows="False">
<Fields>
<asp:BoundField DataField="OrderNumber" HeaderText="Order #" />
<asp:BoundField DataField="GST" HeaderText="GST" DataFormatString="{0:c}" />
<asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:c}" />
</Fields>
</asp:DetailsView>
我只是这样做错了吗?我查看了我能找到的每个例子,我的代码看起来是合法的。我觉得必须有一些我忽略的简单事物。
答案 0 :(得分:5)
应该有一个TemplateField如下:
<asp:DetailsView ID="Order_DetailsView" runat="server" AutoGenerateRows="False">
<Fields>
<asp:BoundField DataField="OrderNumber" HeaderText="Order #" />
<asp:BoundField DataField="GST" HeaderText="GST" DataFormatString="{0:c}" />
<asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:c}" />
<asp:TemplateField HeaderText="Order Number">
<ItemTemplate>
<asp:TextBox ID="txtOrderNo" runat="server" Text='<%# Bind("OrderNumber") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
然后你可以这样访问它:
string sOrderNumber = ((TextBox)Order_DetailsView.Rows[0].Cells[0].FindControl("txtOrderNo")).Text;
对于BoundField
值,您可以这样做:
protected void Order_DetailsView_DataBound(object sender, EventArgs e)
{
string MyOrderNumber = Order_DetailsView.Rows[0].Cells[0].Text;
}
答案 1 :(得分:2)
您的详细信息视图中没有TextBox
控件,您应该使用TemplateField
,如下所示:
<asp:DetailsView ID="Order_DetailsView" runat="server" AutoGenerateRows="False">
<Fields>
<asp:BoundField DataField="OrderNumber" HeaderText="Order #" />
<asp:TemplateField HeaderText="Order #">
<ItemTemplate>
<asp:Label ID="LabelOrderNumber" runat="server"
Text='<%# Eval("OrderNumber") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="GST" HeaderText="GST" DataFormatString="{0:c}" />
<asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:c}" />
</Fields>
</asp:DetailsView>
然后您可以使用FindControl()
方法按ID值获取Label
控件,如下所示:
Label theOrderNumberLabel = Order_DetailsView.FindControl("LabelOrderNumber") as Label;
// Verify that we found the label before we try to use it
if(theOrderNumberLabel != null)
{
string orderNumberText = theOrderNumberLabel.Text;
// Do something with order number here
}
答案 2 :(得分:1)
您的详细信息中没有文本框是Cell。所以你需要改变你的代码。
string sOrderNumber = Order_DetailsView.Rows[0].Cells[0].Text.ToString();
int orderNumber = Int32.Parse(sOrderNumber);
答案 3 :(得分:0)
**//This controller.cs class will make a .pdf file from the query output. Change //the values at "p" from the attributes of your database table.
//The href tag of calling the controller class Action Export method from the //View class as a MVC design is:
// <a href="@Url.Action("Export","tblOrder")">Print Orders</a>
//Make sure to make the model class with crystal report design and ADO.NET //dataset. I have only include the controller class of the MVC model to
//make it work only.**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationCrystalReportRptSTP.Reports;
using MvcApplicationCrystalReportRptSTP.Models;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
namespace MvcApplicationCrystalReportRptSTP.Controllers
{
public class tblOrderController : Controller
{
private DB_JDBCLOGEntities mde = new DB_JDBCLOGEntities();
//
// GET: /tblOrder/
public ActionResult Index()
{
ViewBag.ListProducts = mde.tblOrders.ToList();
return View();
}
public ActionResult Export()
{
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Reports/CrystalReporttblOrder.rpt")));
rd.SetDataSource(mde.tblOrders.Select(p=> new
{
ID= p.ID,
Ordernum=p.Ordernum,
Username=p.Username,
Password=p.Password,
Price=p.Price.Value,
AddCart=p.AddCart.Value,
Image=p.Image
}).ToList());
Response.Buffer=false;
Response.ClearContent();
Response.ClearHeaders();
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream,"application/pdf","tblOrder.pdf");
}
}
}
发布者:Aneel Goplani。 CIS。 2002年。美国。明尼苏达州立大学,曼凯托。