关闭顶部展开顶部

解读26个ASP.Net开发新手的常见问题(2)_.Net教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:浅析ASp.Net自定义验证码控件
最近自己写了一个自定义验证码控件把它拿出来和大家分享分享 具体步骤 1---》新建asp.net 网站 2---》添加新建项目 ,选择类库 3---》新建两个类 3.1--》自定义控件类(WebC

以下为引用的内容:

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='';");
}

8.关于日期格式

以下为引用的内容:

  日期格式设定

DataFormatString="{0:yyyy-MM-dd}"

  我觉得应该在itembound事件中

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

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

以下为引用的内容:

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

  e.g

// in global.asax
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");

//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
}

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

10.清空Cookie

以下为引用的内容:

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

11.自定义异常处理

以下为引用的内容:

//自定义异常处理类
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
}

12.Panel 横向滚动,纵向自动扩展

〈asp:panel style="overflow-x:scroll;overflow-y:auto;">〈/asp:panel>

13.回车转换成Tab

分享:解读ASP.NET网站程序防SQL注入式攻击方法
一、什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令。在某些表单中,用户输入的内容直接用来

来源:模板无忧//所属分类:.Net教程/更新时间:2009-09-01
相关.Net教程
闂佹眹鍩勯崹閬嶆偤閺囶澁缍栭柛鈩冪⊕閳锋帗銇勯弴妤€浜惧銈忕秶閹凤拷
濠电偛顕慨顓㈠磻閹炬枼妲堥柡鍌濇硶婢ф稒淇婇懠顒夆偓婵嬫煟閵忊晛鐏查柟鍑ゆ嫹
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径瀣閻庣懓瀚竟鍡欐崲娑斾線鏌i姀鈺佺伈闁瑰嚖鎷�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径濠勵吋闂佽鍨庨崟顓фК闂佽閰eḿ褍螞濞戙垺鍋夐柨鐕傛嫹
闂備胶枪缁绘劙骞婃惔銊ョ劦妞ゆ帒鍊哥敮鍫曞箹鐎涙ḿ鐭掔€规洘绻堥弫鎾绘晸閿燂拷
闂備胶枪缁绘劙骞婃惔銊ョ劦妞ゆ巻鍋撻柛姘儑缁﹪鏁傞崜褏鐓撻柣搴岛閺呮繈鎯屽▎鎴犵=濞撴艾锕ョ€氾拷
闂備浇銆€閸嬫挻銇勯弽銊р槈闁伙富鍣i弻娑樷攽閹邦亞鑳虹紓浣靛妽濡炶棄顕i妸鈺婃晬婵炲棙鍨电粭锟犳⒑閸濆嫬鈧骞婇幘鑸殿潟闁跨噦鎷�
闂備礁鎼崯鐗堟叏妞嬪海绀婂鑸靛姈閻擄綁鎮规潪鎷岊劅婵炲眰鍊曢湁闁挎繂妫欑粈鈧梺鍛娚戦悧鐘茬暦閹扮増鏅搁柨鐕傛嫹
婵犵妲呴崹顏堝礈濠靛棭鐔嗘俊顖氬悑鐎氱粯銇勯幘瀵哥畺閻庢熬鎷�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓奸崶銊ヮ伕濡炪倖鎸荤换鍐偓姘虫珪娣囧﹪顢涘Δ鈧晶鍙夌節椤喗瀚�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎规洦鍓熼、娆撳礂閻撳簶鍋撻悽鍛婄厸闁割偅绻勫瓭婵犳鍣幏锟�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾闁哄苯鎳橀崺鈧い鎺嗗亾闁宠閰i獮鎴﹀箛闂堟稒顔嗛梻浣告惈鐎氭悂骞忛敓锟�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎规洩缍侀獮瀣攽閸偂绱�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径濠勭厬闂佺懓鐡ㄧ换鍕敂鐎涙ü绻嗘い鏍殔婢у弶绻濋~顔藉
闂佽楠搁崢婊堝礈濠靛鍋嬮柟鎯版閻鈹戦悩鎻掓殭闁奸潧缍婇弻銈夋嚍閵夈儱顫嶉梺缁樼壄缂嶄礁鐣峰▎鎾存櫢闁跨噦鎷�
UB闂備礁婀辩划顖炲礉濡ゅ懐宓侀柛銉㈡櫆鐎氭岸鎮楀☉娅虫垿锝為敓锟�
闂備浇澹堟ご绋款潖婵犳碍鐒鹃悗鐢电《閸嬫捇鐛崹顔句痪濠电姭鍋撻柨鐕傛嫹
闂佽楠哥粻宥夊垂閸濆嫸鑰块柛銏㈠殰
闂備礁鎲″缁樻叏妞嬪海绀婂璺虹灱閸楁碍绻涢崱妤€顒㈤柛鐐差槹缁绘稓绱欓悩鍝勫帯闂佺ǹ楠忛幏锟�
缂傚倸鍊烽悞锕傛偡閿曞倸鍨傛繝濠傚椤╅攱銇勯幒宥囶槮缂佹彃婀遍埀顒傚仯閸婃繄绱撳棰濇晩闁跨噦鎷�
©2017 www.mb5u.com婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎殿噮鍣i幃鈺呭箵閹烘挸鐦�
闂備浇銆€閸嬫捇鏌熼婊冾暭妞ゃ儻鎷�&闂備礁鎲$敮鎺懳涢弮鍫燁棅闁跨噦鎷