<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Graygift@ye &#187; asp.net</title>
	<atom:link href="http://www.graygift.com/tag/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.graygift.com</link>
	<description>我是孤独的根号5      </description>
	<lastBuildDate>Sun, 08 Nov 2009 02:13:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>ASP.NET 程序中常用代码</title>
		<link>http://www.graygift.com/web/aspnetcode/</link>
		<comments>http://www.graygift.com/web/aspnetcode/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 12:39:32 +0000</pubDate>
		<dc:creator>Graygift</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[asp.net]]></category>

		<guid isPermaLink="false">http://www.graygift.com/?p=16</guid>
		<description><![CDATA[1. 打开新的窗口并传送参数
2.为按钮添加对话框
3.删除表格选定记录
4.删除表格记录警告
5.点击表格行链接另一页
6.表格超连接列传递参数
8.关于日期格式
9.获取错误信息并到指定页面
10... ]]></description>
			<content:encoded><![CDATA[<p>1. 打开新的窗口并传送参数：　　</p>
<div>传送参数：  </div>
<div><span style="font-family: 'Courier New'; line-height: 18px; white-space: pre;">接收参数：</span></div>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code1'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p161"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p16code1"><pre class="language" style="font-family:monospace;">string a = Request.QueryString(&quot;id&quot;);
string b = Request.QueryString(&quot;id1&quot;);</pre></td></tr></table></div>

<p>2.为按钮添加对话框</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code2'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p162"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p16code2"><pre class="language" style="font-family:monospace;">Button1.Attributes.Add(&quot;onclick&quot;,&quot;return confirm('确认?')&quot;);
button.attributes.add(&quot;onclick&quot;,&quot;if(confirm('are you sure...?')){return true;}else{return false;}&quot;)</pre></td></tr></table></div>

<p><span id="more-16"></span>3.删除表格选定记录</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code3'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p163"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p16code3"><pre class="language" style="font-family:monospace;">int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];
string deleteCmd = &quot;DELETE from Employee where emp_id = &quot; + intEmpID.ToString()</pre></td></tr></table></div>

<p>4.删除表格记录警告</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code4'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p164"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p16code4"><pre class="language" style="font-family:monospace;">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(&quot;onclick&quot;,&quot;return confirm('您是否确定要删除这条信息');&quot;);
　　　break;
　　default:
　　　break;
　}
}</pre></td></tr></table></div>

<p>5.点击表格行链接另一页</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code5'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p165"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p16code5"><pre class="language" style="font-family:monospace;">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(&quot;onclick&quot;,&quot;window.open('Default.aspx?id=&quot; + e.Item.Cells[0].Text + &quot;');&quot;);
}
　　双击表格连接到另一页
　　在itemDataBind事件中
if(e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
{
　string OrderItemID =e.item.cells[1].Text;
　...
　e.item.Attributes.Add(&quot;ondblclick&quot;, &quot;location.href='../ShippedGrid.aspx?id=&quot; + OrderItemID + &quot;'&quot;);
}
　　双击表格打开新一页
if(e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
{
　string OrderItemID =e.item.cells[1].Text;
　...
　e.item.Attributes.Add(&quot;ondblclick&quot;, &quot;open('../ShippedGrid.aspx?id=&quot; + OrderItemID + &quot;')&quot;);
}</pre></td></tr></table></div>

<p>6.表格超连接列传递参数</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code6'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p166"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p16code6"><pre class="language" style="font-family:monospace;">&amp;lt;asp:HyperLinkColumn Target=&quot;_blank&quot; headertext=&quot;ID号&quot; DataTextField=&quot;id&quot; 
NavigateUrl=&quot;aaa.aspx?id='　
&amp;lt;%# DataBinder.Eval(Container.DataItem, &quot;数据字段1&quot;)%&amp;gt;' &amp;amp; name='
&amp;lt;%# DataBinder.Eval(Container.DataItem, &quot;数据字段2&quot;)%&amp;gt;' /&amp;gt;</pre></td></tr></table></div>

<p>7.表格点击改变颜色</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code7'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p167"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p16code7"><pre class="language" style="font-family:monospace;">if (e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add(&quot;onclick&quot;,&quot;this.style.backgroundColor='#99cc00';
this.style.color='buttontext';this.style.cursor='default';&quot;);
}
　　写在DataGrid的_ItemDataBound里
if (e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add(&quot;onmouseover&quot;,&quot;this.style.backgroundColor='#99cc00';
this.style.color='buttontext';this.style.cursor='default';&quot;)
e.Item.Attributes.Add(&quot;onmouseout&quot;,&quot;this.style.backgroundColor='';this.style.color='';&quot;);
}</pre></td></tr></table></div>

<p>8.关于日期格式<br />
日期格式设定</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code8'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p168"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p16code8"><pre class="language" style="font-family:monospace;">DataFormatString=&quot;{0:yyyy-MM-dd}&quot; 我觉得应该在itembound事件中
e.items.cell[&quot;你的列&quot;].text=DateTime.Parse(e.items.cell[&quot;你的列&quot;].text.ToString(&quot;yyyy-MM-dd&quot;)) </pre></td></tr></table></div>

