3

我似乎总是有这个问题。我在视图之外有一个按钮,它调用一个需要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]

这是详细信息视图:

<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>

我只是做错了吗?我已经查看了我能找到的每个示例,并且我的代码看起来是合法的。我觉得我必须忽略一些简单的事情。

4

4 回答 4

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;
}
于 2013-12-05T05:01:00.627 回答
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>

然后你可以使用该方法通过 ID 值FindControl()获取控件,如下所示: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

}
于 2013-12-05T04:55:54.470 回答
1

您的详细信息中没有文本框,它是一个单元格。所以你需要改变你的代码。

string sOrderNumber = Order_DetailsView.Rows[0].Cells[0].Text.ToString();
int orderNumber = Int32.Parse(sOrderNumber);
于 2013-12-05T04:57:48.717 回答
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。独联体。2002. 美国。明尼苏达州立大学,曼凯托。

于 2016-04-18T13:23:06.783 回答