在线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";
}

How to use usercontrol - pass paramters的更多相关文章

  1. [模拟电路] 2、Passive Band Pass Filter

    note: Some articles are very good in http://www.electronics-tutorials.ws/,I share them in the Cnblog ...

  2. 一种开发模式:ajax + ashx + UserControl

    一.ajax+ashx模式的缺点     在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...

  3. Wpf usercontrol dispose

    窗口关闭时组件"析构": public UserControl()        {            InitializeComponent();               ...

  4. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade

    XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...

  5. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  6. XAML UserControl的继承

    欢迎访问Heroius博客:崩溃的脑壳查看文章原文! 前言 相信不少学习WPF和Silverlight的同学们都出于Winform的习惯,希望能够在新展示层框架中实现控件的继承.本文就是说明如何实现这 ...

  7. wpf的UserControl用户控件怎么添加到Window窗体中

    转载自 http://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html 我们来新建一个用户控件UserControl1.xaml &l ...

  8. WPF 弹出UserControl

    UserControl正常情况下是不能被弹出的,而编写好的UserControl页面,为了查看效果,又需要弹出. 为了解决这个问题,UserControl需要一个Windows来接收. var win ...

  9. WPF中UserControl和DataTemplate

    最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...

随机推荐

  1. OpenCV 3.1 VS 2010 Cuda 7.5 TBB Configuration 配置

    Download OpenCV 3.1 Download OpenCV Extra Modules Download VS2010 Download CMake 3.2.0 Download Cuda ...

  2. 异常 java.util.regex.PatternSyntaxException:

    可变参数是在JDK1.5之后出来的一个行特性,也是一个比较好用的东西 想起好多jfinal还有其他框架的查询方法就有好多用到了可变参数,自己也写了个这样的方法 public class Test{   ...

  3. Oracle恢复删除数据 && connect by 树形结构查询

    1.一个表中根据以父子级别关系查询显示出来(如图) select t.* from department t CONNECT BY PRIOR t.depid=t.supdepid ; --这样也可以 ...

  4. Retina时代的前端视觉优化

    随着New iPad的发布,平板也将逐渐进入Retina时代,在高分辨率设备里图片的显示效果通常不尽人意,为了达到最佳的显示效果就需要对图片进行优化,这里介绍一些优化方法: 一.用CSS替代图片 这一 ...

  5. Mariadb 10.1 joiner节点加入报错WSREP: Failed to prepare for incremental state transfer

    Mariadb 10.1 galera cluster 集群joiner 节点加入集群 会出现这种报错,导致mysql一直点点点,这里我贴出报错.2016年04月19日13:34:58 2016-04 ...

  6. js 所有事件列表

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  7. gcc和g++

    一.GCC GNU编译器套件(GNU Compiler Collection)包括C.C++.Objective-C.Fortran.Java.Ada和Go语言的前端,也包括了这些语言的库(如libs ...

  8. 【iCore3 双核心板】例程二十:LAN_TCPC实验——以太网数据传输

    实验指导书及代码包下载: http://pan.baidu.com/s/1pJY5uXH iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  9. app与后台通信协议

    通用的语言有很多种,例如英语和中文,在网络的通讯中,通用的协议有很多,其中http是被最广泛使用的.如果是私有的协议,那就只能自己设计了. 用http是最方便的,如果是私有协议,包含协议的封装和拆解, ...

  10. bootstrap加深

    1.安装: bootstrap中文网:http://www.bootcss.com/ bootstrap.css样式:http://v3.bootcss.com/css/#tables class=' ...