<p>9.获取错误信息并到指定页面<br />
不要使用Response.Redirect,而应该使用Server.Transfer</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code9'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p169"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p16code9"><pre class="language" style="font-family:monospace;">　　e.g
// in global.asax protected void Application_Error(Object sender, EventArgs e) 
{ if (Server.GetLastError() is HttpUnhandledException) Server.Transfer(&quot;MyErrorPage.aspx&quot;);
//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
} 　&lt;span style=&quot;font-family: Georgia; line-height: 19px; white-space: normal;&quot;&gt;　&lt;/span&gt;</pre></td></tr></table></div>

<p>Redirect会导致post－back的产生从而丢失了错误信息，所以页面导向应该直接在服务器端执行，<br />
这样就可以在错误处理页面得到出错信息并进行相应的处理<br />
10.清空Cookie</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code10'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1610"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p16code10"><pre class="language" style="font-family:monospace;">Cookie.Expires=[DateTime]; Response.Cookies(&quot;UserName&quot;).Expires = 0 　</pre></td></tr></table></div>

<p>11.自定义异常处理<br />
自定义异常处理类</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code11'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1611"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p16code11"><pre class="language" style="font-family:monospace;">using System;
using System.Diagnostics;
namespace MyAppException
{
/// &amp;lt;summary&amp;gt;
/// 从系统异常类ApplicationException继承的应用程序异常处理类。
/// 自动将异常内容记录到Windows NT/2000的应用程序日志
/// &amp;lt;/summary&amp;gt;
public class AppException : System.ApplicationException
{
public AppException()
{
if (ApplicationConfiguration.EventLogEnabled) LogEvent(&quot;出现一个未知错误。&quot;);
}
public AppException(string message)
{
LogEvent(message);
}
public AppException(string message, Exception innerException)
{
LogEvent(message);
if (innerException != null)
{
LogEvent(innerException.Message);
}
}
}
}</pre></td></tr></table></div>

<p>日志记录类</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code12'); return false;">View Code</a> LANGUAGE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1612"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code" id="p16code12"><pre class="language" style="font-family:monospace;">using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
namespace MyEventLog
{
/// &amp;lt;summary&amp;gt;
/// 事件日志记录类，提供事件日志记录支持
/// &amp;lt;remarks&amp;gt;
/// 定义了4个日志记录方法 (error, warning, info, trace)
/// &amp;lt;/remarks&amp;gt;
/// &amp;lt;/summary&amp;gt;
public class ApplicationLog
{
/// &amp;lt;summary&amp;gt;
/// 将错误信息记录到Win2000/NT事件日志中
/// &amp;lt;param name=&quot;message&quot;&amp;gt;需要记录的文本信息&amp;lt;/param&amp;gt;
/// &amp;lt;/summary&amp;gt;
public static void WriteError(String message)
{
WriteLog(TraceLevel.Error, message);
}
/// &amp;lt;summary&amp;gt;
/// 将警告信息记录到Win2000/NT事件日志中
/// &amp;lt;param name=&quot;message&quot;&amp;gt;需要记录的文本信息&amp;lt;/param&amp;gt;
/// &amp;lt;/summary&amp;gt;
public static void WriteWarning(String message)
{
WriteLog(TraceLevel.Warning, message);
}
/// &amp;lt;summary&amp;gt;
/// 将提示信息记录到Win2000/NT事件日志中
/// &amp;lt;param name=&quot;message&quot;&amp;gt;需要记录的文本信息&amp;lt;/param&amp;gt;
/// &amp;lt;/summary&amp;gt;
public static void WriteInfo(String message)
{
WriteLog(TraceLevel.Info, message);
}
/// &amp;lt;summary&amp;gt;
/// 将跟踪信息记录到Win2000/NT事件日志中
/// &amp;lt;param name=&quot;message&quot;&amp;gt;需要记录的文本信息&amp;lt;/param&amp;gt;
/// &amp;lt;/summary&amp;gt;
public static void WriteTrace(String message)
{
WriteLog(TraceLevel.Verbose, message);
}
/// &amp;lt;summary&amp;gt;
/// 格式化记录到事件日志的文本信息格式
/// &amp;lt;param name=&quot;ex&quot;&amp;gt;需要格式化的异常对象&amp;lt;/param&amp;gt;
/// &amp;lt;param name=&quot;catchInfo&quot;&amp;gt;异常信息标题字符串.&amp;lt;/param&amp;gt;</pre></td></tr></table></div>

<ul class="related_post"><li><a href="http://www.graygift.com/web/use-firebug/" title="Firebug使用说明">Firebug使用说明</a></li><li><a href="http://www.graygift.com/web/flash1/" title="我的期末Flash作业！">我的期末Flash作业！</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.graygift.com/web/aspnetcode/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

