ASP.NET常用技巧总结

 

1、   //获取当前页传输控件的页

         if (Page.PreviousPage != null)
         
{
             
string a = (TextBox)Page.PreviousPage.FindControl("TextBox!").text;
         }


2、加在<head>外     <!-- #Include File="border.html" -->两个页中的id不能重复等

3、

<asp:Label ID="Label2" runat="server" ie:Text="IE 浏览器" mozilla:Text="Mozilla 或 Firefor 浏览器" Style="position: relative"></asp:Label><br />

 

4.      //Url传递中文参数解决方案

 

    //解码html
        // Response.Write(Server.HtmlDecode("aaaaaaaaaaaaaaaaaaaaa
<font >ass&nbsp;&nbsp;ssssss</font>"));

        // 
<%# Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem,"内容")) %>

//Url传递中文参数解决方案 
a href="Admin_SmallSort.aspx?iProductBigSort_ID=
<%Eval("iProductBigSort_ID")%>&sProductBigSort=<%# Server.UrlEncode(Eval("sProductBigSort").ToString())%>">

<asp:HyperLink ID="HyperLink1" runat="server"  
                                    NavigateUrl
='<%# "~/DeleteUser.aspx?UserID="+ Server.UrlEncode((string)Eval("UserID")) + "&UserName=" + Server.UrlEncode((string)Eval("UserName")) %>' >

 

<%#   GetEncode(DataBinder.Eval(Container.DataItem,   "Name").ToString())%>    
    
  
public   string   GetEncode(string   str)   
  
{   
        
return   Server.UrlEncode(str);   
        
//return   HttpUtility.UrlEncode(str);   
  }


                                   
5.         //获取上一个页面控件的值

         if (Page.PreviousPage!=null)
         
{
             TextBox a 
= (TextBox)Page.PreviousPage.FindControl("TextBox1");
             a.Text; 
         }

 

6。 使用 foreach 显示整数数组的内容    

 

         int[] fibarray = new int[] 01235813 };
         
foreach (int i in fibarray)
         
{
             System.Console.WriteLine(i);
         }

 

7.       执行break语句,跳出swith语句。如果没有break语句,所以的语句都会执行。
   

string sWork = "a|b|c";
         
string[] Gz=sWork.Split(new char[] {'|'});
         
foreach (string s in Gz)
         
{
             System.Console.WriteLine(s);
         }

只能在 
whiledo...whilefor 或 for...in 循环内使用 continue 语句。执行 continue 语句会停止当前循环的迭代,并从循环的开始处继续程序流。这将对不同类型的循环有如下影响: 

 

8.
//比较日期大小

DateTime t1 = DateTime.Now; //
DateTime t2 = Convert.ToDateTime("9:10:59");
int a = DateTime.Compare(t1, t2); //a=1,t1大

//从此实例中减去指定的日期和时间Subtract

DateTime t1 
= System.DateTime.Now; //系统时间
DateTime t2 = Convert.ToDateTime(Row["dtime"].ToString()); //最后在线时间
TimeSpan d3= t2.Subtract(t1);
int a = d3.Minutes;   //t2与t1的差几分钟 获取由当前 TimeSpan 结构表示的整分钟数。

 

9.//DropDownList1 添加数据

DropDownList1.Items.Add(new ListItem("02""02"));
DropDownList1.Items.Add(
new ListItem( string Text,string Value))

 

10. 先提示后跳转

Response.Write("<script>alert('aa');window.location=right1.aspx</script>")   
// Response.Redirect("~/Login.aspx",true); true -不执行后面的内容(但alert不行) ,false则执行,

 

11.
一个页面,该页面上两个按钮和一个容器控件(Panel),一按钮为生成控件,一按钮名为取值
一个用户控件,该控件上一个TextBox控件
-------
点生成按钮,生成十个用户控件,
点取值按钮,取刚生成的这十个用户控件中,TextBox的值

其中,生成十个用户控件代码,

