⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 srcctrl.ascx.cs

📁 ASP.NET2.0(C#篇)经典教程的源码...本源码很好的实现了购物车....编码规范和类的设计具有很好的借鉴性!
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Security.Permissions;
using System.Text.RegularExpressions;
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 SrcCtrl : System.Web.UI.UserControl
{
    public String displayFilename;
    private bool _showdisplayFilename = true;
    private Int32 _fontsize = 3;
    const string TAG_FNTRED = "<font color= \"red\">";
    const string TAG_FNTBLUE = "<font color= \"blue\">";
    const string TAG_FNTGRN = "<font color= \"green\">";
    const string TAG_FNTMRN = "<font color=\"maroon\">";
    const string TAG_EFONT = "</font>";

    public Boolean ShowFileName
    {
        get
        {
            return _showdisplayFilename;
        }
        set
        {
            _showdisplayFilename = value;
        }
    }

    public Int32 FontSize
    {
        get
        {
            return _fontsize;
        }
        set
        {
            _fontsize = value;
        }
    }

    protected override void Render(HtmlTextWriter output)
    {

        string err_message = "<p><b>Source Viewer Error: cannot show this displayFile</b>";

        try
        {
            if (displayFilename != null)
            {
                Trace.Write("SrcCtrl", displayFilename);

               if (ShowFileName)
                    Response.Write("<h3>" + new FileInfo(displayFilename).Name + "</h3>");

                FileStream fs = new FileStream(displayFilename, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                StringWriter textBuffer = new StringWriter();
                String sourceLine;

                if (_fontsize > 5)
                {
                    textBuffer.Write("<font size=\"" + _fontsize + "\"><b>\r\n");
                }
                else
                {
                    textBuffer.Write("<font size=\"" + _fontsize + "\">\r\n");
                }

                if ((displayFilename.ToLower()).EndsWith(".cs"))
                {
                    textBuffer.Write("<pre>\r\n");
                    while ((sourceLine = sr.ReadLine()) != null)
                    {
                        textBuffer.Write(FixCSLine(sourceLine));
                        textBuffer.Write("\r\n");
                    }
                    textBuffer.Write("</pre>");
                }
                else if ((displayFilename.ToLower()).EndsWith(".js"))
                {
                    textBuffer.Write("<pre>\r\n");
                    while ((sourceLine = sr.ReadLine()) != null)
                    {
                        textBuffer.Write(FixJSLine(sourceLine));
                        textBuffer.Write("\r\n");
                    }
                    textBuffer.Write("</pre>");
                }
                else if ((displayFilename.ToLower()).EndsWith(".cs"))
                {
                    textBuffer.Write("<pre>\r\n");
                    while ((sourceLine = sr.ReadLine()) != null)
                    {
                        textBuffer.Write(FixVBLine(sourceLine));
                        textBuffer.Write("\r\n");
                    }
                    textBuffer.Write("</pre>");
                }
                else
                {
                    String lang = "VB";
                    bool isInScriptBlock = false;
                    bool isInMultiLine = false;

                    textBuffer.Write("<pre>\r\n");
                    while ((sourceLine = sr.ReadLine()) != null)
                    {
                        // First we want to grab the global language
                        // for this page by a Page directive.  Or
                        // possibly from a script block.
                        lang = GetLangFromLine(sourceLine, lang);
                        if (IsScriptBlockTagStart(sourceLine))
                        {
                            textBuffer.Write(FixAspxLine(sourceLine));
                            isInScriptBlock = true;
                        }
                        else if (IsScriptBlockTagEnd(sourceLine))
                        {
                            textBuffer.Write(FixAspxLine(sourceLine));
                            isInScriptBlock = false;
                        }
                        else if (IsMultiLineTagStart(sourceLine) && !isInMultiLine)
                        {
                            isInMultiLine = true;
                            textBuffer.Write("<font color=blue><b>" + HttpUtility.HtmlEncode(sourceLine));
                        }
                        else if (IsMultiLineTagEnd(sourceLine) && isInMultiLine)
                        {
                            isInMultiLine = false;
                            textBuffer.Write(HttpUtility.HtmlEncode(sourceLine) + "</b></font>");
                        }
                        else if (isInMultiLine)
                        {
                            textBuffer.Write(HttpUtility.HtmlEncode(sourceLine));
                        }
                        else
                        {
                            if (isInScriptBlock == true)
                            {
                                if (lang.ToLower() == "c#")
                                {
                                    textBuffer.Write(FixCSLine(sourceLine));
                                }
                                else if (lang.ToLower() == "vb")
                                {
                                    textBuffer.Write(FixVBLine(sourceLine));
                                }
                                else if (lang.ToLower() == "jscript" || lang.ToLower() == "javascript")
                                {
                                    textBuffer.Write(FixJSLine(sourceLine));
                                }
                            }
                            else
                            {
                                textBuffer.Write(FixAspxLine(sourceLine));
                            }
                        }
                        textBuffer.Write("\r\n");
                    }
                    textBuffer.Write("</pre>");
                }
                if (_fontsize > 5)
                {
                    textBuffer.Write("</b></font>\r\n");
                }
                else
                {
                    textBuffer.Write("</font>\r\n");
                }

                Response.Write(textBuffer.ToString());

                fs.Close();
            }
        }
        catch (Exception e)
        {
            Response.Write(err_message + ": " + e.ToString());
        }
    }
    string GetLangFromLine(string sourceLine, string defLang)
    {
        if (sourceLine == null)
        {
            return defLang;
        }

        Match langMatch = Regex.Match(sourceLine, "(?i)<%@\\s*Page\\s*.*Language\\s*=\\s*\"(?<lang>[^\"]+)\"");
        if (langMatch.Success)
        {
            return langMatch.Groups["lang"].ToString();
        }

        langMatch = Regex.Match(sourceLine, "(?i)(?=.*runat\\s*=\\s*\"?server\"?)<script.*language\\s*=\\s*\"(?<lang>[^\"]+)\".*>");
        if (langMatch.Success)
        {
            return langMatch.Groups["lang"].ToString();
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -