webform小技巧
1. SQL中如果字段内容中有单引号('),必须替换成两个单引号('')
2. Response.Write ("<script>alert('按确定返回!');window.close()</script>");
return;
3.DateTime.Parse(变量) /判断日期选择是否合法
4.先替换数据,再分解字符串放入数组,然后遍历数组
DDi=DDi.Replace(" and ",",");
DDi=DDi.Replace(" or ",",");
string [] DDis=Convert.ToString(DDi).Split(',');
foreach (string d in DDis)
{
语句;
}
5.DateTime.Now.ToString();//当前时间
6.Server.Transfer("地址"); 转向
Response.Redirect("地址");
7.Response.Write ("<script>alert('发言不能为空');location.href='w2.aspx'</script>");
8.Response.Write ("<script>history.back()</script>");
9.M² =M2 (平方米)
10.格式化HTML页面的字段格式(日期型):
<%# string.Format("{0:d}",DataBinder.Eval(Container.DataItem,"字段")) %>
<%# DataBinder.Eval(Container.DataItem,"字段","{0:d}") %>
11.使用html来显示网页列表信息
html页面:<span id="mblist" runat="server"></span>
CS页面:mblist.InnerHtml+="<table>....</table>";
下面是分页时使用的
<asp:linkbutton id="First" runat="server" CommandArgument="first" OnCommand="PagerButtonClick" Text="首页">
12.按钮控件.Attributes.Add("onclick","return confirm('您确定删除?')");
//按确定删除,按取消不继续执行下一条语句
13.<%# DataBinder.Eval(Container.DataItem,"Titles" %> //限制Title字段的只能显示20个字
使用SQL语句Select top 10 ID,Left(Title,20) AS Titles,UpdataDateTime from tbKnowledge where DocType=0 order by ID DESC
把限制Title的字节给别名.在C#再给绑定别人Titles
14.在一个框架页面中如何将数据传到另一个框架页面,并更新该框架页面
Response.Write("<script language='javascript'>window.open('WebForm1.aspx','menu');</script>");
15.模板列自定义控件绑定数据库,以DropDownList控件为例:
首先定义实例:protected System.Web.UI.WebControls.DropDownList DDL;
在DataGrid1_ItemDataBound方法写入代码如下:
DDL = (DropDownList)(e.Item.Cells[3].Controls[1]);
string SQL = "SELECT ID,AgentID FROM tbAgents ORDER BY ID";
Ds=conn.Db_DataSet(SQL);
DDL.DataSource = Ds;
DDL.DataTextField = "AgentID";
DDL.DataValueField = "ID";
DDL.DataBind();
conn.close();
16.在html加入缓存<%@ OutputCache Duration="60" VaryByparam="none"%>
17.DataGrid的链接可以做成 window.open 方式"javascript:win=window.open('webform1.aspx?ID={0}',null,'width=778,height=650,scrollbars=yes');window.close();"
18.DataGrid中把链接方式设为 target=_new 通过点击连接永远只打开一个窗口