母版页A_TEST.master代码如下:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="A_TEST.master.cs" Inherits="A_TEST" %>
<!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>
<style type="text/css">
#div_head{ text-align:center; background-color:Gray; height:60px;}
#div_body{ width:750px;background-color:Green;}
#div_left{ float:left; width:700px;background-color:Orange; height:500px;}
#div_right{ float:right; width:400px; height:500px; background-color:Orange; }
#div_right div{ padding:10px 5px 10px 5px; border-bottom:1px dotted silver; }
#div_right div a{ display:block; float:right;
padding:3px; border:1px solid blank; text-decoration:none;}
#div_right div a:hover{ color:Black;}
.clr{ clear:both;}
#div_foot { height:50px; background-color:Olive;}
</style>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div><div id="div_head"><h1>母版页实例</h1></div>
<div id="div_main">
<div id="div_left">
<asp:ContentPlaceHolder id="holder_main" runat="server">
</asp:ContentPlaceHolder>
</div>
<div id="div_right" runat="server"></div>
<div class="clr"></div>
</div>
<div id="div_foot"></div>
</div>
</form>
</body>
</html>
A_TEST.master.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class A_TEST : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Panel pnl_item;
Label lab_fname;
HyperLink hlink_alltext, hlink_lines;
string[] sarr_fname = Directory.GetFiles(MapPath("files"), "*.txt");
foreach (string _s in sarr_fname) {
lab_fname = new Label();
lab_fname.Text = Path.GetFileName(_s);
hlink_alltext = new HyperLink();
hlink_alltext.Text = "全部读取";
hlink_alltext.NavigateUrl = "readalltext.aspx?fname="+lab_fname.Text;
hlink_lines = new HyperLink();
hlink_lines.Text = "分行读取";
hlink_lines.NavigateUrl = "readalllines.aspx?fname=" + lab_fname.Text;
pnl_item = new Panel();
pnl_item.Controls.Add(lab_fname);
pnl_item.Controls.Add(hlink_alltext);
pnl_item.Controls.Add(hlink_lines);
div_right.Controls.Add(pnl_item);
}
}
}
母版页首页.aspx代码如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/A_TEST.master" AutoEventWireup="true" CodeFile="母版页练习.aspx.cs" Inherits="母版页练习" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="holder_main" Runat="Server">
</asp:Content>
新建两个页面 readalltext.aspx和readalllines.aspx。设置母版页为A_TEST.master
readalltext.aspx代码如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/A_TEST.master" AutoEventWireup="true" CodeFile="readalltext.aspx.cs" Inherits="readalltext" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="holder_main" Runat="Server">
<asp:TextBox ID="txt_content" runat="server" Height="225px" Width="254px" TextMode="MultiLine" Rows="13"></asp:TextBox>
<asp:Button ID="btn_save" runat="server" Text="保存" οnclick="btn_save_Click" />
</asp:Content>
readalltext.aspx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class readalltext : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
if (Request.QueryString["fname"] != null)
{
string _path = Path.Combine(MapPath("files"), Request["fname"]);
if (File.Exists(_path))
{
txt_content.Text = File.ReadAllText(_path);
}
else { Response.Redirect("母版页练习.aspx"); }
}
else { Response.Redirect("母版页练习.aspx"); }
}
}
protected void btn_save_Click(object sender, EventArgs e)
{
string _path = Path.Combine(MapPath("files"), Request["fname"]);
File.WriteAllText(_path, txt_content.Text);
}
}
效果如图: