DataBinder.Eval 关于有些属性不存在的问题。

勿腻阳 2008-07-15 03:57:51
客户端代码
<%@ Page language="c#" Codebehind="DataGridMasterDetail.aspx.cs" AutoEventWireup="false" Inherits="MBO.Chapter11.DataGridMasterDetail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridMasterDetail</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgrdCategories" OnItemCommand="dgrdCategories_ItemCommand" DataKeyField="CategoryID"
AutoGenerateColumns="False" SelectedItemStyle-BackColor="LightGreen" ShowHeader="False" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton Runat="server">
<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Description"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
<p>
<asp:DataGrid ID="dgrdProducts" HeaderStyle-BackColor="yellow" Runat="Server"></asp:DataGrid>
</p>
</form>
</body>
</HTML>

服务端代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace MBO.Chapter11
{
/// <summary>
/// DataGridMasterDetail 的摘要说明。
/// </summary>
public class DataGridMasterDetail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgrdProducts;
protected System.Web.UI.WebControls.DataGrid dgrdCategories;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
BindMasterGrid();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

protected void BindMasterGrid()
{
SqlConnection conNorthwind = new SqlConnection("server=localhost;uid=uid;pwd=pwd;database=Northwind");
SqlCommand cmdSelect = new SqlCommand("select * from categories", conNorthwind);
conNorthwind.Open();
dgrdCategories.DataSource = cmdSelect.ExecuteReader();
dgrdCategories.DataBind();
conNorthwind.Close();
}

private void BindDetailGrid(int intCatID)
{
SqlConnection conNorthwind = new SqlConnection("server=localhost;uid=uid;pwd=pwd;database=Northwind");
SqlCommand cmdSelect = new SqlCommand("Select * From Products Where CategoryID=@catID", conNorthwind);
cmdSelect.Parameters.Add( "@catID", intCatID );
conNorthwind.Open();
dgrdCategories.DataSource = cmdSelect.ExecuteReader();
dgrdCategories.DataBind();
conNorthwind.Close();
}

protected void dgrdCategories_ItemCommand( object sender, DataGridCommandEventArgs e )
{
int intCatID;
intCatID = Convert.ToInt32(dgrdCategories.DataKeys[ e.Item.ItemIndex ]);
dgrdCategories.SelectedIndex = e.Item.ItemIndex;
BindDetailGrid( intCatID );
}
}
}

出现的错误:
DataBinder.Eval:“System.Data.Common.DbDataRecord”不包含名称为 CategoryName 的属性。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Web.HttpException: DataBinder.Eval:“System.Data.Common.DbDataRecord”不包含名称为 CategoryName 的属性。

源错误:


行 17: <ItemTemplate>
行 18: <asp:LinkButton Runat="server">
行 19: <%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
行 20: </asp:LinkButton>
行 21: </ItemTemplate>


源文件: c:\inetpub\wwwroot\MBO\Chapter11\DataGridMasterDetail.aspx 行: 19

请高手门不惜赐教。谢谢!


...全文
216 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
勿腻阳 2008-07-27
  • 打赏
  • 举报
回复
问题找到了,就是“乌鸦”所说的应该写在LinkButton里而不是写在外面,虽然花了不少时间还是把问题解决了,在此谢谢各位好心的指点。这就加分去。
西安风影 2008-07-17
  • 打赏
  • 举报
回复
没有该字段
tinkcn 2008-07-16
  • 打赏
  • 举报
回复
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton Runat="server">
<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>


为什么<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
会嵌套在<asp:LinkButton 中?

有点看不懂 也许问题就在这里
勿腻阳 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 li520na 的回复:]
楼主查的时候把字段分别写写出来,然后绑定,或许就不出错了,现在是找不到你的字段
[/Quote]

按照你说的查询语句中把字段都罗列出来了,但“然后绑定”能说的具体些吗?不会要把所有的字段都要绑定吧?毕竟我也用不了那么多字段。请说的详细些,谢谢!
李班头 2008-07-16
  • 打赏
  • 举报
