ASP.NET程序中常用代码汇总(一)


1. 打开新的窗口并传送参数:
None.gif // 传送参数:
None.gif
response.write( " <script>window.open(’*.aspx?id= " + this .DropDownList1.SelectIndex + " &id1= " + dot.gif + " ’)</script> " )
None.gif  
// 接收参数:
None.gif
string  a  =  Request.QueryString( " id " );
None.gif
string  b  =  Request.QueryString( " id1 " );
2.为按钮添加对话框
None.gif Button1.Attributes.Add( " onclick " , " return confirm(’确认?’) " );
None.gifbutton.attributes.add(
" onclick " , " if(confirm(’are you suredot.gif?’)){return true;}else{return false;} " )
3.删除表格选定记录
None.gif int  intEmpID  =  ( int )MyDataGrid.DataKeys[e.Item.ItemIndex];
None.gif
string  deleteCmd  =   " DELETE from Employee where emp_id =  "   +  intEmpID.ToString()
4.删除表格记录警告
None.gif private   void  DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)
ExpandedBlockStart.gif
{
InBlock.gif 
switch(e.Item.ItemType)
ExpandedSubBlockStart.gif 
{
InBlock.gif  
case ListItemType.Item :
InBlock.gif  
case ListItemType.AlternatingItem :
InBlock.gif  
case ListItemType.EditItem:
InBlock.gif   TableCell myTableCell;
InBlock.gif   myTableCell 
= e.Item.Cells[14];
InBlock.gif   LinkButton myDeleteButton ;
InBlock.gif   myDeleteButton 
= (LinkButton)myTableCell.Controls[0];
InBlock.gif   myDeleteButton.Attributes.Add(
"onclick","return confirm(’您是否确定要删除这条信息’);");
InBlock.gif   
break;
InBlock.gif  
default:
InBlock.gif   
break;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
5.点击表格行链接另一页
None.gif private   void  grdCustomer_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedBlockStart.gif
{
InBlock.gif 
//点击表格打开
InBlock.gif
 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
InBlock.gif  e.Item.Attributes.Add(
"onclick","window.open(’Default.aspx?id=" + e.Item.Cells[0].Text + "’);");
ExpandedBlockEnd.gif}

None.gif  
// 双击表格连接到另一页
None.gif  
// 在itemDataBind事件中
None.gif
if (e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem)
ExpandedBlockStart.gif
{
InBlock.gif 
string OrderItemID =e.item.cells[1].Text;
InBlock.gif dot.gif
InBlock.gif e.item.Attributes.Add(
"ondblclick""location.href=’../ShippedGrid.aspx?id=" + OrderItemID + "");
ExpandedBlockEnd.gif}

None.gif   
// 双击表格打开新一页
None.gif
if (e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem)
ExpandedBlockStart.gif
{
InBlock.gif 
string OrderItemID =e.item.cells[1].Text;
InBlock.gif dot.gif
InBlock.gif e.item.Attributes.Add(
"ondblclick""open(’../ShippedGrid.aspx?id=" + OrderItemID + "’)");
ExpandedBlockEnd.gif}

None.gif  特别注意:【
? id = 】 处不能为 【 ? id  =
6.表格超连接列传递参数
None.gif <asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id=’
None.gif <%# DataBinder.Eval(Container.DataItem, "数据字段1")%>’ & name=’<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>’ />
7.表格点击改变颜色
None.gif if  (e.Item.ItemType  ==  ListItemType.Item  || e.Item.ItemType  ==  ListItemType.AlternatingItem)
ExpandedBlockStart.gif
{
InBlock.gif e.Item.Attributes.Add(
"onclick","this.style.backgroundColor=’#99cc00’;
InBlock.gif
    this.style.color=’buttontext’;this.style.cursor=default’;");
ExpandedBlockEnd.gif
}
 
None.gif  写在DataGrid的_ItemDataBound里
None.gif
if  (e.Item.ItemType  ==  ListItemType.Item  || e.Item.ItemType  ==  ListItemType.AlternatingItem)
ExpandedBlockStart.gif
{
InBlock.gife.Item.Attributes.Add(
"onmouseover","this.style.backgroundColor=’#99cc00’;
InBlock.gif
   this.style.color=’buttontext’;this.style.cursor=default’;");
InBlock.gif
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=’’;this.style.color=’’;");
ExpandedBlockEnd.gif}

None.gif

8.关于日期格式

None.gif 日期格式设定
None.gifDataFormatString
= " {0:yyyy-MM-dd} "
None.gif  
// 我觉得应该在itembound事件中
None.gif
e.items.cell[ " 你的列 " ].text = DateTime.Parse(e.items.cell[ " 你的列 " ].text.ToString( " yyyy-MM-dd " ))
9.获取错误信息并到指定页面
None.gif // 不要使用Response.Redirect,而应该使用Server.Transfer
None.gif
  e.g
None.gif
//  in global.asax
ExpandedBlockStart.gif
protected   void  Application_Error(Object sender, EventArgs e)  {
InBlock.gif
if (Server.GetLastError() is HttpUnhandledException)
InBlock.gifServer.Transfer(
"MyErrorPage.aspx");
InBlock.gif
InBlock.gif
//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
ExpandedBlockEnd.gif
}

None.gif  
// Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理 
10.清空Cookie
None.gif Cookie.Expires = [DateTime];
None.gifResponse.Cookies(
" UserName " ).Expires  =   0
None.gif
web窗体状态栏内类似跑马等效果的实现
None.gif 第一步:定义一个javascript代码段 
None.gif
ExpandedBlockStart.gif
< script  language ="javascript" >  
InBlock.gif
var strMsg = "          欢迎光临本站点by loveFXNdot.gifdot.gif          "
InBlock.gif
function DoLoad(index) 
ExpandedSubBlockStart.gif

InBlock.gifwindow.status 
= strMsg.substr(index,index+10); 
InBlock.gifindex 
= (index>=strMsg.length-10?0:index+1); 
InBlock.gifwindow.setTimeout('DoLoad('
+index+')',300,"javascript"); 
ExpandedBlockEnd.gif}
 
None.gif
</ script >  
None.gif
None.gif该代码的功能是:每隔300毫秒则执行一次窗体状态栏内容的更新,并对显示内容长度作判断。 
None.gif
None.gif注意其中strMsg内的空格用来实现一段空白效果。 
None.gif
None.gif第二步: 
None.gif
None.gif在 
< body  onload ="DoLoad(0)" > 内添加函数的实现。 
None.gif
None.gif看看效果,是不是比较好玩? 
None.gif
None.gif
None.gif 遍历获取ASP.NET页面控件的名称及值 
None.gif如果直接用Page.Control 获取的到只是最顶层的页面元素,而真正的拖拉放上去的文本框或Label之类的控件,还隐藏在这些顶层页面元素的里面,所以需要再次遍历。
None.gif函数及使用方法如下,结果保存在这里选择了HashTable的方式。
None.gif
None.gif
protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gif    
{
InBlock.gif        getAllControlValue(
this);
ExpandedBlockEnd.gif    }

None.gif
None.gif    Hashtable getAllControlValue( 
object  PageOrUserControl )
ExpandedBlockStart.gif    
{
InBlock.gif        Hashtable rtn 
= new Hashtable();
InBlock.gif
InBlock.gif        
foreach (Control ctr in (PageOrUserControl as Page).Controls)
ExpandedSubBlockStart.gif        
{
InBlock.gif            getControlValue(ctr, rtn);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return rtn;
ExpandedBlockEnd.gif    }

None.gif
None.gif    
void  getControlValue(Control ctrIn,Hashtable ht)
ExpandedBlockStart.gif    
{
InBlock.gif        
foreach (Control ctr in ctrIn.Controls)
ExpandedSubBlockStart.gif        
{
InBlock.gif            Type controlType 
= ctr.GetType();
InBlock.gif
InBlock.gif            
switch (controlType.ToString())
ExpandedSubBlockStart.gif            
{
InBlock.gif                
case "System.Web.UI.WebControls.TextBox":
InBlock.gif                    TextBox controlTextBoxObj 
= (TextBox)ctr;
InBlock.gif                    
string controlTextBoxName = controlTextBoxObj.ID;
InBlock.gif                    
string controlTextBoxValue = controlTextBoxObj.Text;
InBlock.gif                    ht.Add(controlTextBoxName, controlTextBoxValue);
InBlock.gif                    
break;
InBlock.gif                
case "System.Web.UI.WebControls.Label":
InBlock.gif                    Label controlLabelObj 
= (Label)ctr;
InBlock.gif                    
string controlLabelName = controlLabelObj.ID;
InBlock.gif                    
string controlLabelValue = controlLabelObj.Text;
InBlock.gif                    ht.Add(controlLabelName, controlLabelValue);
InBlock.gif                    
break;
InBlock.gif                
//case "其他类型":
InBlock.gif                
//    其它类型 controlTextBoxObj = (其它类型)ctr;
InBlock.gif                
//    string controlTextBoxName = controlTextBoxObj.ID;
InBlock.gif                
//    string controlTextBoxValue = controlTextBoxObj.Text;
InBlock.gif                
//    ht.Add(controlTextBoxName, controlTextBoxValue);
InBlock.gif                
//    break;
InBlock.gif
                default:
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/01/266666.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值