snis 绝顶 痉挛 高潮:ASP.net常见问题集锦.Net技巧
来源:百度文库 编辑:偶看新闻 时间:2024/05/05 05:30:44
asp.net常见问题解决办法
1. 打开新的窗口并传送参数:
传送参数:
response.write("")接收参数:
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 + "')");
}
★特别注意:【?id=】 处不能为 【?id =】
6.表格超连接列传递参数
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
例子:
// in global.asaxprotected 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
{
///
/// 从系统异常类ApplicationException继承的应用程序异常处理类。
/// 自动将异常内容记录到Windows NT/2000的应用程序日志
///
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
{
///
/// 事件日志记录类,提供事件日志记录支持
///
/// 定义了4个日志记录方法 (error, warning, info, trace)
///
///
public class ApplicationLog
{
///
/// 将错误信息记录到Win2000/NT事件日志中
/// 需要记录的文本信息
///
public static void WriteError(String message)
{
WriteLog(TraceLevel.Error, message);
}
///
/// 将警告信息记录到Win2000/NT事件日志中
/// 需要记录的文本信息
///
public static void WriteWarning(String message)
{
WriteLog(TraceLevel.Warning, message);
}
///
/// 将提示信息记录到Win2000/NT事件日志中
/// 需要记录的文本信息
///
public static void WriteInfo(String message)
{
WriteLog(TraceLevel.Info, message);
}
///
/// 将跟踪信息记录到Win2000/NT事件日志中
/// 需要记录的文本信息
///
public static void WriteTrace(String message)
{
WriteLog(TraceLevel.Verbose, message);
}
///
/// 格式化记录到事件日志的文本信息格式
/// 需要格式化的异常对象
/// 异常信息标题字符串.
///
///
///
///
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();
}
///
/// 实际事件日志写入方法
/// 要记录信息的级别(error,warning,info,trace).
/// 要记录的文本.
///
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.Inf
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 横向滚动,纵向自动扩展
13.回车转换成Tab
onkeydown="if(event.keyCode==13) event.keyCode=9"
14.DataGrid超级连接列
DataNavigateUrlField="字段名" DataNavigateUrlFormatString=http://6ccn.com/inc/delete.aspx?ID={0}15.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"+"\"");
}
}
16.模板列
后台代码
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;
}
}
}
连接字符串 config
ConfigurationManager.ConnectionStrings["connstring"].ConnectionString2.为按钮添加对话框
Button1.Attributes.Add("onclick","return confirm('确认?')");button.attributes.add("onclick","return confirm('are you sure...?')")
3.showModalDialog和showModelessDialog使用心得
一、showModalDialog和showModelessDialog有什么不同?
showModalDialog:被打开后就会始终保持输入焦点。除非对话框被关闭,否则用户无法切换到主窗口。类似alert的运行效果。
showModelessDialog:被打开后,用户可以随机切换输入焦点。对主窗口没有任何影响(最多是被挡住一下而以。:P)
二、怎样才让在showModalDialog和showModelessDialog的超连接不弹出新窗口?
在被打开的网页里加上
三、怎样才刷新showModalDialog和showModelessDialog里的内容?
在showModalDialog和showModelessDialog里是不能按F5刷新的,又不能弹出菜单。这个只能依靠javascript了,以下是相关代码:
将filename.htm替换成网页的名字然后将它放到你打开的网页里,按F5就可以刷新了,注意,这个要配合
四、如何用javascript关掉showModalDialog(或showModelessDialog)打开的窗口。
也要配合
五、showModalDialog和showModelessDialog数据传递技巧。(作者语:本来想用一问一答形式来写的,但是我想不出这个怎么问,所以只好这样了。)
这个东西比较麻烦,我改了好几次了不是没办法说明白(语文水平越来越差了),只好用个例子说明了。
例子:
现在需要在一个showModalDialog(或showModelessDialog)里读取或设置一个变量var_name
一般的传递方式:
window.showModalDialog("filename.htm",var_name)//传递var_name变量
在showModalDialog(或showModelessDialog)读取和设置时:
alert(window.dialogArguments)//读取var_name变量window.dialogArguments="oyiboy"//设置var_name变量
这种方式是可以满足的,但是当你想在操作var_name同时再操作第二个变理var_id时呢?就无法再进行操作了。这就是这种传递方式的局限性。
以下是我建议使用的传递方式:
window.showModalDialog("filename.htm",window)//不管要操作什么变量,只直传递主窗口的window对象
在showModalDialog(或showModelessDialog)读取和设置时:
alert(window.dialogArguments.var_name)//读取var_name变量window.dialogArguments.var_name="oyiboy"//设置var_name变量
同时我也可以操作var_id变量
alert(window.dialogArguments.var_id)//读取var_id变量window.dialogArguments.var_id="001"//设置var_id变量
同样还可以对主窗口的任何对象进行操作,如form对象里的元素。
window.dialogArguments.form1.index1.value="这是在设置index1元素的值"六、多个showModelessDialog的相互操作。
因为光说很费劲,我就偷点懒,直接用代码来说了,如果不明白的话那就直接来信(oyiboy#163.net(使用时请将#改成@))问我吧。
以下代码的主要作用是在一个showModelessDialog里移动别一个showModelessDialog的位置。
主文件的部份js代码。
var s1=showModelessDialog(''控制.htm'',window,"dialogTop:1px;dialogLeft:1px") //打开控制窗口var s2=showModelessDialog(''about:blank'',window,"dialogTop:200px;dialogLeft:300px") //打开被控制窗口
控制.htm的部份代码。
以上关键部份是:
窗口命名方式:var s1=showModelessDialog(''控制.htm'',window,"dialogTop:1px;dialogLeft:1px")
变量访问方式:window.dialogArguments.s2.dialogTop
这个例子只是现实showModelessDialog与showModelessDialog之间的位置操作功能,通过这个原理,在showModelessDialog之间相互控制各自的显示页面,传递变量和数据等