asp.net-母版页实用测试

母版页A_TEST.master代码如下:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="A_TEST.master.cs" Inherits="A_TEST" %>





    
    
    
    


    

母版页实例

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="母版页练习" %>






新建两个页面 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" %>





    
    



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);

    }
}

效果如图:

asp.net-母版页实用测试_第1张图片


你可能感兴趣的:(asp.net)