您的位置:首页 > 编程语言 > ASP

asp.net母版页实现文本阅读实例

2014-12-06 12:00 531 查看
.master代码如下:

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

<!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>
    <script src="Jquery172.js" type="text/javascript"></script>
    <script type="text/javascript">//动态效果
        $(document).ready(function () {
            $("#div_right div").mouseover(function () {//鼠标移入显示
                $("a", this).show();
            });

            $("#div_right div").mouseout(function () {//鼠标移出隐藏
                $("a", this).hide();
            });

        });
    
    </script>

    <style type="text/css">
        body{ width:750px; margin:0px auto;}
        h2{ text-align:center; border-bottom:1px dotted silver; padding-bottom:20px;}
        .clr{ clear:both;}
        #div_head {}
        #div_left { float:left; }
        #div_right { float:right; width:270px;}
           #div_right div
           { padding:10px 0px 10px 0px;
             border-bottom:1px dotted silver; 
                
               }
           #div_right div a
           { display:block; float:right; 
             padding:3px; border:1px solid black; 
             font-size:12px; text-decoration:none; margin-left:5px; 
             display:none;  }
           #div_right div a:hover
            { color:#850000;}  
        
        #div_foot { height:30px; background-color:#dedede; border:1px solid gray;}
        
        
    </style>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="div_head">
          <h2>  简易文本阅读编辑</h2>
        </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>




.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_spl_02 : 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("txtfiles"), "*.txt");
        foreach (string _s in sarr_fname) {
            lab_fname = new Label();
            <span style="color:#ff0000;">lab_fname.Text = Path.GetFileName(_s);</span>

            hlink_alltext = new HyperLink();
            hlink_alltext.Text = "全部读取";
            hlink_alltext.NavigateUrl = "spl_02_readalltext.aspx?fname=" + lab_fname.Text;//QueryString 方式传值

            hlink_lines = new HyperLink();
            hlink_lines.Text = "分行读取";
            hlink_lines.NavigateUrl = "spl_02_readallline.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);
        }

       

    }
}


readalltext.aspx代码如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/A_spl_02.master" AutoEventWireup="true" CodeFile="spl_02_readalltext.aspx.cs" Inherits="spl_02_readalltext" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
   </asp:Content>
<span style="color:#ff0000;"><asp:Content ID="Content2" ContentPlaceHolderID="holder_main" Runat="Server">
    <asp:TextBox ID="txt_content" runat="server" Width="400px" TextMode="MultiLine"
     Rows="13"></asp:TextBox><br />
    <asp:Button ID="btn_save" runat="server" Text="保存修改" onclick="btn_save_Click" />
</asp:Content></span>




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 spl_02_readalltext : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            if (Request.QueryString["fname"] != null)
            {
                <span style="color:#ff0000;">string _path = Path.Combine(MapPath("txtfiles"),Request["fname"]);</span>
                if (File.Exists(_path))
                {
                    txt_content.Text = File.ReadAllText(_path);
                }
                else { Response.Redirect("spl_02_default.aspx"); }

            }
            else { Response.Redirect("spl_02_default.aspx"); }
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        <span style="color:#ff0000;">string _path = Path.Combine(MapPath("txtfiles"), Request["fname"]);</span>
        File.WriteAllText(_path, txt_content.Text);
    }
}


readallline.aspx代码如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/A_spl_02.master" AutoEventWireup="true" CodeFile="spl_02_readallline.aspx.cs" Inherits="spl_02_readallline" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        #txts_holder input { <pre name="code" class="html"><span style="color:#ff0000;">display:block;</span>
width:400px;}
<span style="color: rgb(255, 0, 0);">注释:display:block;可以自动换行</span>
</style></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="holder_main" Runat="Server" > <div id="txts_holder" style ="width:400px;" runat="server" clientidmode="Static">
</div> <asp:Button ID="btn_save" runat="server" Text="保存修改" onclick="btn_save_Click" /></asp:Content>




readallline.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 spl_02_readallline : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string _path = getpath();
        string[] sarr_content = File.ReadAllLines(_path);

        TextBox _txt;
        foreach (string _s in sarr_content) {
            _txt = new TextBox();
            _txt.Text = _s;
            txts_holder.Controls.Add(_txt);
        }

    }

    protected string getpath() {
        string _path = "";
        if (Request["fname"] == null) { Response.Redirect("spl_02_default.aspx"); }
        _path = Path.Combine(MapPath("txtfiles"), Request["fname"]);
        if (!File.Exists(_path)) { Response.Redirect("spl_02_default.aspx"); }
        
        return _path;
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string _content = "";

        foreach (Control _ctl in txts_holder.Controls) {
            if (_ctl is TextBox) { _content += ((TextBox)_ctl).Text + '\u0081'; }//先将所有字符串合并成大的字符串,并用'\u0081'隔开

            
        }

        _content = _content.Substring(0, _content.Length - 1);//去除最后一个<span style="font-family: Arial, Helvetica, sans-serif;">'\u0081'</span>

        string[] _lines = _content.Split('\u0081');//字符串分成字符串数组
        string _path = Path.Combine(MapPath("txtfiles"), Request["fname"]);
        File.WriteAllLines(_path, _lines);

    }
}


显示效果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: