母版页练习

来源:互联网 发布:南宁软件培训 编辑:程序博客网 时间:2024/06/01 22:41
.新建 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>
&nbsp;城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="20px"
Width="126px">
</asp:DropDownList>
<p>
&nbsp;</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="显示城市" />
&nbsp;
<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>
&nbsp;城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="20px"
Width="126px">
</asp:DropDownList>
<p>
&nbsp;</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 + "&nbsp;&nbsp;&nbsp;" + 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;
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 怀孕干活累着了怎么办 怀孕了上班很累怎么办 孕妇胖的太快怎么办 孕妇长得太快怎么办 眼睛一按吱吱响怎么办 孕期太胖了怎么办啊 人流后子宫复位不好怎么办 怀孕初期有盆腔积液怎么办 怀孕了有盆腔积液怎么办 多囊怀孕不想要怎么办 6个月婴儿大小眼怎么办 健身教练岁数大了以后怎么办 超变战陀玩具手柄坏了怎么办 飓风战魂三陀螺中轴坏了怎么办 怎么办晚安角和铁陀螺 白衣服染上荧光剂了怎么办 指尖陀螺不亮了怎么办 手指陀螺不转了怎么办 月经推迟私处还老是流水怎么办 苹果手机刷机后忘记id密码怎么办 锤基意外怀孕怎么办零6 职场遇到心机婊怎么办 高二会考没过怎么办 保险柜没电了打不开怎么办 保险柜没有电了打不开怎么办 小保险箱没电了怎么办 bim墙的颜色反了怎么办 眼睛大但是无神怎么办 吃了凉的胃难受怎么办 吃凉东西胃疼怎么办 游戏只有一个分辨率选项怎么办 玩游戏心态易崩怎么办 打游戏心态炸了怎么办 赛鸽比赛回来拉稀怎么办 鸽子拉竹节水便怎么办 新买的鸽子拉稀怎么办 信鸽羽毛上长了虫子怎么办 羊肉煮熟了太硬怎么办 切菜不小心切到手指怎么办 打荷盘子端错了怎么办 学厨师不会翻锅怎么办?