在线PhotoShop
http://uupoop.com/
In the previous chapter we created a UserControl, and now we will try using it for the first time. Pick a page in your project, or simply create a new one for the purpose, and open it. The first thing we have to do, is declare our UserControl. It can be done either in each page where it's used, or globally in the web.config file. There is no performance difference, but when declaring UserControls in the web.config file, the controls have to reside in a different directory than the page(s) using it.
For now, let's just declare it within the page. Add the following line below the standard page declaration:
for example:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HorizontalMenuControl.ascx.cs" Inherits="WebApplication3.HorizontalMenuControl" %> <style> .menu_hor_list { background-color: #dff3ff; height: 35px; padding: 0px; margin: 0px; border-bottom: solid 1px #ddd; padding-top: 5px; } .menu_hor_list ul { padding: ; position: absolute; left: 35px; margin: ; text-align: left; } .menu_hor_list ul li { display: block; list-style: none; float: left; margin: 0px; padding-right: 3px; height: 35px; line-height: 35px; } .menu_hor_list ul li a { padding: 0px 15px 0px 15px; margin-left: 0px; display: block; float: left; font-size: .1em; text-decoration: none; color: #; background-position: top left; background-repeat: no-repeat; } .menu_hor_list ul li a:hover { text-decoration: underline; color: #ff006e; } .menu_hor_list ul li .selected_label { text-decoration: none; background-color: #fff; background-position: top left; cursor: pointer; border-left: solid 1px #ddd; border-right: solid 1px #ddd; border-top: solid 1px #ddd; } </style> <div id="menuDiv" class="menu_hor_list" runat="server"> </div> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.BindMenus(); } } public string ParentMenuId { get; set; } public string SelectedMenuId { get; set; } private void BindMenus() { DataSet ds = GetChildMenus(ParentMenuId); const string NameField = "Name"; const string URLField = "URL"; const string IdField = "Id"; StringBuilder sb = new StringBuilder(); sb.Append("<ul>"); ].Rows) { string strClass = this.SelectedMenuId == Convert.ToString(dr[IdField]) ? "class='selected_label'" : ""; sb.Append(string.Format("<li><a {0} href='{1}'>{2}</a></li>", strClass, Convert.ToString(dr[URLField]), Convert.ToString(dr[NameField]))); } sb.Append("</ul>"); menuDiv.InnerHtml = sb.ToString(); } private DataSet GetChildMenus(string parentId) { BllMenu bm = new BllMenu(); return bm.GetChildMenus(parentId); }
<style> .menu_list ul { margin-left: 30px; } .menu_list ul li { list-style: none; line-height: 40px; background: url("icon_arrow_blue.png") no-repeat left center; text-indent: 1em; font-size: 16px; } .menu_list ul li a { color: #1cA7FF; } .menu_list ul li a:hover { text-decoration: underline; color: #ff006e; } </style> <div id="verticalMenuDiv" class="menu_list" runat="server"> </div> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.BindMenus(); } } public string ParentMenuId { get; set; } private void BindMenus() { DataSet ds = GetChildMenus(ParentMenuId); const string NameField = "Name"; const string URLField = "URL"; StringBuilder sb = new StringBuilder(); sb.Append("<ul>"); ].Rows) { sb.Append(string.Format("<li><a href='{0}'>{1}</a></li>", Convert.ToString(dr[URLField]), Convert.ToString(dr[NameField]))); } sb.Append("</ul>"); verticalMenuDiv.InnerHtml = sb.ToString(); } private DataSet GetChildMenus(string parentId) { BllMenu bm = new BllMenu(); return bm.GetChildMenus(parentId); }
If you look at the page now, you will see our UserControl in action, although the information will be a bit... limited. We will have to set a value for the properties we defined, for things to get just a bit more interestingly. Fortunately, it's very easy:
How to use:
<uc1:VerticalMenuControl runat="server" ID="VerticalMenuControl" ParentMenuId="1" />
<uc1:HorizontalMenuControl runat="server" ID="HorizontalMenuControl" ParentMenuId="1" SelectedMenuId="4" />
In the CodeBehind of the page, try something like this:
protected void Page_Load(object sender, EventArgs e) { // These values can come from anywhere, but right now, we just hardcode them MyUserInfoBoxControl.UserName = "Jane Doe"; MyUserInfoBoxControl.UserAge = ; MyUserInfoBoxControl.UserCountry = "Germany"; }