protected void Button1_Click(object sender, EventArgs e) 

for (int i = 0; i < 9; i++

Control c 
= Page.LoadControl("WebUserControl.ascx"); 
this.Panel1.Controls.Add(c); 
}
 
}
 
取值时,不会了 
protected void Button2_Click(object sender, EventArgs e) 

//取刚生成的这十个用户控件中的,TextBox的值?????? 
}

1.foreach 循环遍历窗体所有控件 
2.FindControl("");

 

12、设置了50M ~~最大上传量,90秒上传时间

 

<system.web>
       
<httpRuntime maxRequestLength="50000"     useFullyQualifiedRedirectUrl="false"
                     executionTimeout
="90"
                    
/>
     
<compilation debug="true"/>

 

13 ‘在绑定数据时经常会用到这个句程序

 

<%# DataBinder.Eval(Container.DataItem,"xxxx")%>
或者
<%# DataBinder.Eval(Container,"DataItem.xxxx")%> 
或者
<%#Eval("列名")%>

 

今天又学到一种,而且微软也说这种方法的效率要比以上两种高。 

<%# ((DataRowView)Container.DataItem)["xxxx"]%> 

很有用的,这样可以在前台页面做好多事情了。 

还要记住要这样用必须要在前台页面导入名称空间System.Data,否则会生成错误信息。 

<%@ Import namespace="System.Data" %> 

这种用法其实和
<%# ((DictionaryEntry)Container.DataItem).Key%>是一个道理。 

 

14         //比较两个字符串是否相等,true表示不区分大小写

        int i = string.Compare("aaaaaaa""aaaaaaa"true); 
        
if (i == 0)
        
{
            Response.Write(
"相等");
        }

        
if (i==1)
        
{
            Response.Write(
"不相等");
        }


15、字符串加密
   

 

 public string EncryptPassword(string PasswordString,string PasswordFormat ) 
          ...

              
if (PasswordFormat="SHA1")
               ...

                     EncryptPassword
=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1"); 
                }
 
              
else if   (PasswordFormat="MD5"
               ...
{
                     EncryptPassword
=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5"); 
                }

              
else
              ...
{

              }

          }


     
public string md5(string str, int code)
     ...
{
         
if (code == 16//16位MD5加密(取32位加密的9~25字符) SHA1还可以换成MD5
         ...{
             
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower().Substring(816);
           
//   md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower().Substring(8, 16);
        
         }


         
if (code == 32//32位加密 
         ...{
             
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();

           
//   md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
         }

         
//必须每种情况都要有返回值。如果code<>16,32 则返回的值
        return "a";
     }
 


16    // String.Format()可以替换相关项,代码如下,其中{}为此方法认别的替换符:

 

     //单个字符替换如下: 
     String RepString   = String.Format("Format 是C#String类常用功能之一,{0}","谢谢观赏!"); 
     
//RepString 的值为:Format 是C#String类常用功能之一,谢谢观赏! 
     
//两个或者两个以上 
     String RepString = String.Format("String.Format(),{2},{1},{0}","谢谢观赏!","我们在学习和工作中要灵活运用","是C#常用方法之一"); 
     
//RepString的值为:String.Format(),是C#常用方法之一,我们在学习和工作中要灵活运用,谢谢观赏! 
     
//总结:前面有几个{},后面就跟几个参数。以此类推,后面的参数可以是无限。 

     
17     //判断字符串是否为空String.IsNullOrEmpty(str) == True   则说明字符串为空(null 和 "");

     //string.IsNullOrEmpty("");
         TextBox1.Text = "aaa";
     
if (TextBox1.Text == string.Empty)   // TextBox1.Text=null 或 ="" 都为true
     {
         Response.Write(
"");
     }

     
else
     
{
         Response.Write(
"非空");
          }

 

    
18    //指定索引位置插入一个指定的 String 实例

     string a = "aaa";
     
string b=a.Insert(1"b"); 
     Response.Write(b);   
//abaa
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值