母板页练习
来源:互联网 发布:适合程序员的电脑壁纸 编辑:程序博客网 时间:2024/05/21 21:01
1.在母版中设置属性,内容也中通过母版页的属性获取和赋值母版页中的div的内容。
----母版页前台---------
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="BaodiaoMaster.master.cs" Inherits="母版页.BaodiaoMaster" %>
<!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>
<link href="Sheet1.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<%--<img src="images/t.jpg" />--%>
<asp:Image ID="Image1" runat="server" ImageUrl="images/t.jpg" />
<asp:Button ID="BtnRegister" runat="server" Text="注册" />
<div id="divTitle" runat="server"></div>
<div id="container">
<div id="header">
<h1>保钓网</h1><p>钓鱼岛是中国的!决不允许列强侵犯!</p>
<div id="navi">
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/index.aspx">首页</asp:HyperLink> |
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/AboutBaodiao.aspx">关于我们</asp:HyperLink> |
<a href='<%=ResolveClientUrl("~/About.aspx") %>'>新闻旧事</a> |
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="~/Demo1.aspx">联系我们</asp:HyperLink> |
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="~/loginBaodiao.aspx">登陆</asp:HyperLink>
</div>
</div>
<div id="content"><hr />
<div id="leftSiderBar">
<ul>主要内容
<li>首页</li>
<li>关于我们</li>
<li>新闻旧事</li>
<li>联系我们</li>
</ul>
<ul>友情链接
<li>宝岛军事</li>
<li>军事天地</li>
<li>铁血军事</li>
<li>百姓军事</li>
</ul>
</div>
<div id="mainContentWrap">
<div id="mainContent">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<p> 新闻:</p>
<p>自钓鱼岛领土纠纷产生以来,两岸三地及海外华人前后成立了数个颇具影响的保钓组织,其中最早的一个成立于1970年,美国普林斯顿大学的台湾留学生在当时组成了“保卫钓鱼台行动委员会”,对保钓活动产生深远影响。在此之后的数十年间,华人还成立了世界各地的“中国民间保钓联合会”、“世界华人保钓联盟”等颇具影响力的保钓组织,这些组织的领导者们也策划了包括游行、示威、登船出海到钓鱼岛及其附属岛屿海域以及登陆上岛在内的种种保钓活动。</p>
<p>1970年由美国普林斯顿的台湾留学生组织成立,是最早成立的华人保钓组织,该组织表示“反对美日私相受授”、“外抗强权,内争主权”,一方面抨击美国与日本,另一方面也要求中华民国政府应该力争主权。该组织也对保钓运动的组织构成产生了深远影响。</p>
<p>1971年2月,由香港教师、学生所成立。1996年9月,该组织的陈毓祥等五人于钓鱼台海域跳海示威,当中陈毓祥遇溺身亡,该事件也引发了港台两地民众的极大反弹,并在陈毓祥举殡当日组织的大规模保钓行动中成功登陆钓鱼岛。
</p>
</asp:ContentPlaceHolder> </div>
</div>
</div>
<div id="footer">
版权所有@copyright:中国保钓联合会Email:Baodiao.com.cn<asp:Label ID="lblMaster" runat="server" Text="初始值"></asp:Label>
</div>
</div>
</form>
</body>
</html>
-------母版页后台-----
namespace 母版页
{
public partial class BaodiaoMaster : System.Web.UI.MasterPage
{
public string DivContent
{
get { return this.divTitle.InnerText; }
set { this.divTitle.InnerText = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
this.lblMaster.Text = DateTime.Now.ToShortTimeString();
}
}
}
------loginBaodiao.aspx-------
<%@ Page Title="" Language="C#" MasterPageFile="~/BaodiaoMaster.Master" AutoEventWireup="true" CodeBehind="loginBaodiao.aspx.cs" Inherits="母版页.loginBaodiao" %>
<%@ MasterType VirtualPath="~/BaodiaoMaster.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.style1
{
width: 137px;
}
.style2
{
width: 242px;
}
table{ width:600px; height:400px; border-bottom-width:thin; border-bottom-color:Aqua; border-bottom-style:dashed;}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button3" runat="server" Text="设置母版中div的innserText"
onclick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="获取" onclick="Button4_Click"
/>
<table style="width: 600px;">
<tr>
<td class="style1">
用户名:</td>
<td class="style2">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
密 码:</td>
<td class="style2">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
<asp:Button ID="Button1" runat="server" Text="登陆" />
</td>
<td class="style2">
<asp:Button ID="Button2" runat="server" Text="注册" />
</td>
<td>
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>
-----loginBaodiao.aspx.cs------
namespace 母版页
{
public partial class loginBaodiao : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "保钓网登陆";
}
protected void Button3_Click(object sender, EventArgs e)
{
this.Master.DivContent = "欢迎注册";
}
protected void Button4_Click(object sender, EventArgs e)
{
this.Label1.Text = this.Master.DivContent;
}
}
}
2.新建MyMenu.ascx用户控件,在defaul,about,writeBlog,viewBlog,changeMessage。中分别使用。
-------MyMenu.ascx------------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyMenu.ascx.cs" Inherits="用户自定义控件.MyMenu" %>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="default.aspx">主页</asp:HyperLink> |
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="WritePassage.aspx">发表文章</asp:HyperLink>  |
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="ViewBlog.aspx">日志浏览</asp:HyperLink>  |
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl="changePassword.aspx">修改密码</asp:HyperLink>  |
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="About.aspx">关于我们</asp:HyperLink>
----------defaul---------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="用户自定义控件._default" %>
<%@ Register Src="~/MyMenu.ascx" TagName="netMenu" TagPrefix="hbsi"%>
<!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>
<hbsi:netMenu id="netMenu1" runat="server" ></hbsi:netMenu>
</div>
</form>
</body>
</html>
----------about-----------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="用户自定义控件.About" %>
<%@ Register src="MyMenu.ascx" tagname="MyMenu" 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:MyMenu ID="MyMenu1" runat="server" />
</div>
</form>
</body>
</html>
-------writeBlog-------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WritePassage.aspx.cs" Inherits="用户自定义控件.WritePassage" %>
<%@ Register src="MyMenu.ascx" tagname="MyMenu" 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:MyMenu ID="MyMenu1" runat="server" />
</div>
</form>
</body>
</html>
------viewBlog-------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewBlog.aspx.cs" Inherits="用户自定义控件.ViewBlog" %>
<%@ Register src="MyMenu.ascx" tagname="MyMenu" 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:MyMenu ID="MyMenu1" runat="server" />
</div>
</form>
</body>
</html>
-------changeMessage-----
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="changePassword.aspx.cs" Inherits="用户自定义控件.changePassword" %>
<%@ Register src="MyMenu.ascx" tagname="MyMenu" 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:MyMenu ID="MyMenu1" runat="server" />
</div>
</form>
</body>
</html>
3.新建 ProvinceCity用户控件。在该控件 中增加一个Button1按钮。
在Demo1.aspx页面中引用 ProvincCity控件。并且加入button1和Lable1控件。
当点击Demo1中的button时,在lable1中显示用户在provinceCity控件中选择的省和市。
使用两种方法,实现当点击用户控件中的button1时,让demo1中的lable显示ProvinceCity控件中 drowpdownList2中选中的内容。
------CitySelect.ascx-----------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CitySelect.ascx.cs" Inherits="用户自定义控件.CitySelect" %>
省份:<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" Height="25px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="146px">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="19px"
Width="132px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="获取页面中Lable,为其赋值"
onclick="Button1_Click" />
<p>
</p>
--------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 conn = new SqlConnection(constr))
{
string sql = "select provinceID,province from province";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
conn.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 p = new SqlParameter("@pid",pid);
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(p);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
conn.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,this.DropDownList2.SelectedItem.Text);
}
}
}
public delegate void CitySelectHandler(object sender,string selectCity);//定义一个委托
}
-------demo1.aspx----------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo1.aspx.cs" Inherits="用户自定义控件.Demo1" %>
<%@ 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" />
<asp:Button ID="Button1" runat="server" Text="以上选择的城市是:"
onclick="Button1_Click" /><asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
-------demo1.aspx.cs--------
namespace 用户自定义控件
{
public partial class Demo1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CitySelect1.GetCitySelect += new CitySelectHandler(CitySelect1_GetCitySelect);
//增加用户控件CitySelect.ascx的事件处理程序
}
void CitySelect1_GetCitySelect(object sender, string selectCity)//按Tab键实例化委托,并生成一个事件处理程序
{
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;
//this.Label1.Text = "省份:" + ddl1.SelectedItem.Text;
//DropDownList ddl2 = this.CitySelect1.FindControl("DropDownList2") as DropDownList;
//this.Label1.Text += "城市:" + ddl2.SelectedItem.Text;
#endregion
#region 方法二 在用户控件中定义属性,这里调用属性获取省市
this.Label1.Text = this.CitySelect1.GetProvinceCity;
#endregion
}
}
}
4新建 demo2.aspx,引用ProvinceCity,当点击 provinceCity中的button时,在页面中显示选择的省和市。
------CitySelect.ascx-----------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CitySelect.ascx.cs" Inherits="用户自定义控件.CitySelect" %>
省份:<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" Height="25px"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="146px">
</asp:DropDownList>
城市:<asp:DropDownList ID="DropDownList2" runat="server" Height="19px"
Width="132px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="获取页面中Lable,为其赋值"
onclick="Button1_Click" />
<p>
</p>
--------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 conn = new SqlConnection(constr))
{
string sql = "select provinceID,province from province";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
conn.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 p = new SqlParameter("@pid",pid);
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(p);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
conn.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, "省份:" + this.DropDownList1.SelectedItem.Text + "城市:" + this.DropDownList2.SelectedItem.Text);
}
}
}
public delegate void CitySelectHandler(object sender,string selectCity);//定义一个委托
}
-------demo2.aspx---------
<%@ Page Language="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-----
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);
}
}
}
- 母板页练习
- 母板页
- 母板页
- 母板页运行顺序
- asp 母板页
- 关于母板页
- 操作母板页
- 内容页 母板页 传值
- 母板页下路径问题
- 动态的加载母板页
- VS 2005 母板页应用
- 新建及使用母板页
- 复合控件与母板页
- master 母板页的使用
- 母板页中的content标签
- 嵌套母板页简单应用
- SharePoint2010母板页的介绍
- 母板页与内容页传值
- SharePoint 2013 配置 查询规则(Query Rule)
- linux下调试方法记录
- WAP站开发之3G版、触屏版跳转验证工具
- VC 6.0怎么只能同时打开一个工程?
- 使用ContentProvider共享数据
- 母板页练习
- lua 类与继承
- C++11 'yield' is not a member of 'std::this_thread'
- windows环境下通过添加静态路由信息,实现多网卡环境下,指定某网络由某网卡来连接
- iSCSI CHAP认证不完全攻略
- zoj 3348 网络流 汇总
- iOS中XML解析 (一) TBXML (实例:打印xml内容及存储到数组)
- 分享一个两年前写的JMS&ActiveMQ入门的ppt
- JSON