.net 小知识

来源:互联网 发布:昆明中药厂网络商学院 编辑:程序博客网 时间:2024/05/16 14:12

1、if(!Page.IsPostback){} 使用该函数,使网页仅在第一次加载时运行函数体的内容;。

2、GridView的字段(BoundField)有一个属性HtmlEncond,要使字段内的图片链接显示出来,应设置HtmlEncode="true"。

3、网页中获取日期后进行格式化的方法:Eval("wtDate", "{0:yyyy-MM-dd}"),但在DateTime.Now.Date.ToString("yyyy-MM-dd") 中的字符串格式化却有所不同。

4、将字符串类型转换为Guid类型,Guid gd = New Guid(string); Guid类型的空值为Guid.Empty

5、对记录数进行统计,使用类似下面的语句: int cout =(int)cmd.ExecuteScalar();

6、存储过程中,通配符与参数结合使用,要采用字符串加法:WHERE RTRIM(f.ftTitle) LIKE '%'+RTRIM(@searchkey)+'%'

7、获取list<T>对象中某个项的值:List<ftInfo> CurrentFault = new List<ftInfo>(); CurrentFaut[0].ftIsResolved

8、C#中关闭窗口:Response.Write("<script language:javascript>javascript:window.close();</script>");

9、DateTime 取当前年月日时分秒
System.DateTime currentTime=new System.DateTime();
currentTime=System.DateTime.Now;
int 年=currentTime.Year; //[年Year,月Month,日Day,时Hour,分Minute,秒 Second,毫秒Millisecond]

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、取字串长度:int Len = str.Length

12、字码转换 转为比特码 System.Text.Encoding.Default.GetBytes(变量)

13、截取字串的一部分:string s1 = str.Substring(0,2)

14、取远程用户IP地址:String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();

15、 Session["username"]="小布什"; 取值: Object objName=Session["username"].ToString(); 清空: Session.RemoveAll();
用session判断是否登陆 if(Sessi on["login"] == null) { Response.Redirect("error.aspx"); }

16、地址栏传值:<a href=Edit.aspx?fbid="变量"> String str=Request.QueryString["变量"];

17、字符串相加: System.Text.StringBuilder("") ;sb.Append("中华");

18.清空Cookie Cookie.Expires=[DateTime]; Response.Cookies("UserName").Expires = 0

19、创建XML文档新节点:DOC对象.CreateElement("新建节点名");
19.1、 将新建的子节点加到XML文档父节点下:父节点.AppendChild(子节点);
19.2、删除节点 :父节点.RemoveChild(节点);

20、向页面输出: Response.Write("字串"); Response.Write(变量);
跳转到:Response.Redirect("URL地址"); 获取错误信息并到指定页面不使用Response.Redirect,而使用Server.Transfer

21、查指定位置是否空字符:char.IsWhiteSpce(字串变量,位数)--逻辑型
21.1、查字符是否是标点符号:char.IsPunctuation('A');

22、把数字和字符可互换:Response.Write((char)22269);Response.Write((int)国);

23、 清除字串前后空格 字串变量.Trim()
23.1 、字串替换: 字串变量.Replace("子字串","替换为")
23.2、取最大值 Math.Max(i,j)
23.3、 在字串中指定索引位插入指定字符: str1="中国"str1.Insert(1,"字");结果为中字国

24、字串对比: if(str1==str2){ }
str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值
Equals(str1,str2);str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值。

25、查找字串中指定字符或字串首次(最后一次LastIndexOf() )出现的位置:str1.IndexOf("字串",[3],[2])第几个 几个字

26、在字串左(或右PadRight() )加空格或指定char字符,使字串达到指定长度:str1="中国人"; str1=str1.PadLeft(10,'1');结果为"1111111中国人"

27. 打开新的窗口并传送参数: response.write("<script>window.open('*.aspx?id="+aa+"&id1="+...+"')</script>")
接收参数:string a = Request.QueryString("id");

28.为按钮添加对话框 Button1.Attributes.Add("onclick","return confirm('确认?')");

29、删除表格选定记录
int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];
string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString()

29.1、删除表格记录警告
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;
}
}

29.2、点击表格行链接另一页
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 + "');");
}

30、表格超连接列传递参数
<asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id='<%# DataBinder.Eval(Container.DataItem, "数据字段1")%>' & name='<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>' />

31、Panel 横向滚动,纵向自动扩展
<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>

 

原创粉丝点击