博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在网页上显示用户控件上学选择的内容(例子省市选择器) .
阅读量:6966 次
发布时间:2019-06-27

本文共 4991 字,大约阅读时间需要 16 分钟。

要求:

新建 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为其值"
    Xοnclick="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 ";
            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" "">

<html xmlns="">

<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/archive/2012/12/25/2832674.html

你可能感兴趣的文章
Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题
查看>>
httpd/php/mysql的安装-1
查看>>
终极版:由简单工厂模式,升级到抽象工厂模式(用到反射)
查看>>
LintCode: O(1) Check Power of 2
查看>>
sysbench 测试MYSQL
查看>>
putty如何退出全屏模式
查看>>
c# 异步编程demo (async await)
查看>>
命令行參数选项处理:getopt()及getopt_long()函数使用
查看>>
OSS设置CORS规则以后还是报No 'Access-Control-Allow-Origin'解决方法
查看>>
opengl 教程(24) shadow mapping (2)
查看>>
数据库——浅谈数据库中的存储过程(转)
查看>>
html学习一(html简史及doctype)
查看>>
Castle IOC容器与Spring.NET配置之比较
查看>>
[Javascript] Call Stack
查看>>
单表60亿记录等大数据场景的MySQL优化和运维之道
查看>>
Linux zip解压/压缩并指定目录
查看>>
Ubuntu下安装MySQL 5.6.23
查看>>
Codeforces Round #261 (Div. 2)——Pashmak and Buses
查看>>
kafka源码分析之一server启动分析
查看>>
【CodeForces 626C】Block Towers
查看>>