MasterPage事件使用
生活随笔
收集整理的這篇文章主要介紹了
MasterPage事件使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1。創建一個MasterPage 命名為MasterPage2.master,并在頁面中放入測試控件 DropDownList1 和XmlDataSource1
代碼 <%@?Master?Language="C#"?AutoEventWireup="true"?CodeFile="MasterPage2.master.cs"?Inherits="MasterPage2"?%><!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>
????<asp:ContentPlaceHolder?id="head"?runat="server">
????</asp:ContentPlaceHolder>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
????????母版頁中的內容??<asp:ContentPlaceHolder?id="ContentPlaceHolder1"?runat="server">?</asp:ContentPlaceHolder>?
?????????<asp:DropDownList?ID="DropDownList1"?runat="server"?AutoPostBack="True"?
????????????????DataSourceID="XmlDataSource1"?DataTextField="text"?
????????????DataValueField="value"?
???????????>
????????????</asp:DropDownList>
????????<asp:XmlDataSource?ID="XmlDataSource1"?runat="server"?
????????????DataFile="~/DropDownListData.xml"></asp:XmlDataSource>
????</div>
????</form>
</body>
</html>
?
2.創建一個內容頁面Default.aspx,并放入測試控件DropDownList1,XmlDataSource1
?
代碼 <%@?Page?Title=""?Language="C#"?MasterPageFile="~/MasterPage2.master"?AutoEventWireup="true"?CodeFile="Default.aspx.cs"?Inherits="_Default"?%><%@?MasterType?VirtualPath="~/MasterPage2.master"?%>
<asp:Content?ID="Content1"?ContentPlaceHolderID="head"?Runat="Server">
</asp:Content>
<asp:Content?ID="Content2"?ContentPlaceHolderID="ContentPlaceHolder1"?Runat="Server">
????<asp:XmlDataSource?ID="XmlDataSource2"?runat="server"?
????DataFile="~/DropDownListData.xml"></asp:XmlDataSource>
內容頁:<asp:DropDownList?ID="DropDownList1"?runat="server"?AutoPostBack="True"?
????DataSourceID="XmlDataSource2"?DataTextField="text"?DataValueField="value"?
????????onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</asp:Content>
?
以上值得注意的是要加上<%@?MasterType?VirtualPath="~/MasterPage2.master"?%>, 這樣內容頁面就可以使用Master類中自定義的方法、屬性。。
?
3.MasterPage2.aspx.cs
?
代碼 using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
public?partial?class?MasterPage2?:?System.Web.UI.MasterPage
{
????protected?override?void?OnInit(EventArgs?e)
????{
????????base.OnInit(e);
????????DropDownList1.SelectedIndexChanged?+=?new?EventHandler(DropDownList1_SelectedIndexChanged); //注冊SelectedIndexChanged事件
????}
????void?DropDownList1_SelectedIndexChanged(object?sender,?EventArgs?e)
????{
????????OnContentChange(this,new?CommandEventArgs(DropDownList1.SelectedItem.Text,DropDownList1.SelectedItem.Value));?//觸發內容頁面注冊事件
????}
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????}
????public?void?ChangeIndex(int?index)??//
????{
????????DropDownList1.SelectedIndex?=?index;
????}
????public?event?CommandEventHandler?OnContentChange;??//定義事件,供內容頁面注冊
}
?
?4.Default.aspx.cs
?
?
代碼 using?System;using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
public?partial?class?_Default?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????}
????protected?override?void?OnInit(EventArgs?e)
????{
????????base.OnInit(e);
????????Master.OnContentChange?+=?new?CommandEventHandler(ChangeDropDownList);?//注冊事件
????}
????protected?void?DropDownList1_SelectedIndexChanged(object?sender,?EventArgs?e)
????{
????????Master.ChangeIndex(DropDownList1.SelectedIndex);//調用MasterPage頁面方法
????}
????void?ChangeDropDownList(object?send,?CommandEventArgs?e)??//當MasterPage中?DropDownList1的SelectIndexChanage觸發此方法
????{
????????string?selectvalue?=?e.CommandArgument.ToString();
????????DropDownList1.SelectedValue?=?selectvalue;
????}
}
?
?
附XML文件:
代碼 <?xml?version="1.0"?encoding="utf-8"??><root>
??<Empolyer?value="1"?text="lirong"/>
??<Empolyer?value="2"?text="aaaaaa"/>
??<Empolyer?value="3"?text="bbbbbb"/> ??<Empolyer?value="4"?text="cccccc"/> ??<Empolyer?value="5"?text="dddddd"/>
</root>
?
?
轉載于:https://www.cnblogs.com/paopaofan/archive/2010/01/11/1644084.html
總結
以上是生活随笔為你收集整理的MasterPage事件使用的全部內容,希望文章能夠幫你解決所遇到的問題。