常用小代碼

来源:互联网 发布:广电网络营业厅 编辑:程序博客网 时间:2024/06/07 20:51
 

1.Server.MapPath("../PDF/")
2.GridView中 <asp:TemplateField HeaderText="出貨單號&lt;br&gt;項次">的
  &lt;br&gt;在html頁是:"<br>"
3.ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();//獲取前一頁的uri.
4.常用的一些html替換:
      str = str.Replace("&", "&amp;");
        str = str.Replace("<", "&lt;");
        str = str.Replace(">", "&gt;");
        str = str.Replace("\n", "<br>");
        str = str.Replace("\"", "&quot;");
        str = str.Replace(" ", "&nbsp;");

5.時間格式化:
5.1. DataField="process_date" HeadingText="最後辦理時間"
DataFormatString="{0:yyyy/MM/dd HH:mm}  />

5.2.<%# String.Format("{0:yyyy/MM/dd}", DataBinder.Eval(Container.DataItem, "receive_date")) %>

6.正則匹配:Regex.IsMatch(filename, @"^\d{4}.*20\d{12}$")

7.全局多語言的讀取
btnExit.Text = Resources.TrustLinkGeneralSampleFormRes.ResourceManager.GetString("IDE_BTN_EXIT",AP_culture);

8.string lan = new ClassSessionAccess().Language;
        lblEANoTitle.Text = (lan == "zh-TW") ? "單號:" : "Bill No:";

9.cursor:point 鼠標的形狀.

10.javascript傳中文用encodeURIComponent("")加密或encodeURI("");後臺直接傳值可以接收

11. string strSub = "abcdef";
    strSub.IndexOf("c"):值為2;
    strSub.Substring(2):cdef;
    strSub.Substring(strSub.IndexOf("c")):cdef;

12.this.select()選中該文本框的內容

13.用在gridview中,加行字用于javascript的作用
<a onclick="SelectUserName('<%# DataBinder.Eval(Container.DataItem,"user_name") %>');"                                 onmouseover="this.style.cursor='hand';">
<%# DataBinder.Eval(Container.DataItem, "user_name") %></a>

14.打開對話框:
showModalDialog(url,p,"dialogWidth:540px;help:no;status:no;dialogHeight:160px;scroll:true;");
關閉的時候:window.close();
傳回值:window.returnValue=;

15.依照預設,SessionID 值會儲存在 Cookie 中。但是,您也可以設定應用程式,將 "cookieless" 工作階段的 SessionID 值儲存在 URL 中。

16.string.Join(string separator,string[],第幾個,取幾個)//將string數組用分隔號串成一個字符串

17.獲取sessionId:
System.Web.HttpContext.Current.Session.SessionID;

18.問題:SortedList與IDictionary的區別

19.心得:可以從sql數據庫表複製一部分內容到excel,它是兼容的

20.后臺調用前臺方法: string strscript = "<script language='javascript'>";
            strscript += "Sign('" + userName + "')</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "sign", strscript);

21. 在提交頁面之后,保持滾動條的位置
可以在page指令上加上MaintainScrollPositionOnPostback指令
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeFile="..." Inherits="..." %>
22. 在頁面載入完之后,將焦點移動到某個控件,只需要指定Form的DefaultFocus屬性就可以了。
<form. id="frm" DefaultFocus="txtUserName" runat="server">
  ...
id="frm" DefaultButton="btnSubmit" runat="server">
  ...
id="form1" runat="server" DefaultFocus="formVw$txtName">
    <div>
        <asp:FormView ID="formVw" runat="server">
            <ItemTemplate>
                Name:
                <asp:TextBox ID="txtName" runat="server"
                    Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' />
            </ItemTemplate>
        </asp:FormView>
    </div>
</form>
在上面的例子中使用form的DefaultFocus屬性指定頁面載入時焦點所在的控件,使用$符號就可以輕鬆的定位txtName
也可以使用以下代碼來輕鬆的找到控件
TextBox tb = this.FindControl("form1$formVw$txtName") as TextBox;
if (tb != null)
{
    //Access TextBox control
}
25. 關於跨頁提交的取得發出提交頁面控件強類型的方法,見原文
26. 使用強類型訪問MasterPage屬性成員的方法,見原文

27.重新註冊IIS:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -i

28.js隨機數:Math.floor(Math.random()*100+1)