日期:2014-05-18  浏览次数:20427 次

DataBinder.Eval 关于有些属性不存在的问题。
客户端代码:
<%@ 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(&quo