程序中常用的种代码
程序员文章站
2024-01-21 09:20:16
1. 打开新的窗口并传送参数: 传送参数: response.write("<script>window.open('*.aspx?id="+this...
1. 打开新的窗口并传送参数:
传送参数:
response.write("<script>window.open('*.aspx?id="+this.dropdownlist1.selectindex+"&id1="+...+"')</script>")
接收参数:
string a = request.querystring("id");
string b = request.querystring("id1");
2.为按钮添加对话框
button1.attributes.add("onclick","return confirm('确认?')");
button.attributes.add("onclick","if(confirm('are you sure...?')){return true;}else{return false;}")
3.删除表格选定记录
int intempid = (int)mydatagrid.datakeys[e.item.itemindex];
string deletecmd = "delete from employee where emp_id = " + intempid.tostring()
4.删除表格记录警告
private void datagrid_itemcreated(object sender,datagriditemeventargs e)
{
switch(e.item.itemtype)
{
case listitemtype.item :
case listitemtype.alternatingitem :
case listitemtype.edititem:
tablecell mytablecell;
mytablecell = e.item.cells[14];
linkbutton mydeletebutton ;
mydeletebutton = (linkbutton)mytablecell.controls[0];
mydeletebutton.attributes.add("onclick","return confirm('您是否确定要删除这条信息');");
break;
default:
break;
}
}
5.点击表格行链接另一页
private void grdcustomer_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
//点击表格打开
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
e.item.attributes.add("onclick","window.open('default.aspx?id=" + e.item.cells[0].text + "');");
}
双击表格连接到另一页
在itemdatabind事件中
if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
string orderitemid =e.item.cells[1].text;
...
e.item.attributes.add("ondblclick", "location.href='../shippedgrid.aspx?id=" + orderitemid + "'");
}
双击表格打开新一页
if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
string orderitemid =e.item.cells[1].text;
...
e.item.attributes.add("ondblclick", "open('../shippedgrid.aspx?id=" + orderitemid + "')");
}
6.表格超连接列传递参数
<asp:hyperlinkcolumn target="_blank" headertext="id号" datatextfield="id" navigateurl="aaa.aspx?id='
<%# databinder.eval(container.dataitem, "数据字段1")%>' & name='<%# databinder.eval(container.dataitem, "数据字段2")%>' />
7.表格点击改变颜色
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
}
传送参数:
response.write("<script>window.open('*.aspx?id="+this.dropdownlist1.selectindex+"&id1="+...+"')</script>")
接收参数:
string a = request.querystring("id");
string b = request.querystring("id1");
2.为按钮添加对话框
button1.attributes.add("onclick","return confirm('确认?')");
button.attributes.add("onclick","if(confirm('are you sure...?')){return true;}else{return false;}")
3.删除表格选定记录
int intempid = (int)mydatagrid.datakeys[e.item.itemindex];
string deletecmd = "delete from employee where emp_id = " + intempid.tostring()
4.删除表格记录警告
private void datagrid_itemcreated(object sender,datagriditemeventargs e)
{
switch(e.item.itemtype)
{
case listitemtype.item :
case listitemtype.alternatingitem :
case listitemtype.edititem:
tablecell mytablecell;
mytablecell = e.item.cells[14];
linkbutton mydeletebutton ;
mydeletebutton = (linkbutton)mytablecell.controls[0];
mydeletebutton.attributes.add("onclick","return confirm('您是否确定要删除这条信息');");
break;
default:
break;
}
}
5.点击表格行链接另一页
private void grdcustomer_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
//点击表格打开
if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
e.item.attributes.add("onclick","window.open('default.aspx?id=" + e.item.cells[0].text + "');");
}
双击表格连接到另一页
在itemdatabind事件中
if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
string orderitemid =e.item.cells[1].text;
...
e.item.attributes.add("ondblclick", "location.href='../shippedgrid.aspx?id=" + orderitemid + "'");
}
双击表格打开新一页
if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
{
string orderitemid =e.item.cells[1].text;
...
e.item.attributes.add("ondblclick", "open('../shippedgrid.aspx?id=" + orderitemid + "')");
}
6.表格超连接列传递参数
<asp:hyperlinkcolumn target="_blank" headertext="id号" datatextfield="id" navigateurl="aaa.aspx?id='
<%# databinder.eval(container.dataitem, "数据字段1")%>' & name='<%# databinder.eval(container.dataitem, "数据字段2")%>' />
7.表格点击改变颜色
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
}
下一篇: 页面包含的处理