ASP.NET學習筆記 —-常用第三方控制項

來源:互聯網
上載者:User
 

常用第三方控制項  

添加第三方控制項
在工具箱任意一個選項卡中單擊右鍵——再單擊選擇項——在選擇工具箱項中的.NET Framework 組建中——單擊瀏覽——在彈出的對話方塊中尋找要添加的第三方控制項——單擊開啟——最後單擊確定使用FreeTextBox線上編輯錄入控制項
1
、添加FreeTextBox到標準工具箱——在頁面中添加FreeTextBox控制項即可
2
FreeTextBox控制項常用屬性
3
、設定FreeTextBox控制項Text屬性實現綁定資料(必須使用Bind進行資料繫結,如果使用Eval將無法完成目錄的更新)例:

<FTB:FreeTextBox---ID="FreeTextBox1"--runat="server"--Text='<%#Bind("Name")%>'></FTB:FreeTextBox>
4
、錯誤處理
錯誤提示:從用戶端中檢測到有潛在危險的Request.From
錯誤原因:這是由於Asp.Net自身安全機制引起的,它屏蔽了有潛在危險的表單提交。
解決方案:在Page指令上設定ValidateRquest="false" 就可以了

使用驗證碼控制項
1
、添加SerialNumber控制項到標準工具箱——在頁面中添加SerialNumber控制項
2
、在後置代碼中編輯驗證代碼
//
首次載入處理
protected void Page_Load(object sender, EventArgs e){
    if (!IsPostBack) {
        this.SerialNumber1.Create(); //
首次載入頁面建立新驗證碼
    }}
//
判斷輸入的驗證碼是否正確,並做出相應的顯示
protected void Button1_Click(object sender, EventArgs e){
    if (SerialNumber1.CheckSN(this.TextBox1.Text.Trim()))
    {    this.Label1.Text = "<script>alert('
正確!');</script>";   

} else {
        SerialNumber1.Create();
    }}

常用屬性和方法

傳回型別

方法名

說明

void

Create()

建立新驗證碼

bool

CheckSN(比較參數)

驗證輸入的驗證碼是否正確

session 產生驗證碼
1
、編寫產生的驗證碼頁面(GenerateSureCode.aspx)
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;
using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Drawing;
public partial class GenerateSureCode : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e)  {
        if (!Page.IsPostBack)     {
            this.GenImg(this.GenCode(4));
        }   }
    //
產生隨機字串
    private string GenCode(int num)    {
        string[] source ={"0","1","2","3","4","5","6","7","8","9",
                         "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
                   "O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
        string code = "";
        Random rd = new Random();
        for (int i = 0; i < num; i++)    {
            code += source[rd.Next(0, source.Length)];
        }
        return code;    }
    //
產生圖片
    private void GenImg(string code)    {
        Bitmap myPalette = new Bitmap(60, 20);//
定義一個畫板
        Graphics gh = Graphics.FromImage(myPalette);//
在畫板上定義繪圖的執行個體
        Rectangle rc = new Rectangle(0, 0, 60, 20);//
定義一個矩形
        gh.FillRectangle(new SolidBrush(Color.Yellow), rc);//
填充矩形
        gh.DrawString(code, new Font("
宋體", 16), new SolidBrush(Color.Red), rc);//在矩形內畫出字串
   myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//
將圖片顯示出來
        Session["ValidateCode"] = code;//
將字串儲存到Session中,以便需要時進行驗證
        gh.Dispose();     myPalette.Dispose();
    }     }

2在頁面中加一個Image圖片控制項(通過ImageUrl屬性獲得在GenerateSureCode.aspx頁面產生的驗證碼)
<asp:Image ID="imgValidate" ImageUrl="~/GenerateSureCode.aspx" runat="server" />
3
、在後置代碼中編寫如下代碼
    //
此方法驗證使用者輸入的驗證碼是否與產生的驗證碼一樣
    private bool CheckSN() {
      string validateCode = Session["ValidateCode"].ToString();//
獲得儲存在Session中的驗證碼
        if (this.txtValidate.Text != validateCode)   {
            Response.Write("<script>alert('
驗證碼輸入錯誤,請重新輸入!');</script>");
            txtValidate.Text = "";
            return false;
        }   else  {
            return true;
        }    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.