【Asp.net精品源码栏目提醒】:网学会员在Asp.net精品源码频道为大家收集整理了“c#常用源码 - 艺术“提供大家参考,希望对大家有所帮助!
1. 打开新的窗口并传送参数:传送参数:response.writequot < script >window.open.aspxidquotthis.DropDownList1.SelectIndexquotampid1quot...quot</script>quot接收参数:string a Request.QueryStringquotidquotstring b Request.QueryStringquotid1quot2.为按钮添加对话框Button1.Attributes.Addquotonclickquotquotreturn confirm确认quotbutton.attributes.addquotonclickquotquotifconfirmare you sure...returntrueelsereturn falsequot3.删除表格选定记录int intEmpID intMyDataGrid.DataKeyse.Item.ItemIndexstring deleteCmd quotDelete from Employee where emp_id quot intEmpID.ToString4.删除表格记录警告private void DataGrid_ItemCreatedObject senderDataGridItemEventArgs e switche.Item.ItemType case ListItemType.Item :case ListItemType.AlternatingItem :case ListItemType.EditItem: TableCell myTableCell myTableCell e.Item.Cells14 LinkButton myDeleteButton myDeleteButton LinkButtonmyTableCell.Controls0 myDeleteButton.Attributes.Addquotonclickquotquotreturn confirm您是否确定要删除这条信息quot breakdefault: break 5.点击表格行链接另一页private void grdCustomer_ItemDataBoundobject senderSystem.Web.UI.WebControls.DataGridItemEventArgs e //点击表格打开 if e.Item.ItemType ListItemType.Item e.Item.ItemType ListItemType.AlternatingIteme.Item.Attributes.Addquotonclickquotquotwindow.openDefault.aspxidquot e.Item.Cells0.Text quotquot双击表格连接到另一页在 itemDataBind 事件中ife.Item.ItemType ListItemType.Item e.Item.ItemType ListItemType.AlternatingItem string orderItemID e.item.cells1.Text ... e.item.Attributes.Addquotondblclickquot quotlocation.href../ShippedGrid.aspxidquot orderItemID quotquot双击表格打开新一页ife.Item.ItemType ListItemType.Item e.Item.ItemType ListItemType.AlternatingItem string orderItemID e.item.cells1.Text ... e.item.Attributes.Addquotondblclickquot quotopen../ShippedGrid.aspxidquot orderItemID quotquot6.表格超连接列传递参数< asp:HyperLinkColumn Targetquot_blankquot headertextquotID 号 quot DataTextFieldquotidquotNavigateUrlquotaaa.aspxid < DataBinder.EvalContainer.DataItem quot 数 据 字 段 1quot > amp name < DataBinder.EvalContainer.DataItem quot数据字段 2quot> />7.表格点击改变颜色if e.Item.ItemType ListItemType.Item e.Item.ItemType ListItemType.AlternatingItem e.Item.Attributes.Addquotonclickquotquotthis.style.backgroundColor99cc00 this.style.colorbuttontextthis.style.cursordefaultquot写在 DataGrid 的_ItemDataBound 里if e.Item.ItemType ListItemType.Item e.Item.ItemType ListItemType.AlternatingIteme.Item.Attributes.Addquotonmouseoverquotquotthis.style.backgroundColor99cc00 this.style.colorbuttontextthis.style.cursordefaultquote.8.关于日期格式日期格式设定DataFormatStringquot0:yyyy-MM-ddquot我觉得应该在 itembound 事件中e.items.cellquot 你 的 列 quot.textDateTime.Parsee.items.cellquot 你 的 列quot.text.ToStringquotyyyy-MM-ddquot9.获取错误信息并到指定页面不要使用 Response.Redirect而应该使用 Server.Transfere.g// in global.asaxprotected void Application_ErrorObject sender EventArgs e if Server.GetLastError is HttpUnhandledExceptionServer.TransferquotMyErrorPage.aspxquot//其余的非 HttpUnhandledException 异常交给
ASP.NET 自己处理就 okay 了 :Redirect 会导致 post-back 的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理10.清空 CookieCookie.ExpiresDateTimeResponse.CookiesquotUserNamequot.Expires 011.自定义异常处理//自定义异常处理类using Systemusing System.Diagnosticsnamespace MyAppException /// <summary> /// 从系统异常类 ApplicationException 继承的应用程序异常处理类。
/// 自动将异常内容记录到 Windows NT/2000 的应用程序日志 /// </summary> public class AppException:System.ApplicationException public AppException if ApplicationConfiguration.EventLogEnabledLogEventquot出现一个未知错误。
quot public AppExceptionstring message LogEventmessage public AppExceptionstring messageException innerException LogEventmessageif innerException null LogEventinnerException.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 namequotmessagequot>需要记录的文本信息</param> /// </summary> public static void WriteErrorString message WriteLogTraceLevel.Error message /// <summary> /// 将警告信息记录到 Win2000/NT 事件日志中 /// <param namequotmessagequot>需要记录的文本信息</param> /// </summary> public static void WriteWarningString message WriteLogTraceLevel.Warning message /// <summary> /// 将提示信息记录到 Win2000/NT 事件日志中 /// <param namequotmessagequot>需要记录的文本信息</param> /// </summary> public static WriteLogTraceLevel.Info message /// <summary> /// 将跟踪信息记录到 Win2000/NT 事件日志中 /// <param namequotmessagequot>需要记录的文本信息</param> /// </summary> public static void WriteTraceString message WriteLogTraceLevel.Verbose message /// <summary> /// 格式化记录到事件日志的文本信息格式 /// <param namequotexquot>需要格式化的异常对象</param> /// <param namequotcatchInfoquot>异常信息标题字符串.</param> /// <retvalue> /// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para> /// </retvalue> /// </summary> public static String FormatExceptionException ex String catchInfo StringBuilder strBuilder new StringBuilderif catchInfo String.Empty strBuilder.AppendcatchInfo.AppendquotrnquotstrBuilder.Appendex.Message.Appendquotrnquot.Appendex.StackTracereturn strBuilder.ToString /// <summary> /// 实际事件日志写入方法 /// <param namequotlevelquot>要记录信息的级别(errorwarninginfotrace.</param> /// <param namequotmessageTextquot>要记录的文本.</param> /// </summary> private static void WriteLogTraceLevel level String messageText try EventLogEntryType LogEntryType switch level case TraceLevel.Error: LogEntryType EventLogEntryType.Error breakcase TraceLevel.Warning: LogEntryType EventLogEntryType.Warning breakcase TraceLevel.Info: LogEntryType EventLogEntryType.Information breakcase TraceLevel.Verbose: LogEntryType EventLogEntryType.SuccessAudit breakdefault: LogEntryType EventLogEntryType.SuccessAudit break EventLog eventLog new EventLogquotApplicationquotApplicationConfiguration.EventLogMachineNameApplicationConfiguration.EventLogSourceName //写入事件日志 eventLog.WriteEntrymessageText LogEntryType catch //忽略任何异常 //class ApplicationLog12.Panel 横向滚动,纵向自动扩展<asp:panel stylequotoverflow-x:scrolloverflow-y:autoquot></asp:panel>13.回车转换成 Tab<script languagequotjavascriptquot forquotdocumentquot eventquotonkeydownquot> ifevent.keyCode13 ampamp event.srcElement.typebutton ampampevent.srcElement.typesubmit ampamp event.srcElement.typereset ampampevent.srcElement.typeampampevent.srcElement.typetextarea event.keyCode9</script>onkeydownquotifevent.keyCode13 event.keyCode9quot14.DataGrid 超级连接列DataNavigateUrlFieldquot 字 段 名 quotDataNavigateUrlFormatStringquothttp://xx/inc/delete.aspxID0quot15.DataGrid 行随鼠标变色private void DGzf_ItemDataBoundobject sender System.Web.UI.WebControls.DataGr if e.Item.ItemTypeListItemType.Header e.Item.Attributes.Add quotonmouseoutquotquotthis.style.backgroundColorquotquote.Item.StylequotBACKGROUND-COLORquotquotquotquote.Item.Attributes.Add quotonmouseoverquotquotthis.style.backgroundColorquotquotquotEFF3F7quotquotquotquot .模板列www.knowsky.com<ASP:TEMPLATECOLUMN visiblequotFalsequot sortexpressionquotdemoquot headertextquotIDquot><ITEMTEMPLATE>< ASP:LABEL text < DataBinder.EvalContainer.DataItem quotArticleIDquot > runatquotserverquot widthquot80quot idquotlblColumnquot /></ITEMTEMPLATE></ASP:TEMPLATECOLUMN><ASP:TEMPLATECOLUMN headertextquot选中quot><HEADERSTYLE wrapquotFalsequot horizontalalignquotCenterquot></HEADERSTYLE><ITEMTEMPLATE><ASP:CHECKBOX idquotchkExportquot runatquotserverquot /></ITEMTEMPLATE><EDITITEMTEMPLATE><ASP:CHECKBOX idquotchkExportONquot runatquotserverquot enabledquottruequot /></EDITITEMTEMPLATE></ASP:TEMPLATECOLUMN>后台代码protected void CheckAll_CheckedChangedobject sender System.EventArgs e //改变列的选定,实现全选或全不选。
CheckBox chkExport if CheckAll.Checked foreachDataGridItem oDataGridItem in MyDataGrid.Items chkExport CheckBoxoDataGridItem.FindControlquotchkExportquot chkExport.Checked true else foreachDataGridItem oDataGridItem in MyDataGrid.Items chkExport CheckBoxoDataGridItem.FindControlquotchkExportquot chkExport.Checked false 17.数字格式化【<Container.DataItemquotpricequot>的结果是 500.0000,怎样格式化为 500.00】<Container.DataItemquotpricequotquot0:¥0.00quot>int i123456string si.ToStringquot.00quot18.日期格式化【aspx 页面内:< DataBinder.EvalContainer.DataItemquotCompany_Ureg_Datequot>显示为: 2004-8-11 19:44:28我只想要:2004-8-11 】< DataBinder.EvalContainer.DataItemquotCompany_Ureg_Datequotquot0:yyyy-M-dquot>应该如何改?【格式化日期】取出来一般是 objectDateTimeobjectFromDB.ToStringquotyyyy-MM-ddquot【日期的验证表达式】A.以下正确的输入格式: 2004-2-29 2004-02-29 10:29:39 pm 2004/12/31d2024680481357926-/s013578102-/s01-91-20-9301046911-/s01-91-20-93002-/s01-91-20-9d20246812356799-/s013578102-/s01-91-20-9301046911-/s01-91-20-93002-/s01-910-920-8s01-910-2:0-50-9s:0-50-9sAMPMampm22B.以下正确的输入格式:0001-12-31 9999 09 30 2002/03/03d4-/s013578102-/s0-20-9301046911-/s0-20-93002-/s0-20-9【大小 HttpUtility.HtmlEncodestringHttpUtility.HtmlDecodestring19.如何设定全局变量Global.asax 中Application_Start事件中添加 Application属性名 = xxx就是你的全局变量20.怎样作到 HyperLinkColumn 生成的连接后,点击连接,打开新窗口?HyperLinkColumn 有个属性 Target将器值设置成quot_blankquot即可.Targetquot_blankquot【ASPNETMENU】点击菜单项弹出新窗口在你的 menuData.xml 文件的菜单项中加入 URLTargetquot_blankquot,如:<xml versionquot1.0quot encodingquotGB2312quot><MenuData ImagesBaseURLquotimages/quot><MenuGroup><MenuItem Labelquot内参信息quot URLquotInfomation.aspxquot ><MenuGroup IDquotBBCquot>< MenuItem Labelquot 公 告 信 息 quot URLquotInfomation.aspxquot URLTargetquot_blankquotLeftIconquotfile.gifquot/><MenuItem Labelquot编制信息简报quot URLquotNewInfo.aspxquot LeftIconquotfile.gifquot />......最好将你的 aspnetmenu 升级到 1.2 版21.读取 DataGrid 控件 TextBox 值foreachDataGrid dgi in yourDataGrid.Items TextBox tb TextBoxdgi.FindControlquotyourTextBoxIdquot tb.Text....23.在 DataGrid 中有 3 个模板列包含 Textbox 分别为 DG_ShuLiang 数量 DG_DanJian单价DG_JinE金额分别在 5.6.7 列,要求在录入数量及单价的时候自动算出金额即:数量单价金额还要求录入时限制为数值型.我如何用客户端脚本实现这个功能<asp:TemplateColumn HeaderTextquot数量quot><ItemTemplate>< asp:TextBox idquotShuLiangquot runatserver Text < DataBinder.EvalContainer.DataItemquotDG_ShuLiangquot>onkeyupquotjavascript:DoCalquot/>< asp:RegularExpressionValidator idquotrevSquot runatquotserverquotControlToValidatequotShuLiangquot ErrorMessagequotmust be integerquotValidationExpressionquotdquot /></ItemTemplate></asp:TemplateColumn><asp:TemplateColumn HeaderTextquot单价quot><ItemTemplate>< asp:TextBox idquotDanJianquot runatserver Text < DataBinder.EvalContainer.DataItemquotDG_DanJianquot>onkeyupquotjavascript:DoCalquot/>< asp:RegularExpressionValidator idquotrevS2quot runatquotserverquotControlToValidatequotDanJianquot ErrorMessagequotmust be numericquotValidationExpressionquotd.dquot /></ItemTemplate></asp:TemplateColumn><asp:TemplateColumn HeaderTextquot金额quot><ItemTemplate>< asp:TextBox idquotJinEquot runatserver Text < DataBinder.EvalContainer.DataItemquotDG_JinEquot> /></ItemTemplate></asp:TemplateColumn><script languagequotjavascriptquot>function DoCal var e event.srcElement var row e.parentNode.parentNode var txts row.all.tagsquotINPUTquot if txts.length txts.length < 3return var q txtstxts.length-3.value var p txtstxts.length-2.value if isNaNq isNaNpreturn q parseIntq p parseFloatp txtstxts.length-1.value q p.toFixed2</script>24.datagrid 选定比较底下 page_loadpage.smartNavigationtrue25.在 Datagrid 中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小 private void DataGrid1_ItemDataBoundobj senderDataGridItemEventArgs e forint i0i<e.Item.Cells.Count-1iife.Item.ItemTypeListItemType.EditType e.Item.Cellsi.Attributes.AddquotWidthquot quot80pxquot26.对话框private static string ScriptBegin quot<script languagequotJavaScriptquot>quotprivate static string ScriptEnd quot</script>quotpublic static void ConfirmMessageBoxstring PageTargetstring Content string ConfirmContentquotvarretValuewindow.confirmquotContentquotquotquotifretValuewindow.locationquotPageTargetquotquot ConfirmContentScriptBegin ConfirmContent ScriptEnd Page ParameterPage PageSystem.Web.HttpContext.Current.Handler ParameterPage.RegisterStartupScriptquotconfirmquotConfirmContent //Response.WritestrScript 27. 将时间格式化:string aaDateTime.Now.ToStringquotyyyy 年 MM 月 dd 日quot1.1 取当前年月日时分秒currentTimeSystem.DateTime.Now1.2 取当前年int 年 DateTime.Now.Year1.3 取当前月int 月 DateTime.Now.Month1.4 取当前日int 日 DateTime.Now.Day1.5 取当前时int 时 DateTime.Now.Hour1.6 取当前分int 分 DateTime.Now.Minute1.7 取当前秒int 秒 DateTime.Now.Second1.8 取当前毫秒int 毫秒 DateTime.Now.Millisecond28.自定义分页代码:先定义变量 :public static int pageCount //总页面数public static int curPageIndex1 //当前页面下一页:ifDataGrid1.CurrentPageIndex < DataGrid1.PageCount - 1 DataGrid1.CurrentPageIndex 1 curPageIndex1bind // DataGrid1 数据绑定函数上一页:ifDataGrid1.CurrentPageIndex >0 DataGrid1.CurrentPageIndex 1 curPageIndex-1bind // DataGrid1 数据绑定函数直接页面跳转:int aint.ParseJumpPage.Value.Trim//JumpPage.Value.Trim为跳转值ifa<DataGrid1.PageCount this.DataGrid1.CurrentPageIndexabind29.DataGrid 使用:添加删除确认:private void DataGrid1_ItemCreatedobject senderSystem.Web.UI.WebControls.DataGridItemEventArgs e foreachDataGridItem di in this.DataGrid1.Items ifdi.ItemTypeListItemType.Itemdi.ItemTypeListItemType.AlternatingItem LinkButtondi.Cells8.Controls0.Attributes.Addquotonclickquotquotreturnconfirm确认删除此项吗quot 样式交替:ListItemType itemType e.Item.ItemTypeif itemType ListItemType.Item e.Item.Attributesquotonmouseoutquot quotjavascript:this.style.backgroundColorFFFFFFquot e.Item.Attributesquotonmouseoverquot quotjavascript:this.styelse if itemType ListItemType.AlternatingItem e.Item.Attributesquotonmouseoutquot quotjavascript:this.style.backgroundColora0d7c4quot e.Item.Attributesquotonmouseoverquot quotjavascript:this.style.backgroundColord9ece1cursorhandquot 添加一个编号列:DataTable dt c.ExecuteRtnTableForAccesssqltxt //执行 sql 返回的 DataTableDataColumn dcdt.Columns.AddquotnumberquotSystem.Type.GetTypequotSystem.Stringquotforint i0i<dt.Rows.Counti dt.Rowsiquotnumberquoti1.ToStringDat.
上一篇:
基于AJAX 和Web Service的Web开发第07章ASPNETAjax核心组件一]理论课
下一篇:
病理学实验课多样化教学方法探讨