在网页上显示用户控件上学选择的内容(例子省市选择器) .

要求:

新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮。

在Demo1.aspx页面中引用 ProvincCity控件。并且加入button1和Lable1控件。

当点击Demo1中的button时,在lable1中显示用户在provinceCity控件中选择的省和市。

使用两种方法,实现当点击用户控件中的button1时,让demo1中的lable显示ProvinceCity控件中 drowpdownList2中选中的内容。

在Demo2中直接调用用户控件。

用户控件:

citySelect.ascx文件:

<%@ Control Xlanguage="C#" AutoEventWireup="true" CodeBehind="citySelect.ascx.cs" Inherits="用户自定义控件.citySelect" %>
省份:<asp:DropDownList ID="DropDownList1" runat="server"
    AutoPostBack="True" Xonselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="获取页面中的label为其值"
    Xonclick="Button1_Click" />

citySelect.ascx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace 用户自定义控件
{
    public partial class citySelect : System.Web.UI.UserControl
    {
       
        public event CitySelectHandler GetCitySelect;      //用委托定义对应类型的事件

        public string GetProvinceCity
        {
            get { return "省份是:" + this.DropDownList1.SelectedItem.Text + "  城市是:" + this.DropDownList2.SelectedItem.Text; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindprovince();
            }
           
        }

        private void bindprovince()
        {
            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {

                string sql = "select provinceID,province from province";
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    con.Open();
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataTextField = "province";
                    DropDownList1.DataValueField = "provinceID";
                    DropDownList1.DataBind();
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DropDownList2.Items.Clear();
            string Pid = this.DropDownList1.SelectedValue;
            string sql = "select cityId,city from city where father=@Pid";
            SqlParameter P1 = new SqlParameter("@Pid", Pid);

            string constr = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    cmd.Parameters.Add(P1);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    con.Open();
                    DropDownList2.DataSource = dt;
                    DropDownList2.DataTextField = "city";
                    DropDownList2.DataValueField = "cityID";
                    DropDownList2.DataBind();
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Label lbl = this.Parent.FindControl("Label1") as Label;
            //lbl.Text =  this.DropDownList2.SelectedItem.Text;


            //当用户控件中的button1按钮被单击的时候,激发GetCitySelect事件
            if (GetCitySelect!=null)
            {
                 GetCitySelect(this, DropDownList2.SelectedItem.Text);
            }
          
        }
    }


    public delegate void CitySelectHandler(object sender,string selectCity);  //委托
}

Demo1.asox.cs文件:

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

namespace 用户自定义控件
{
    public partial class Demo1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //增加用户控件citySelect.ascx的事件处理程序
            this.citySelect1.GetCitySelect += new CitySelectHandler(citySelect1_GetCitySelect);
        }

        //按Tab建实例化委托,并生成一个方法。
        void citySelect1_GetCitySelect(object sender, string selectCity)
        {
            this.Label1.Text = selectCity;
            this.TextBox1.Text = selectCity;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
          #region 方法一  FindControl方法
            //DropDownList ddl1 = this.citySelect1.FindControl("DropDownList1") as DropDownList;

            //DropDownList ddl2 = this.citySelect1.FindControl("DropDownList2") as DropDownList;
            //this.Label1.Text = "省份是:" + ddl1.SelectedItem.Text + "  城市是:" + ddl2.SelectedItem.Text;
          #endregion
          
            #region 方法二 在用户控件中定义属性,这里调用属性获取省市

            this.Label1.Text = this.citySelect1.GetProvinceCity;
            #endregion

        }
    }
}

Demo2.aspx文件:

<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="Demo2.aspx.cs" Inherits="用户自定义控件.Demo2" %>

<%@ Register src="citySelect.ascx" tagname="citySelect" tagprefix="uc1" %>

<!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>
   
        <uc1:citySelect ID="citySelect1" runat="server" />
   
    </div>
    </form>
</body>
</html>
Demo2.aspx.cs文件:

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

namespace 用户自定义控件
{
    public partial class Demo2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.citySelect1.GetCitySelect += new CitySelectHandler(citySelect1_GetCitySelect);
        }

        void citySelect1_GetCitySelect(object sender, string selectCity)
        {
            this.Response.Write(selectCity);
        }
    }
}

原文地址:https://www.cnblogs.com/duanlinlin/p/2832674.html