首页 后端开发 C#.Net教程 asp.net 传值总结

asp.net 传值总结

Mar 31, 2017 am 11:48 AM

篇文章是网上搜集的,我看了里面大部分内容都适合asp.net2.0
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 + "')");
}

★特别注意:【?id=】 处不能为 【?id =】
6.表格超连接列传递参数
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
{
/**////


/// 从系统异常类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.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
}
12.Panel 横向滚动,纵向自动扩展



13.回车转换成Tab



onkeydown="if(event.keyCode==13) event.keyCode=9"

http://dotnet.aspx.cc/exam/enter2tab.aspx

14.DataGrid超级连接列

DataNavigateUrlField="字段名" DataNavigateUrlformatString="http://xx/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.模板列



"ArticleID")%>' runat="server" width="80%" id="lblColumn" />













  后台代码

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;
  }
 }
}
17.数字格式化

【<%#Container.DataItem("price")%>的结果是500.0000,怎样格式化为500.00?】
<%#Container.DataItem("price","{0:¥#,##0.00}")%>
int i=123456;
string s=i.ToString("###,###.00");

18.日期格式化

【aspx页面内:<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")%>
显示为: 2004-8-11 19:44:28
我只想要:2004-8-11 】

<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>

应该如何改?

【格式化日期】
取出来,一般是object
((DateTime)objectFromDB).ToString("yyyy-MM-dd");

【日期的验证表达式】
A.以下正确的输入格式: [2004-2-29], [2004-02-29 10:29:39 pm], [2004/12/31]  

^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))
[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|
([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468]
[1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]
?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|
([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))
(/s(((0?[1-9])|(1[0-2]))/:([0-5][0-9])((/s)|(/:([0-5][0-9])/s))
([AM|PM|am|pm]{2,2})))?$

B.以下正确的输入格式:[0001-12-31], [9999 09 30], [2002/03/03]  

^/d{4}[/-///s]?((((0[13578])|(1[02]))[/-///s]?(([0-2][0-9])|(3[01])))|
(((0[469])|(11))[/-///s]?(([0-2][0-9])|(30)))|(02[/-///s]?[0-2][0-9]))$

【大小写转换】

HttpUtility.HtmlEncode(string);
HttpUtility.HtmlDecode(string)

19.如何设定全局变量

  Global.asax中
  Application_Start()事件中
  添加Application[属性名] = xxx;
  就是你的全局变量

20.怎样作到HyperLinkColumn生成的连接后,点击连接,打开新窗口?

  HyperLinkColumn有个属性Target,将器值设置成"_blank"即可.(Target="_blank")

  【ASPNETMENU】点击菜单项弹出新窗口
  在你的menuData.xml文件的菜单项中加入URLTarget="_blank"
  如:




 
   
      URLTarget="_blank" LeftIcon="file.gif"/>
     

  最好将你的aspnetmenu升级到1.2版


以上是asp.net 传值总结的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

char在C语言字符串中的作用是什么 char在C语言字符串中的作用是什么 Apr 03, 2025 pm 03:15 PM

在 C 语言中,char 类型在字符串中用于:1. 存储单个字符;2. 使用数组表示字符串并以 null 终止符结束;3. 通过字符串操作函数进行操作;4. 从键盘读取或输出字符串。

C语言各种符号的使用方法 C语言各种符号的使用方法 Apr 03, 2025 pm 04:48 PM

C 语言中符号的使用方法涵盖算术、赋值、条件、逻辑、位运算符等。算术运算符用于基本数学运算,赋值运算符用于赋值和加减乘除赋值,条件运算符用于根据条件执行不同操作,逻辑运算符用于逻辑操作,位运算符用于位级操作,特殊常量用于表示空指针、文件结束标记和非数字值。

char在C语言中如何处理特殊字符 char在C语言中如何处理特殊字符 Apr 03, 2025 pm 03:18 PM

C语言中通过转义序列处理特殊字符,如:\n表示换行符。\t表示制表符。使用转义序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要转义两次。不同平台和编译器可能有不同的转义序列,请查阅文档。

c#多线程和异步的区别 c#多线程和异步的区别 Apr 03, 2025 pm 02:57 PM

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

char与wchar_t在C语言中的区别 char与wchar_t在C语言中的区别 Apr 03, 2025 pm 03:09 PM

在 C 语言中,char 和 wchar_t 的主要区别在于字符编码:char 使用 ASCII 或扩展 ASCII,wchar_t 使用 Unicode;char 占用 1-2 个字节,wchar_t 占用 2-4 个字节;char 适用于英语文本,wchar_t 适用于多语言文本;char 广泛支持,wchar_t 依赖于编译器和操作系统是否支持 Unicode;char 的字符范围受限,wchar_t 的字符范围更大,并使用专门的函数进行算术运算。

char在C语言中如何进行类型转换 char在C语言中如何进行类型转换 Apr 03, 2025 pm 03:21 PM

在 C 语言中,char 类型转换可以通过:强制类型转换:使用强制类型转换符将一种类型的数据直接转换为另一种类型。自动类型转换:当一种类型的数据可以容纳另一种类型的值时,编译器自动进行转换。

char数组在C语言中如何使用 char数组在C语言中如何使用 Apr 03, 2025 pm 03:24 PM

char 数组在 C 语言中存储字符序列,声明为 char array_name[size]。访问元素通过下标运算符,元素以空终止符 '\0' 结尾,用于表示字符串终点。C 语言提供多种字符串操作函数,如 strlen()、strcpy()、strcat() 和 strcmp()。

C语言 sum 的作用是什么? C语言 sum 的作用是什么? Apr 03, 2025 pm 02:21 PM

C语言中没有内置求和函数,需自行编写。可通过遍历数组并累加元素实现求和:循环版本:使用for循环和数组长度计算求和。指针版本:使用指针指向数组元素,通过自增指针遍历高效求和。动态分配数组版本:动态分配数组并自行管理内存,确保释放已分配内存以防止内存泄漏。

See all articles