`
evget
  • 浏览: 144573 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
文章分类
社区版块
存档分类

ASP.NET Web开发实用代码举例(二)

阅读更多
文章关键字:|ASP.NET|Web|开发|代码|举例|传递|参数|

1.表格超连接列传递参数

<asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id='
<%# DataBinder.Eval(Container.DataItem, "数据字段1")%>' & name='<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>' />

2.表格点击改变颜色

if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
    e.Item.Attributes.Add("onclick","this.style.backgroundColor='#99cc00';
      this.style.color='buttontext';this.style.cursor='default';");
}

写在DataGrid的_ItemDataBound里

if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#99cc00';
    this.style.color='buttontext';this.style.cursor='default';");
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='';this.style.color='';");
}

3.关于日期格式

日期格式设定

DataFormatString="{0:yyyy-MM-dd}"
我觉得应该在itembound事件中

e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))

4.获取错误信息并到指定页面

不要使用Response.Redirect,而应该使用Server.Transfer

// in global.asax
protected void Application_Error(Object sender, EventArgs e) {
    if (Server.GetLastError() is HttpUnhandledException)
    Server.Transfer("MyErrorPage.aspx");//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了:)
}
其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了。

Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理。

5.清空Cookie

Cookie.Expires=[DateTime];
Response.Cookies("UserName").Expires = 0

6.自定义异常处理

//自定义异常处理类 
using System;
using System.Diagnostics;
namespace MyAppException
{
    /// <summary>
    /// 从系统异常类ApplicationException继承的应用程序异常处理类。
    /// 自动将异常内容记录到Windows NT/2000的应用程序日志
    /// </summary>
    public class AppException:System.ApplicationException
    {
        public AppException(){
            if (ApplicationConfiguration.EventLogEnabled)LogEvent("出现一个未知错误。");
        }
        public AppException(string message){
            LogEvent(message);
        }
        public AppException(string message,Exception innerException)
        {
            LogEvent(message);
            if (innerException != null){
                LogEvent(innerException.Message);
            }
        }
    }
}

//日志记录类
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
namespace MyEventLog
{
/// <summary>
/// 事件日志记录类,提供事件日志记录支持 
/// <remarks>
/// 定义了4个日志记录方法 (error, warning, info, trace) 
/// </remarks>
/// </summary>
public class ApplicationLog
{
    /// <summary>
    /// 将错误信息记录到Win2000/NT事件日志中
    /// <param name="message">需要记录的文本信息</param>
    /// </summary>
    public static void WriteError(String message){
        WriteLog(TraceLevel.Error, message);
    }

    /// <summary>
    /// 将警告信息记录到Win2000/NT事件日志中
    /// <param name="message">需要记录的文本信息</param>
    /// </summary>
    public static void WriteWarning(String message){
        WriteLog(TraceLevel.Warning, message);
    }

    /// <summary>
    /// 将提示信息记录到Win2000/NT事件日志中
    /// <param name="message">需要记录的文本信息</param>
    /// </summary>
    public static void WriteInfo(String message){
        WriteLog(TraceLevel.Info, message);
    }

    /// <summary>
    /// 将跟踪信息记录到Win2000/NT事件日志中
    /// <param name="message">需要记录的文本信息</param>
    /// </summary>
    public static void WriteTrace(String message){
        WriteLog(TraceLevel.Verbose, message);
    }

    /// <summary>
    /// 格式化记录到事件日志的文本信息格式
    /// <param name="ex">需要格式化的异常对象</param>
    /// <param name="catchInfo">异常信息标题字符串.</param>
    /// <retvalue>
    /// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para>
    /// </retvalue>
    /// </summary>
    public static String FormatException(Exception ex, String catchInfo){
        StringBuilder strBuilder = new StringBuilder();
        if (catchInfo != String.Empty){
            strBuilder.Append(catchInfo).Append("\r\n");
        }
        strBuilder.Append(ex.Message).Append("\r\n").Append(ex.StackTrace);
        return strBuilder.ToString();
    }

    /// <summary>
    /// 实际事件日志写入方法
    /// <param name="level">要记录信息的级别(error,warning,info,trace).</param>
    /// <param name="messageText">要记录的文本.</param>
    /// </summary>
    private static void WriteLog(TraceLevel level, String messageText){
        try{ 
        EventLogEntryType LogEntryType;
        switch (level)
        {
            case TraceLevel.Error:
                LogEntryType = EventLogEntryType.Error;
                break;
            case TraceLevel.Warning:
                LogEntryType = EventLogEntryType.Warning;
                break;
            case TraceLevel.Info:
                LogEntryType = EventLogEntryType.Information;
                break;
            case TraceLevel.Verbose:
                LogEntryType = EventLogEntryType.SuccessAudit;
                break;
            default:
                LogEntryType = EventLogEntryType.SuccessAudit;
                break;
            }
            EventLog eventLog = new EventLog("Application", ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName );
            //写入事件日志
            eventLog.WriteEntry(messageText, LogEntryType);
        }
        catch {} //忽略任何异常
        } 
    } //class ApplicationLog
}



未完,原文地址:http://www.evget.com/zh-CN/Info/ReadInfo.aspx?id=9309
分享到:
评论

相关推荐

    asp.net 自定义下拉多选控件

    在ASP.NET开发中,我们经常需要使用到各种控件来增强用户界面的交互性。"asp.net 自定义下拉多选控件"就是一个这样的组件,它允许用户在下拉菜单中进行多选操作,极大地提高了数据输入的效率。这个控件是基于流行的...

    【ASP.NET编程知识】推荐8项提高 ASP.NET Web API 性能的技术.docx

    提高 ASP.NET Web API 性能的 8 项技术 ASP.NET Web API 是一个流行的框架,用于构建 Web 应用程序。然而,在实际开发中,我们经常遇到性能问题。为此,我们将介绍 8 项提高 ASP.NET Web API 性能的技术。 1. 使用...

    用户登录举例 源代码asp.net

    在ASP.NET中,用户登录是Web应用程序的基本功能之一,...总之,“用户登录举例”这个资源提供了实践和学习ASP.NET用户登录功能的宝贵材料,通过深入研究和理解这些代码,你可以掌握构建安全、高效登录系统的关键技术。

    ASP.NET书稿源代码.rar

    ASP.NET是一种由微软开发的服务器端Web应用程序框架,用于构建动态网站、Web应用程序和Web服务。这个名为"ASP.NET书稿源代码.rar"的压缩包文件包含了一系列与ASP.NET相关的学习资源,主要分为三个部分,涵盖了从基础...

    ASP.Net网络编程实用教程

    - **ASP.NET的优越性**:讨论ASP.NET相比其他Web开发技术的优势。 - **用ASP.NET编制WebForm页面基础**:介绍如何创建和处理WebForm页面,包括按钮、表单和文件操作。 #### 第4章:ASP.NET对象 - **ASP.NET内置...

    ASP.NET通用数据库访问组件

    ASP.NET是微软开发的一种服务器端Web应用程序框架,用于构建动态网站、Web应用和Web服务。它基于.NET Framework,提供了丰富的功能和工具,支持多种编程语言,如C#、VB.NET等。 在ASP.NET中,数据库访问通常涉及ADO...

    最经典的130道ASP.NET面试题

    ASP.NET是微软公司开发的一种用于构建Web应用程序的框架,它基于.NET Framework,为开发者提供了丰富的功能和工具,简化了Web应用的开发流程。这本"最经典的130道ASP.NET面试题"集锦涵盖了ASP.NET的核心概念、编程...

    asp.net面试题

    ASP.NET 是微软公司推出的一种基于.NET Framework的Web应用程序开发框架,它为开发人员提供了一种高效、安全且可扩展的平台来构建动态网站、Web应用和Web服务。本压缩包包含的是关于ASP.NET的面试题集,对于求职者...

    ASP.NET WEB应用程序设计教程(单维锋编著) 图书例子代码-校园音乐吧项目(c#)

    本书基于Visual Studio 2008集成开发工具,系统全面地介绍了使用最新ASP.NET 3.5技术设计、开发和部署Web网站的相关知识。 全书共14章,内容包括Web应用程序概念、HTML、DHTML、 C#语言基础、ADO.NET、服务器端标准...

    ASP.Net 学生管理系统 (sql2008+vs2008)

    这个系统是用ASP.NET 3.5框架开发的,这是一款由微软提供的强大且灵活的工具,用于构建动态网站、Web应用和Web服务。配合SQL Server 2008作为后端数据库,它提供了高效的数据存储和检索能力,而Visual Studio 2008则...

    asp.net控件MultiView

    ASP.NET中的`MultiView`控件是Web表单开发中的一种强大工具,它允许在一个单一的用户界面(UI)中展示多个视图或步骤。在Web应用程序中,尤其是在处理多步骤表单或需要切换不同视图的情况下,`MultiView`控件显得...

    零基础学ASP.NET 2.0电子书&源代码绝对完整版1

    WebSite文件夹 创建的ASP.NET 2.0 Web站点。 www文件夹 第一个用C#开发的Web应用程序。 bianyi.bat 编译网站的批处理文件。 form.html 表单范例。 css.html CSS范例。 第3章...

    ASP.NET页面之间传递值的几种方法

    ASP.NET 页面之间传递值的几种方法 在 ASP.NET 中,页面之间传递值是非常常见的操作。下面我们将讨论几种常见的方法。 一、使用 QueryString 使用 QueryString 是 ASP.NET 页面之间传递值的一种常见方法。这是一...

    asp.net从入门到精通

    通过以上章节的学习,读者能够系统地掌握ASP.NET的基本概念、开发工具、语言基础以及面向对象的设计思路,并深入了解ASP.NET网页的代码模型和生命周期管理。这些知识为深入学习ASP.NET提供了坚实的基础。

    asp.net专家疑难解答200问

    第2章 ASP.NET运行模型 21.如何在页面中应用javascript脚本-示例1 21.如何在页面中应用javascript脚本-示例2 22.如何实现从服务器端向页面动态添加javascript脚本-示例1 22.如何实现从服务器端向...

    零基础学ASP.NET 2.0&源代码绝对完整版1

    示例描述:本章演示ASP.NET 2.0网站的预编译以及学习ASP.NET 2.0的前置知识。 WebSite文件夹 创建的ASP.NET 2.0 Web站点。 www文件夹 第一个用C#开发的Web应用程序。 bianyi.bat 编译网站的批处理文件。 form...

    asp.net3.5从入门到精通

    ### ASP.NET 3.5 从入门到精通 #### 第一篇 .NET 基础 ##### 第1章 认识ASP.NET 3.5 **1.1 什么是ASP.NET** ...后续章节将提供更多关于如何构建高效、可维护的ASP.NET Web应用程序的实用技巧和案例分析。

    ASP.NET在线测评系统

    二、开发工具及数据库管理系统 主要基于Internet技术同时兼顾Window应用来实现软件的创建、部署、使用的.net框架是微软21世纪主推的开发平台,并且,也是微软下一代操作系统策略的核心。 相比于JAVA是一个标准,...

    Silverlight 4 RIA开发全程解析(完整版)

    使用《SilverLight 4 RIA开发全程解析》所阐述的在线业务的新特性,您可以创建一个与传统的基于ASP.NET的网站相比响应速度大为提高的Web应用程序。为了了解这些新特性以及改进的Silverlight工具包,我们在每一章中都...

Global site tag (gtag.js) - Google Analytics