回复
楼主查的时候把字段分别写写出来,然后绑定,或许就不出错了,现在是找不到你的字段
勿腻阳 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tinkcn 的回复:]
把 <%# DataBinder.Eval(Container.DataItem, "CategoryName") %> 中的双引号改成单引号
要是还不行 改成这样
" <%# DataBinder.Eval(Container.DataItem, 'CategoryName') %> "
[/Quote]

同样感谢“tinkcn”的建议。
不过我也按照你的两种方法都试过了,但现在客户端似乎不认这样的写法,总是报如下的错误:
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。

编译器错误信息: CS1012: 字符文本中字符太多
源错误:

行 17: <ItemTemplate>
行 18: <asp:LinkButton Runat="server">
行 19: <%# DataBinder.Eval(Container.DataItem, 'CategoryName') %>
行 20: </asp:LinkButton>
行 21: </ItemTemplate>

或者
编译器错误信息: CS1012: 字符文本中字符太多
源错误:

行 17: <ItemTemplate>
行 18: <asp:LinkButton Runat="server">
行 19: "<%# DataBinder.Eval(Container.DataItem, 'CategoryName') %>"
行 20: </asp:LinkButton>
行 21: </ItemTemplate>

若还有方法请不惜赐教,谢谢。
勿腻阳 2008-07-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kongwei521 的回复:]
MS用一个绑定就解决问题了
protected void BindMasterGrid()
{
SqlConnection conNorthwind = new SqlConnection("server=localhost;uid=uid;pwd=pwd;database=Northwind");
SqlCommand cmdSelect = new SqlCommand("select * from categories a inner join Products b on a.CategoryID=b.CategoryID Where CategoryID=@catID ", conNorthwind);
conNorthwind.Open();
dgrdCategories.DataSource = cmdSelect.Execut…
[/Quote]

首先谢谢"kongwei521"的建议。
我用了你说的方法,不过“select * from categories a inner join Products b on a.CategoryID=b.CategoryID Where CategoryID=@catID”似乎应该在BindDetailGrid中,同时CategoryID=@catID似乎应该写成a.CategoryID=@catID或者b.CategoryID=@catID,否则它会提示CategoryID指代不明确,不知道我的做法是否正确。但即使这样数据还是显示不正确,而且主从页不在一起显示而是分页显示。还请耐心指教,再次感谢。
路人乙e 2008-07-16
  • 打赏
  • 举报
回复
try
<%# DataBinder.Eval(Container, "DataItem.CategoryName") %>
tinkcn 2008-07-15
  • 打赏
  • 举报
回复
把<%# DataBinder.Eval(Container.DataItem, "CategoryName") %> 中的双引号改成单引号
要是还不行 改成这样
"<%# DataBinder.Eval(Container.DataItem, 'CategoryName') %> "
billclinton8 2008-07-15
  • 打赏
  • 举报
回复
你的绑定集合 没有CategoryName字段值
TopFans 2008-07-15
  • 打赏
  • 举报
回复
应该是字段没取出来
蝶恋花雨 2008-07-15
  • 打赏
  • 举报
回复
MS用一个绑定就解决问题了
protected void BindMasterGrid()
{
SqlConnection conNorthwind = new SqlConnection("server=localhost;uid=uid;pwd=pwd;database=Northwind");
SqlCommand cmdSelect = new SqlCommand("select * from categories a inner join Products b on a.CategoryID=b.CategoryID Where CategoryID=@catID ", conNorthwind);
conNorthwind.Open();
dgrdCategories.DataSource = cmdSelect.ExecuteReader();
dgrdCategories.DataBind();
conNorthwind.Close();
}
用一个绑定试试
勿腻阳 2008-07-15
  • 打赏
  • 举报
回复
“select * from categories”的categories表中包含“CategoryName”字段。
“Select * From Products”的Products表中不包含“CategoryName”字段,但他们都有的是“CategoryID”字段,我是做了一个主从表单,用“CategoryID”字段联系这两张表。难道这样写是错误的?若是这样的话有改正的办法吗?
蝶恋花雨 2008-07-15
  • 打赏
  • 举报
回复
你SQL语句查询有这个字段吗?或者说这个字段在另一个表里面。你没关联不行写成这样试试
<%#Eval("CategoryName") %> 如果SQL语句查询没这个CategoryName字段就会报那个错误
cooolchen 2008-07-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cooolchen 的回复:]
检查一下 select 语句中是否包含 CategoryName 这个字段,或者是否写错。
[/Quote]

