当前位置: 首页 > 图文教程 > 网络编程 > ASP > 制作一个简单的服务器端控制

ASP
ASP视频教程:后台页面加入限制访问和禁用缓存功能
ASP视频教程:制作网站前台首页
ASP视频教程:备份和还原SQL Server 2000数据库
ASP实例教程:asp无限级显示分类代码
IIS无法启动错误的几种情况汇总
asp Http_Referer,Server_Name和Http_Host
ASP 调用带参数输出的COM接口
隐藏修改文件时间和文件属性的ASP脚本
ASP Crazy 模版操作类(最简单的模板类、仅提供交流)
asp 动态数组 提供Add、Insert、Remove、RemoveAt、Search等方法。
asp 取一个数的整数 但不是四舍五入,只要有小数,就取大于这个数的整数
asp 判断上传文件中是否存在危险代码
asp 获取url函数小结
ASP 调用dll及封装dll实例
asp 实现的冒泡排序程序
asp 自定义分段函数/求第N名成绩
ASP 高级模板引擎实现类
ASP 常见的连接字符串写法(access2007)
ASP向Excel导数据(图片)终结版 ASP操作Excel
ASP实现防止网站被采集代码

ASP 中的 制作一个简单的服务器端控制


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 35 ::
收藏到网摘: n/a

  2000082805.aspx
-------------------------------------------------------------
<%@ Register TagPrefix="CJ" Namespace="cjServerControls" %>
<html>
  <head>
  </head>
  <body>

    <form method="post" action="2000082605.aspx" runat="server">
<CJ:xmlDocReader  listText="au_id" listValue="au_id" runat="server"
path="C:\Inetpub\wwwroot\myASP\tutorials\authors.xml" />
    </form>

  </body>
</html>


2000082805a.cs
-------------------------------------------------------------
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace cjServerControls
{
public class xmlDocReader : Control, INamingContainer
{
string _xmlPath;
string _listID;
string _listValue;

public string Path
{
get { return _xmlPath; }
set { _xmlPath = value; }
}

public string listText
{
get { return _listID; }
set { _listID = value; }
}

public string listValue
{
get {return _listValue; }
set {_listValue = value; }
}

//Were are creating a member here so we can access it throughout the class
DropDownList _DDL;
DataGrid _DG;
FileStream fs;
StreamReader sr;
DataSet ds;
protected override void PreRender()
{
if (Page.IsPostBack) {
DataView dv = new DataView(ds.Tables[0]);
dv.RowFilter = listValue + "='" + _DDL.SelectedItem.Text + "'";
_DG = new DataGrid();
_DG.DataSource = dv;
_DG.DataBind();
Controls.Add(_DG);
}
}


protected override void CreateChildControls()
{
fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
ds = new DataSet();
ds.ReadXml(sr);
fs.Close();

_DDL = new DropDownList();
_DDL.DataTextField = listText;
_DDL.DataValueField = listValue;
_DDL.AutoPostBack = true;
_DDL.DataSource = ds.Tables[0].DefaultView;
_DDL.DataBind();
Controls.Add(_DDL);


}
}
}

authors.xml
-------------------------------------------------------------
<root>
<schema id="DocumentElement" targetNamespace="" xmlns="http://www.w3.org/1999/XMLSchema"
xmlns:xdo="urn:schemas-microsoft-com:xml-xdo" xdo:DataSetName="DocumentElement">
    <element name="Table">
        <complexType content="elementOnly">
            <element name="au_id" type="string"></element>
            <element name="au_lname" type="string"></element>
            <element name="au_fname" type="string"></element>
            <element name="phone" type="string"></element>
            <elem