Csharp/C#教程:从DetailsView获取BoundField的值分享


从DetailsView获取BoundField的值

我似乎总是遇到这个问题。 我在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]).TextRows[i].Cells[i].Controls[i]每个索引组合我可以理解。

这是DetailsView:

        

我只是这样做错了吗? 我看过那些我能找到的每个例子,我的代码看起来是合法的。 我觉得必须有一些我忽略的简单事物。

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

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

应该有一个TemplateField如下:

             

然后你可以这样访问它:

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

您的详细信息视图中没有TextBox控件,您应该使用TemplateField ,如下所示:

              

然后你可以使用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 } 

 **//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: // Print Orders //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年。美国。 明尼苏达州立大学曼凯托分校。

上述就是C#学习教程:从DetailsView获取BoundField的值分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1021680.html

(0)
上一篇 2022年1月6日
下一篇 2022年1月6日

精彩推荐