--兼谈 DadaGrid 模板列的创建
DadaGrid 是 ASP.NET 编程中一个很重要的控件,其优良的可定制功能为提高它的表现力提供了极大的方便。除了与数据源直接绑定以外,我们还可以通过列绑定模板对 DataGrid 的列进行自定义,来按照我们设定的格式显示数据。
例如,数据表中有一个字段 f_DemoImage 用来存放图片的路径(包括图片文件名),为了在 DataGrid 的 Cell 中显示实际的图片,我们可以定义一个模板列,然后给该列赋予字段 f_DemoImage 的值,就可以在 DataGrid 的 Cell 中显示图片。
WebForm1.aspx 文件:
<%@ Page language="c#" Codebehind="Wizard_select_template.aspx.cs" AutoEventWireup="false" Inherits="WebHatcher.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
<LINK href="./styles/style.css" type=text/css rel=stylesheet >
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
<asp:datagrid id=DataGrid1 runat="server" AutoGenerateColumns="False" Width="744px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image id="Image1" ImageUrl='<% #DataBinder.Eval(Container,"DataItem.f_DemoImage") %>' runat="server"></asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</form>
</body>
</HTML>
///WebForm1.aspx.cs 文件:
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 myDBAccess; //我的命名空间
namespace WebHatcher
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (Page.IsPostBack)
{}
else
{
// 在 DataGrid 中显示数据(包括图象):
myTableAccess oDbTable = new myTableAccess(); //myTableAccess我是定义的数据库访问类
oDbTable.sDbPath = Server.MapPath("./webhatcher.mdb");
oDbTable.sPassword = "";
oDbTable.sSQL = "select * from tabTemplate"; //tabTemplate是包含 f_DemoImage 字段的数据表
DataGrid1.DataSource = oDbTable.ReadFromTable();
DataGrid1.DataBind();
}
}
#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
}
}
//数据访问类的定义:
using System;
using System.Data;
using System.Data.OleDb;
namespace myDBAccess
{
/// 数据库存取 namespace:
/// 类名:myTableAccess
/// 属性:
///sDbPath 数据库路径(包括数据库文件名)
/// sPassword 数据库口令
/// sSQL SQL 语句
public class myTableAccess
{
//声明 3 个属性(域):
public string sDbPath = "";
public string sPassword = "";
public string sSQL = "";
private OleDbConnection oConn;
public void OpenDB()
{
oConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sDbPath + ";Password=" + sPassword + ";");
oConn.Open();
}
public void CloseDB()
{oConn.Close();}
public DataView ReadFromTable()
{
OpenDB();
OleDbDataAdapter oAdp = new OleDbDataAdapter(sSQL, oConn);
DataSet oDtSt = new DataSet();
oAdp.Fill(oDtSt, "aTable");
CloseDB();
return oDtSt.Tables["aTable"].DefaultView; //返回默认视图
}
}
}
张庆 zhangking@263.net
http://www.why100000.com
http://soft.why100000.com
QQ:9365852
2004.5.28
分享到:
相关推荐
c# webform 合成图片 在线预览图片 bs模式
C# WinForm,WebForm,Clipboard Image 显示方法 应用程序,网页,剪贴板存储并显示图片方法
Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0)\Ajax 实现在WebForm中拖动控件并即时在服务端保存状态数据 (Asp.net 2.0).rar
很多时候Windows程序也需要用到图文并茂的效果,也希望能类似webform中的htmleditor那样,这里提供一位高人写的控件,很好用。
webform中验证码的三种做法的小程序,通过它你可以学习到有关验证码的知识。
AjaxPro 实现在WebForm中拖动控件并实时在服务端保存状态数据
c# webform无刷新前后台交互
WebForm的EasyUI 有需要的朋友可以下载,带数据库脚本. 先操作'数据库.sql',再打开EasyUI.sln 去Web.config 调整下,即可运行,如有不明白的可留言我
根据文件URL,WebForm页面直接显示pdf文件内容,涉及JS,HTML.zip
用WebForm制作的自定义分页控件,可以将数据源分段显示在指定的数据绑定控件上。
资源名称:DoNet学习-WebForm视频教程(30集)资源目录:【】7ab37727cfeaf3ac0e59eff10c664831【】WebForm状态_上午1复习页面生命周期复习Transfer【】...资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
可在webForm上使用的mvc框架v1.1 源码描述: 一、源码介绍 不需要使用.net mvc庞大的dll库,只是一个简单的dll,可以轻松实现网页和接口的开发,目前为1.0版本 二、注意事项 1、开发环境为Visual Studio 2012,无...
asp.net中webform 多选下拉框DropDownlist
简单webform仿Winform TabControl标签页 ,用js实现
C# .net webform 项目,运行后地址 无aspx 后缀。完整源码。
此源碼演示了如何在網絡讓用戶端動態地操作word文檔,相關介紹文章請查看本站原創文章中“基於WebForm的操作Word文檔實例”一文!如遇誠{試出錯,請確認是否安裝了OfficeXP,如果還是出錯,請按“基於WebForm的操作...
SQLite WebForm Demo
C#基于WebForm的学员管理系统源码.zip
.net实现webform HTTP文件上传
webform AJAX DEMO演示代码例子