asp.net 编程 实用语句(6条)
1.panel 横向滚动,纵向自动扩展
<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>
2.回车转换成tab
(1)
<script language="javascript" for="document" event="onkeydown">
if(event.keycode==13 && event.srcelement.type!='button' && event.srcelement.type!='submit' && event.srcelement.type!='reset' && event.srcelement.type!=''&& event.srcelement.type!='textarea');
event.keycode=9;
</script>
(2) //当在有keydown事件的控件上敲回车时,变为tab
public void tab(system.web .ui.webcontrols .webcontrol webcontrol)
{
webcontrol.attributes .add ("onkeydown", "if(event.keycode==13) event.keycode=9");
}
3.datagrid超级连接列
datanavigateurlfield="字段名" datanavigateurlformatstring="http://xx/inc/delete.aspx?id={0}"
4.自定义异常处理
//自定义异常处理类
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
}
5.datagrid行随鼠标变色
private void dgzf_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
if (e.item.itemtype!=listitemtype.header)
{
e.item.attributes.add( "onmouseout","this.style.backgroundcolor=\""+e.item.style["background-color"]+"\"");
e.item.attributes.add( "onmouseover","this.style.backgroundcolor=\""+ "#eff3f7"+"\"");
}
}
6.模板列
<asp:templatecolumn visible="false" sortexpression="demo"
headertext="id">
<itemtemplate>
<asp label text='<%# databinder.eval(container.dataitem,
"articleid")%>' runat="server" width="80%"
id="lblcolumn" />
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="选中">
<headerstyle wrap="false" horiz></headerstyle>
<itemtemplate>
<asp:checkbox id="chkexport" runat="server" />
</itemtemplate>
<edititemtemplate>
<asp:checkbox id="chkexporton" runat="server"
enabled="true" />
</edititemtemplate>
</asp:templatecolumn>
后台代码
protected void checkall_checkedchanged(object sender, system.eventargs e)
{
//改变列的选定,实现全选或全不选。
checkbox chkexport ;
if( checkall.checked)
{
foreach(datagriditem odatagriditem in mydatagrid.items)
{
chkexport = (checkbox)odatagriditem.findcontrol("chkexport");
chkexport.checked = true;
}
}
else
{
foreach(datagriditem odatagriditem in mydatagrid.items)
{
chkexport = (checkbox)odatagriditem.findcontrol("chkexport");
chkexport.checked = false;
}
}
}
上一篇: ASP.NET Internet安全Forms身份验证方法
下一篇: PHP获取数组中单列值的方法