0

0

asp.net 传值总结

怪我咯

怪我咯

发布时间:2017-03-31 11:48:17

|

1976人浏览过

|

来源于php中文网

原创

篇文章是网上搜集的,我看了里面大部分内容都适合asp.net2.0
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.表格超连接列传递参数
navigateurl="aaa.aspx?id=''
& name='' />

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

【的结果是500.0000,怎样格式化为500.00?】

int i=123456;
string s=i.tostring("###,###.00");

18.日期格式化

【aspx页面内:
显示为: 2004-8-11 19:44:28
我只想要:2004-8-11 】



应该如何改?

【格式化日期】
取出来,一般是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版

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

178

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

35

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

79

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

2

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

4

2026.01.28

Java 消息队列与异步架构实战
Java 消息队列与异步架构实战

本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

8

2026.01.28

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

24

2026.01.27

拼多多赚钱的5种方法 拼多多赚钱的5种方法
拼多多赚钱的5种方法 拼多多赚钱的5种方法

在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

122

2026.01.26

edge浏览器怎样设置主页 edge浏览器自定义设置教程
edge浏览器怎样设置主页 edge浏览器自定义设置教程

在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。

72

2026.01.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
ASP.NET参考手册
ASP.NET参考手册

共0课时 | 0人学习

传播智客ASP.NET中级系列视频教程
传播智客ASP.NET中级系列视频教程

共33课时 | 6.4万人学习

传播智客ASP.NET高级系列视频教程
传播智客ASP.NET高级系列视频教程

共34课时 | 6.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号