用户控件的使用经验

用户控件的一些注意的地方
    用户控件最好放在一个文件夹中便于管理而且如果在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>

你可能感兴趣的:(用户)