泡泡饭

导航

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>

 

 

posted on 2010-01-11 14:18  泡泡饭  阅读(313)  评论(0编辑  收藏  举报