惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 思然

邮件带图片附件 - 思然 - 博客园 XML查找节点 - 思然 - 博客园 动态生成rdlc 报表(原创) 图片处理 - 思然 - 博客园 数据绑定及其他 - 思然 - 博客园 图片上传及网络相册功能 SQL Server 2005 Express 使用心得 - 思然 附加进程调试和存储过程调试 (转)DIV CSS布局教程:应用ul、li实现表格形式 缓存之缓存文件依赖及编程方式设置输出缓存过期 防刷新多次提交 枚举的应用 添加div符号注意符号问题 Datalist调用本地文件绑定图片 用户控件-TreeView的用法 关于table控件的一个疑难问题(涉及循环) 关于javascript调用webservices的中文参数乱码的问题 服务器端和客户端清除TextBox控件的值 获取母版页的控件的方法
用户控件的使用经验
思然 · 2008-07-01 · via 博客园 - 思然

用户控件的一些注意的地方
    用户控件最好放在一个文件夹中便于管理而且如果在Web.Config中全局注册某用户控件的话,是不允许和使用的页面在同一目录下
用户控件最好具有一定的扩展性和重载性
所以一般我们需要为某用户控件赋于一些属性或者事件下面是一个简单的例子
 public partial class TabStrip : System.Web.UI.UserControl
{
    public event EventHandler TabClick;
    public int SelectIndex
    {
        get { return DataList1.SelectedIndex; }
        set { DataList1.SelectedIndex=value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> tabs = new List<string>();
            tabs.Add("Products");
            tabs.Add("Services");
            tabs.Add("About");
            DataList1.DataSource = tabs;
            DataList1.DataBind();
            DataList1.SelectedIndex = 0;
          
        }
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (TabClick != null)
            TabClick(this, EventArgs.Empty);
    }
}

用户控件页面

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TabStrip.ascx.cs" Inherits="TabStrip" %>
  <div>
        <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" SelectedItemStyle-CssClass="SelectTab">
        <ItemTemplate>
        <asp:LinkButton ID="lnkTab" runat="server" Text="<%#Container.DataItem%>" CommandName="select"></asp:LinkButton>
        </ItemTemplate>
        </asp:DataList>
       </div>调用用户控件的页面
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:TabStrip ID="TabStrip1" runat="server" OnTabClick="TabStrip1_click" />
        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
            <asp:View ID="View1" runat="server">
                Most Product</asp:View>
            <asp:View ID="View2" runat="server">
                Best Services</asp:View>
            <asp:View ID="View3" runat="server">
                Beter Company</asp:View>
        </asp:MultiView></div>
    </form>
</body>
</html>
cs 文件
    protected void TabStrip1_click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = TabStrip1.SelectIndex;
    }

还可以动态加载用户控件但是如果你用户控件中有你自己的定义的属性的话需要在页面引用

<%@ Reference Control="~/Dy.ascx" %>
<%@ Reference Control="~/Dy2.ascx" %>
这样就可以在页面中调用
两个用户控件

public partial class Dy2 : System.Web.UI.UserControl
{
    public bool NetBol
    {
        get { return CheckBox1.Checked; }
    }
    public bool SqlBol
    {
        get { return CheckBox2.Checked; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

public partial class Dy : System.Web.UI.UserControl
{
    public bool NetBol
    {
        get { return CheckBox1.Checked; }
    }
    public bool SqlBol
    {
        get { return CheckBox2.Checked; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

调用的页面

public partial class Default2 : System.Web.UI.Page
{
    private Control _surey = null;
    protected void Page_Load(object sender, EventArgs e)
    {

        switch (DropDownList1.SelectedIndex)
        {
            case 1:
                _surey = Page.LoadControl("Dy.ascx");
                break;
            case 2:
                _surey = Page.LoadControl("Dy2.ascx");
                break;

        }
        if (_surey != null)
            PlaceHolder1.Controls.Add(_surey);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        switch (DropDownList1.SelectedIndex)
        {
            case 1:
                Dy net = (Dy)_surey;
                Label1.Text = "<h1>Net</h1>";
                Label1.Text += "<br/>know net"+net.NetBol.ToString();
                Label1.Text += "<br/>know SQl" + net.SqlBol.ToString();
                break;
            case 2:
                Dy2 java=(Dy2)_surey;
                Label1.Text = "<h1>Java</h1>";
                Label1.Text += "<br/>know java" + java.NetBol;
                Label1.Text += "<br/>know oracle" + java.SqlBol;
                break;
        }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Reference Control="~/Dy.ascx" %>
<%@ Reference Control="~/Dy2.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem Value="0">请选择</asp:ListItem>
            <asp:ListItem Value="1">Net</asp:ListItem>
            <asp:ListItem Value="2">java</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>