母版页练习题
.新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮。
在Demo1.aspx页面中引用 ProvincCity控件。并且加入button1和Lable1控件。
当点击Demo1中的button时,在lable1中显示用户在provinceCity控件中选择的省和市。
使用两种方法,实现当点击用户控件中的button1时,让demo1中的lable显示ProvinceCity控件中 drowpdownList2中选中的内容。
母版页前台:
<%@ Control Xlanguage="C#" AutoEventWireup="true" CodeBehind="selectCity.ascx.cs" Inherits="_12_25.selectCity" %>
省份:<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="True" Height="20px"
Xonselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="126px">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="20px"
Width="126px">
</asp:DropDownList>
<p>
</p>
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click"
Text="获取页面中label中的值" />
母版页后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace _12_25
{
public partial class selectCity : System.Web.UI.UserControl
{
public event CitySelectHandler GetCitySelect;//根据委托定义了一个对应类型的事件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownListBind();
}
}
private void DropDownListBind()
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select provinceID,province from province";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
}
}
public string returnDDl
{
get { return "您选择的是:" + DropDownList1.SelectedItem.Text + "省" + DropDownList2.SelectedItem.Text + "市"; }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
string str = this.DropDownList1.SelectedValue;
cmd.CommandText = "select cityID,city from city where
father=@id";
cmd.Parameters.Add(new SqlParameter("id",str));
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "cityID";
DropDownList2.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Label lab = this.Parent.FindControl("Label1") as Label;
//lab.Text = "您选择的省市为:" + DropDownList1.SelectedItem.Text+" "+DropDownList2.SelectedItem.Text;
//当用户单击Button1时激发GetCitySelect
if (this.DropDownList2.SelectedValue.ToString()!="")
{
GetCitySelect(this,DropDownList2.SelectedItem.Text);
}
}
}
public delegate void CitySelectHandler(object sender,string selectCity); //添加一个委托
}
Demo1前台:
<form id="form1" runat="server">
<div>
<uc1:selectCity ID="selectCity1" runat="server" />
<br /><br /><br /><br />
<asp:Button ID="btnShowCity" runat="server" Xonclick="btnShowCity_Click"
Text="显示城市" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
Demo1后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _12_25
{
public partial class Demo1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//增加用户控件CitySelect.ascx的事件处理程序
this.selectCity1.GetCitySelect += new CitySelectHandler(selectCity1_GetCitySelect);
}
void selectCity1_GetCitySelect(object sender, string selectCity)//按Tab键实例化委托,并生成一个方法
{
this.Label1.Text = selectCity;
}
protected void btnShowCity_Click(object sender, EventArgs e)
{
#region 方法1 在自定义的的控件中用FindControl查找DropDownList
//DropDownList ddl = this.selectCity1.FindControl("DropDownList1") as DropDownList;
//DropDownList ddl1 = this.selectCity1.FindControl("DropDownList2") as DropDownList;
//Label1.Text = "您选择的是:"+ddl.SelectedItem.Text+"省"+ddl1.SelectedItem.Text+"市";
#endregion
#region 在母版页中自定义属性
Label1.Text = this.selectCity1.returnDDl;
#endregion
}
}
}
4新建 demo2.aspx,引用ProvinceCity,当点击 provinceCity中的button时,在页面中显示选择的省和市。
母版页前台:
<%@ Control Xlanguage="C#" AutoEventWireup="true" CodeBehind="selectCity.ascx.cs" Inherits="_12_25.selectCity" %>
省份:<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="True" Height="20px"
Xonselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="126px">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="20px"
Width="126px">
</asp:DropDownList>
<p>
</p>
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click"
Text="获取页面中label中的值" />
母版页后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace _12_25
{
public partial class selectCity : System.Web.UI.UserControl
{
public event CitySelectHandler GetCitySelect;//根据委托定义了一个对应类型的事件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownListBind();
}
}
private void DropDownListBind()
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select provinceID,province from province";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
}
}
public string returnDDl
{
get { return "您选择的是:" + DropDownList1.SelectedItem.Text + "省" + DropDownList2.SelectedItem.Text + "市"; }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.Clear();
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
string str = this.DropDownList1.SelectedValue;
cmd.CommandText = "select cityID,city from city where
father=@id";
cmd.Parameters.Add(new SqlParameter("id",str));
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "cityID";
DropDownList2.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.DropDownList2.SelectedValue.ToString()!="")
{
string str = DropDownList1.SelectedItem.Text + " " + DropDownList2.SelectedItem.Text;
GetCitySelect(this,str );
}
}
}
public delegate void CitySelectHandler(object sender,string selectCity); //添加一个委托
}
Demo2前台:
<form id="form1" runat="server">
<div>
<uc1:selectCity ID="selectCity1" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
Demo2后台:
protected void Page_Load(object sender, EventArgs e)
{
//增加用户控件CitySelect.ascx的事件处理程序
this.selectCity1.GetCitySelect += new CitySelectHandler(selectCity1_GetCitySelect);
}
void selectCity1_GetCitySelect(object sender, string selectCity)//按Tab键实例化委托,并生成一个方法
{
this.Label1.Text = selectCity;
}