1.为按钮添加确认对话框
Button.Attributes.Add("onclick","return confirm('确认?')");
Button.Attributes.Add("onclick","if(confirm('确定?')){return true;}else{return false;}")
2.表格超连接列传递参数
<asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id='<%# DataBinder.Eval(Container.DataItem, "数据字段1")%>'&name='<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>'/>
3.表格点击改变颜色
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';");
}
4.清空Cookie
Cookie.Expires=[DateTime];
Response.Cookies("UserName").Expires = 0;
5.Panel 横向滚动,纵向自动扩展
<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>
6.数字格式化
<%#Container.DataItem("price")%> 结果:500.0000格式化:500.00
<%#Container.DataItem("price","{0:¥#,##0.00}")%>
int i=123456;
string s=i.ToString("###,###.00");
7.日期格式化
<%# DataBinder.Eval(Container.DataItem,"Date")%> 结果:2004-8-11 19:44:28 格式化:2004-8-11
<%# DataBinder.Eval(Container.DataItem,"Date","{0:yyyy-M-d}")%>
8.时间格式化
string aa=DateTime.Now.ToString("yyyy年MM月dd日");
当前年月日时分秒 currentTime=System.DateTime.Now;
当前年 int 年= DateTime.Now.Year;
当前毫秒 int 毫秒= DateTime.Now.Millisecond;
9.自定义分页代码
public static int pageCount; //总页面数
public static int curPageIndex=1; //当前页面
if(ccDataGrid.CurrentPageIndex<(ccDataGrid.PageCount - 1))
{//下一页
ccDataGrid.CurrentPageIndex += 1;
curPageIndex+=1;
}
bind(); // ccDataGrid数据绑定函数
if(ccDataGrid.CurrentPageIndex>0)
{ //上一页
ccDataGrid.CurrentPageIndex += 1;
curPageIndex-=1;
}
bind(); // ccDataGrid数据绑定函数
int a=int.Parse(JumpPage.Value.Trim());//JumpPage.Value.Trim()为跳转值
if(a<DataGrid1.PageCount)
{ //直接页面跳转
this.ccDataGrid.CurrentPageIndex=a;
}
bind(); // ccDataGrid数据绑定函数
10.变量.ToString()
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
11.客户端验证控件
//验证空值
<asp:requiredfieldvalidator id="valUsername" runat="server" controltovalidate="txtUsername" display="None" errormessage="请输入用户名 !!"></asp:requiredfieldvalidator>
//验证网址
<asp:regularexpressionvalidator id="rev" runat="server" ErrorMessage="公司网址不合法[要有http://] !" Display="None" ControlToValidate="txtCPWebsite" ValidationExpression="http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:regularexpressionvalidator>
//验证邮箱
<asp:RequiredFieldValidator id="rfv" runat="server" ControlToValidate="txtCPEmail" Display="None" ErrorMessage="请输入电子邮箱 !!"></asp:RequiredFieldValidator>
//验证邮编
<asp:regularexpressionvalidator id="rev5" runat="server" ErrorMessage="邮政编码不合法 !" Display="None" ControlToValidate="txtCPPostCode" ValidationExpression="\d{6}"></asp:regularexpressionvalidator>
//显示错误信息
<asp:validationsummary id="vs" runat="server" ShowSummary="False" ShowMessageBox="True"></asp:validationsummary>
12.DataBinding绑定表达式
以该oil paintings网站案例来说,它的产品页面都调用了DataBinding绑定表达式
1) 普通的绑定表达式
<%# DataBinder.Eval(Container.DataItem, "ContactName") %>
2) 文本+绑定表达式
<asp:Label id=lblDate runat="server" Text='<%# "[" + DataBinder.Eval(Container, "DataItem.NewsCreatedate") + "]" %>' ForeColor="Red"></asp:Label>
3) 同时带有显示格式的绑定表达式
<%# DataBinder.Eval(Container,"DataItem.USActiveDate","{0:yyyy-MM-dd}") %>
4) 结合绑定表达式和模态框
<A href='<%# ShowModalWin(Convert.ToString(DataBinder.Eval(Container.DataItem, "PictureImage")),Convert.ToString(DataBinder.Eval(Container.DataItem, "DetailID")),Convert.ToString(DataBinder.Eval(Container.DataItem, "PictureID")))%>'>
其中:后台代码文件中ShowModalWin()方法的定义如下:
protected string ShowModalWin(string PictureImage,string DetailID,string PictureID)
{
return " window.showModalDialog(\"Customers/ShowPictureInfo.aspx?pid="+PictureImage+"&did="+DetailID+"&id="+PictureID+"\",\"\",\"dialogHeight:320px;dialogWidth:480px;center:yes;help:no;status:no;scroll:no\");";
}
或者将参数提取出来单独定义成一变量:
const string WINDOWPARAMSTRING="dialogWidth:540px;dialogHeight:420px;help:0;status:0;resizeable:1;scroll:no";
Page.RegisterStartupScript("functionscript","<script language='javascript'>window.showModalDialog('EditUserService.aspx?URID="+iURID+"','','"+WINDOWPARAMSTRING+"')</script>");
13.html字符转换的两个函数
public string Encode(string str)
{
str=str.Replace("&","&");
str=str.Replace("'","''");
str=str.Replace("\"",""");
str=str.Replace(" "," ");
str=str.Replace("<","<");
str=str.Replace(">",">");
str=str.Replace("\n","<br>");
return str;
}
public string Decode(string str)
{
str=str.Replace("\n","<br>");
str=str.Replace(">",">");
str=str.Replace("<","<");
str=str.Replace(" "," ");
str=str.Replace(""","\"");
return str;
}