检查你查询的表中是否包含该字段。
cooolchen 2008-07-15
  • 打赏
  • 举报
回复
检查一下 select 语句中是否包含 CategoryName 这个字段,或者是否写错。
一、AspNetPager支持两种方式分页: 一种是PostBack方式分页, 一种是通过Url来实现分页以及Url重写功能 二、AspNetPager支持各种数据绑定控件GridView、DataGrid、DataList、Repeater以及自定义的数据绑定控件的分页功能十分强大。 三、AspNetPager分页控件本身并不显示任何数据,而只显示分页导航元素,数据在页面上的显示方式与该控件无关,所以需要手写数据连接方法来配合, 四、结合TOP 。。。NOT IN 的通用存储过程分页方法使用AspNetPager十分实用 测试控件datalist aspnetpager 的分页方法示例 分页方法为 PostBack 方式 1、 首先将AspNetPager.dll复制于应用程序下的bin目录,打开解决方案,引入dll文件 2、 在工具栏中添加控件,这样可以支持拖拽使用 3、 要使用AspNetPager 要为其设置最基本的属性 使用 SqlServer Northwind数据库的 Products表 protected Wuqi.Webdiyer.AspNetPager AspNetPager1; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.DataList DataList1; private void Page_Load(object sender, System.EventArgs e) { this.AspNetPager1.PageSize=10; //设置每也显示的记录条数 if(!IsPostBack) //只在页面第一次加载时起作用 { SqlDBManager db = new SqlDBManager(System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionString"]); AspNetPager1.RecordCount=db.CountPage("products");//获得要使用表的记录总数 //db.CountItems自定义的方法 this.BindData(); } } private void BindData() { SqlDBManager db= new SqlDBManager(System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionString"].ToString(); DataList1.DataSource=db.FenPage(this.AspNetPager1.PageSize,this.AspNetPager1.CurrentPageIndex,"productid","products","productid,productname,unitprice,unitsinstock",""); //自定义方法由 TOP not in 存储过程分页方法改编 this.DataList1.DataBind(); //控件数据绑定 this.Label1.Text="当前第"+this.AspNetPager1.CurrentPageIndex+"页 总"+this.AspNetPager1.PageCount+"页"; } private void AspNetPager1_PageChanged(object sender, System.EventArgs e) { //页索引改变方法 this.BindData(); } 设计页效果 DataList id="DataList1" style="Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 96px" runat="server"> erTemplate> er='1'> erTemplate> erTemplate>
产品ID 产品名称 产品数量 产品单价
erTemplate> <%# DataBinder.Eval(Container.DataItem,"Productid")%> <%# DataBinder.Eval(Container.DataItem,"productname")%> <%# DataBinder.Eval(Container.DataItem,"unitprice")%> <%# DataBinder.Eval(Container.DataItem,"unitsinstock")%>
DataList> er:AspNetPager id="AspNetPager1" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 40px" runat="server" Width="500px" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" Height="40px" NumericButt PagingButt ShowNavigati ShowInputBox="Always" TextAfterInputBox="页" TextBeforeInputBox="跳转到第" AlwaysShow="True"> er:AspNetPager> Label

61,830

社区成员

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

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

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

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