您的位置:知识库 »

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)

作者: webabcd  发布时间: 2008-09-04 14:33  阅读: 9673 次  推荐: 0   原文链接   [收藏]  
[1] 温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)
[2] 温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)

系列文章导航:

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)

温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)

温故知新ASP.NET 2.0(C#)(2) - Themes(主题)

温故知新ASP.NET 2.0(C#)(3) - SiteMap(站点地图)

温故知新ASP.NET 2.0(C#)(4) - 缓存&SqlCacheDependency

温故知新ASP.NET 2.0(C#)(5) - Localization

温故知新ASP.NET 2.0(C#)(8) - DataSourceControl

温故知新ASP.NET 2.0(C#)(6) - Membership&RoleManager

温故知新ASP.NET 2.0(C#)(7) - Profile

温故知新ASP.NET 2.0(C#)(8) - DataSourceControl


MasterPage/MasterPage.master.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPage_MasterPage : System.Web.UI.MasterPage
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }


    
/// <summary>
    
/// 设置ddlMaster的选中索引
    
/// 这个方法由内容页调用
    
/// </summary>
    
/// <param name="index"></param>

    public void SetddlMaster(int index)
    
{
        ddlMaster.SelectedIndex 
= index;
    }


    
protected void ddlMaster_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
// ddlMaster的选中索引改变后,激发SelectedIndexChanged_ddlMaster事件
        SelectedIndexChanged_ddlMaster(thisnew CommandEventArgs(ddlMaster.SelectedItem.Text, ddlMaster.SelectedValue));
    }


    
// 声明一个公共时间事件,让内容页用
    public event CommandEventHandler SelectedIndexChanged_ddlMaster;
}


内容页
MasterPage/Test.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true"
    CodeFile
="Test.aspx.cs" Inherits="MasterPage_Test" Title="MasterPage测试" 
%>

<%--创建对母版页的强类型引用,并指定到母版页的虚拟路径--%>
<%@ MasterType VirtualPath="~/MasterPage/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cph" runat="Server">
    
<asp:dropdownlist id="ddlPage" runat="server" datasourceid="XmlDataSource1" datatextfield="text"
        datavaluefield
="value" autopostback="True" onselectedindexchanged="ddlPage_SelectedIndexChanged">
    
</asp:dropdownlist>
    
<asp:xmldatasource id="XmlDataSource1" runat="server" datafile="~/Config/DropDownListData.xml">
    
</asp:xmldatasource>
</asp:Content>


MasterPage/Test.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPage_Test : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
// 在内容页用FindControl方法找到内容页中的控件
        DropDownList ddl = new DropDownList();
        ddl 
= Master.Master.FindControl("ContentPlaceHolder1").FindControl("cph").FindControl("ddlPage"as DropDownList;
        Master.Master.FindControl(
"ContentPlaceHolder1").FindControl("cph").Controls.Add(new LiteralControl("<br />内容页中的DropDownList的ClientID是:" + ddl.ClientID));

        
// 增加一个事件处理,该事件是在母版页定义的一个公共事件
        Master.SelectedIndexChanged_ddlMaster += new CommandEventHandler(Master_SelectedIndexChanged_ddlMaster);
    }


    
void Master_SelectedIndexChanged_ddlMaster(object sender, CommandEventArgs e)
    
{
        
// CommandEventArgs已经在母版页中的公共事件“SelectedIndexChanged_ddlMaster”中指定
        string selectedText = e.CommandName;
        
string selectedValue = e.CommandArgument.ToString();

        ddlPage.SelectedValue 
= selectedValue;
    }


    
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
// 调用母版页的方法
        Master.SetddlMaster(ddlPage.SelectedIndex);
    }

}


[第1页][第2页]
0
0

热门文章

    最新文章

      最新新闻

        热门新闻