Get the value of a BoundField from a DetailsView

2019-02-26 00:56发布

问题:

I seem to always have problems with this. I have a button outside of the View that calls a function that needs an OrderNumber. I keep getting an error,

ArgumentOutOfRangeException was unhandled by user code

in debug mode, or this one in the browser,

Specified argument was out of the range of valid values.

This is how I'm accessing it:

string sOrderNumber = (Order_DetailsView.Rows[0].Cells[0].Controls[0] as TextBox).Text;
int orderNumber = Int32.Parse(sOrderNumber);

I've also tried ((TextBox)Order_DetailsView.Rows[0].Cells[0].Controls[0]).Text and every combination of indexes in Rows[i].Cells[i].Controls[i] that I could fathom.

Here is the 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>

Am I just doing this all wrong? I've looked at every example out there I could find, and my code looks legit from what I can tell. I feel like there must be some simple thing I'm overlooking.

回答1:

There is no textbox in your details its a Cell. So u need to change your code.

string sOrderNumber = Order_DetailsView.Rows[0].Cells[0].Text.ToString();
int orderNumber = Int32.Parse(sOrderNumber);


回答2:

There should be a TemplateField as below:

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

Then you could access it this way:

string sOrderNumber = ((TextBox)Order_DetailsView.Rows[0].Cells[0].FindControl("txtOrderNo")).Text;

And for the BoundField value you can do this way:

protected void Order_DetailsView_DataBound(object sender, EventArgs e)
{
    string MyOrderNumber = Order_DetailsView.Rows[0].Cells[0].Text;
}


回答3:

There is no TextBox control in your details view, you should use a TemplateField, like this:

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

Then you can use the FindControl() method to get the Label control by ID value, like this:

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

}


回答4:

**//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");
        }
    }
}

Posted By: Aneel Goplani. CIS. 2002. USA. Minnesota State University, Mankato.