制作用户自定义控件(xml生成)
时间:2007-06-28 来源:chenqiubite
首先生成一个xml文件:(源文件menu.xml)
<?xml version="1.0"?>
<Items>
<Item>
<ID>User</ID>
<Text>用户管理</Text>
<Link>UserList.aspx</Link>
</Item>
<Item>
<ID>Column</ID>
<Text>新闻管理</Text>
<Link>ColumnList.aspx</Link>
</Item>
<Item>
<ID>Resource</ID>
<Text>新闻图片管理</Text>
<Link>ResourceList.aspx</Link>
</Item>
<Item>
<ID>Remark</ID>
<Text>新闻评论管理</Text>
<Link>RemarkList.aspx</Link>
</Item>
<Item>
<ID>default</ID>
<Text>返回首页</Text>
<Link>../default.aspx</Link>
</Item>
<Item>
<ID>logout</ID>
<Text>退出</Text>
<Link>login.aspx?logout=true</Link>
</Item>
</Items> 在IDE里添加新项(Web 用户控件)生成leftmenu.ascx文件: 采用Repeater控件,如下: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="leftmenu.ascx.cs" Inherits="manage_leftmenu" %>
<table cellSpacing="0" cellPadding="9" width="180" border="0">
<TBODY>
<tr>
<td style="
<table cellSpacing="0" cellPadding="0" width="162" border="0">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td class="content"><%#DataBinder.Eval(Container.DataItem,"LeadTag")%><A runat="server" id="link" title="" href='<%#DataBinder.Eval(Container.DataItem,"Link")%>'>
<asp:Label id="itemLabel" runat="server" CssClass="blue">
<%#DataBinder.Eval(Container.DataItem,"Text")%>
</asp:Label></A>
</td>
</tr>
<tr>
<td><IMG height="4" src="images/s.gif" width="1" border="0"></td>
</tr>
<tr>
<td background="images/bgimg.gif"><IMG height="1" src="images/s.gif" width="1" border="0"></td>
</tr>
<tr>
<td><IMG height="4" src="images/s.gif" width="1" border="0"></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</td>
</tr>
</TBODY>
</table> 编辑leftmenu.ascx.cs文件: using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class manage_leftmenu : System.Web.UI.UserControl
{
private string currColumn;
private string xmlsrc; public string Src
{
set
{
xmlsrc = value;
}
} public string Column
{
set
{
currColumn = value;
}
} protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (xmlsrc != null)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(xmlsrc)); // 增加新列
DataTable dt = ds.Tables[0];
DataColumn dc;
dc = new DataColumn("isCurrItem", Type.GetType("System.Boolean"));
dt.Columns.Add(dc);
dc = new DataColumn("LeadTag", Type.GetType("System.String"));
dt.Columns.Add(dc); // 预处理数据
DataRow dr;
for (int i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
dr["isCurrItem"] = (dr["ID"].ToString() == currColumn);
if ((Boolean)(dr["Text"].ToString()[0] == '-'))
{
dr["LeadTag"] = " ";
dr["Text"] = dr["Text"].ToString().Remove(0, 1);
}
else
{
dr["LeadTag"] = "<IMG src='images/arrow_b.gif' border='0'> ";
}
} Repeater1.DataSource = ds;
Repeater1.DataBind();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound); }
#endregion protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{ }
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView currDataItem = (DataRowView)e.Item.DataItem;
Label currLabel = (Label)e.Item.FindControl("itemLabel");
if ((Boolean)currDataItem["isCurrItem"])
{
currLabel.CssClass = "yellowcontent1";
}
}
} 这样一个用户自定义的控件就完成了。 如何使用呢? 在一个页面(.aspx)里托进这个letfmenu.ascx文件:(会生成相关代码) <uc1:leftmenu ID="Leftmenu1" runat="server" Src="menu.xml" /> 注意一定添加这个src部分。
<Items>
<Item>
<ID>User</ID>
<Text>用户管理</Text>
<Link>UserList.aspx</Link>
</Item>
<Item>
<ID>Column</ID>
<Text>新闻管理</Text>
<Link>ColumnList.aspx</Link>
</Item>
<Item>
<ID>Resource</ID>
<Text>新闻图片管理</Text>
<Link>ResourceList.aspx</Link>
</Item>
<Item>
<ID>Remark</ID>
<Text>新闻评论管理</Text>
<Link>RemarkList.aspx</Link>
</Item>
<Item>
<ID>default</ID>
<Text>返回首页</Text>
<Link>../default.aspx</Link>
</Item>
<Item>
<ID>logout</ID>
<Text>退出</Text>
<Link>login.aspx?logout=true</Link>
</Item>
</Items> 在IDE里添加新项(Web 用户控件)生成leftmenu.ascx文件: 采用Repeater控件,如下: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="leftmenu.ascx.cs" Inherits="manage_leftmenu" %>
<table cellSpacing="0" cellPadding="9" width="180" border="0">
<TBODY>
<tr>
<td style="
<table cellSpacing="0" cellPadding="0" width="162" border="0">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td class="content"><%#DataBinder.Eval(Container.DataItem,"LeadTag")%><A runat="server" id="link" title="" href='<%#DataBinder.Eval(Container.DataItem,"Link")%>'>
<asp:Label id="itemLabel" runat="server" CssClass="blue">
<%#DataBinder.Eval(Container.DataItem,"Text")%>
</asp:Label></A>
</td>
</tr>
<tr>
<td><IMG height="4" src="images/s.gif" width="1" border="0"></td>
</tr>
<tr>
<td background="images/bgimg.gif"><IMG height="1" src="images/s.gif" width="1" border="0"></td>
</tr>
<tr>
<td><IMG height="4" src="images/s.gif" width="1" border="0"></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</td>
</tr>
</TBODY>
</table> 编辑leftmenu.ascx.cs文件: using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class manage_leftmenu : System.Web.UI.UserControl
{
private string currColumn;
private string xmlsrc; public string Src
{
set
{
xmlsrc = value;
}
} public string Column
{
set
{
currColumn = value;
}
} protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (xmlsrc != null)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(xmlsrc)); // 增加新列
DataTable dt = ds.Tables[0];
DataColumn dc;
dc = new DataColumn("isCurrItem", Type.GetType("System.Boolean"));
dt.Columns.Add(dc);
dc = new DataColumn("LeadTag", Type.GetType("System.String"));
dt.Columns.Add(dc); // 预处理数据
DataRow dr;
for (int i = 0; i < dt.Rows.Count; i++)
{
dr = dt.Rows[i];
dr["isCurrItem"] = (dr["ID"].ToString() == currColumn);
if ((Boolean)(dr["Text"].ToString()[0] == '-'))
{
dr["LeadTag"] = " ";
dr["Text"] = dr["Text"].ToString().Remove(0, 1);
}
else
{
dr["LeadTag"] = "<IMG src='images/arrow_b.gif' border='0'> ";
}
} Repeater1.DataSource = ds;
Repeater1.DataBind();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound); }
#endregion protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{ }
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView currDataItem = (DataRowView)e.Item.DataItem;
Label currLabel = (Label)e.Item.FindControl("itemLabel");
if ((Boolean)currDataItem["isCurrItem"])
{
currLabel.CssClass = "yellowcontent1";
}
}
} 这样一个用户自定义的控件就完成了。 如何使用呢? 在一个页面(.aspx)里托进这个letfmenu.ascx文件:(会生成相关代码) <uc1:leftmenu ID="Leftmenu1" runat="server" Src="menu.xml" /> 注意一定添加这个src部分。
相关阅读 更多 +