ASP.NET---用户控件

1. 用户控件

  • 创建用户控件:

                ASP.NET---用户控件_第1张图片

  • WebUserControl1.ascx界面布局

                ASP.NET---用户控件_第2张图片

    

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
    Inherits="WebApplication.WebUserControl1" %>



  • Default.aspx界面布局:

                ASP.NET---用户控件_第3张图片

  • 使用控件
               编写方法:

                       ASP.NET---用户控件_第4张图片

        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace WebApplication
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(TextBox1.Text);
        }
        public string Text
        {
            get
            {
                return TextBox1.Text;
            }
            set
            {
                TextBox1.Text = value;
            }
        }

        public void ForeColor(Color color)
        {
            TextBox1.ForeColor = color;
            Button1.BackColor = color;
        }


    }
}

            调用方法:

                       ASP.NET---用户控件_第5张图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace WebApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl11.Text = "一级页面";

            WebUserControl11.ForeColor(Color.Aqua);
        }
    }
}

你可能感兴趣的:(ASP.NET,网站开发)