Attributes.Add用途與用法


Attributes.Add("javascript事件","javascript語句");

如:

this.TextBox1.Attributes.add("onblue", "window.Label1.style.backgroundColor='#000000';");

this.TextBox1.Attributes.Add("onblur","this.style.display='none'");

javascript事件:

onClick     鼠標點擊事件,多用在某個對象控制的范圍內的鼠標點擊

onDblClick    鼠標雙擊事件

onMouseDown    鼠標上的按鈕被按下了

onMouseUp    鼠標按下后,松開時激發的事件

onMouseOver 當鼠標移動到某對象范圍的上方時觸發的事件

onMouseMove    鼠標移動時觸發的事件

onMouseOut 當鼠標離開某對象范圍時觸發的事件

onKeyPress    當鍵盤上的某個鍵被按下並且釋放時觸發的事件.[注意:頁面內必須有被聚焦的對象]

onKeyDown    當鍵盤上某個按鍵被按下時觸發的事件[注意:頁面內必須有被聚焦的對象]

onKeyUp   當鍵盤上某個按鍵被按放開時觸發的事件[注意:頁面內必須有被聚焦的對象]

Attributes.Add添加多了之后會影響一定速度,Attributes和Attributes.CssStyle被自動保存到ViewState中后,除了ViewState體積急增后,PostBack時Load ViewState的負擔也同時增大了。

 在下面的事件中添加,如下形式:
protected override void Render(HtmlTextWriter output)
{
    this.Attributes["abc"] = "123";
    this.Attributes.CssStyle["abc-style"] = "123-style";
    output.Write(Text);
}
  就不會再將Attributes和Attributes.CssStyle保存到ViewState中

引自:http://hi.baidu.com/sbdnpgk/blog/item/617ddb1a8b0de1128618bfbc.html

Button1.Attributes.Add()方法小結 
//首先要在PageLoad()事件中注冊屬性
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1.Attributes.Add("onclick", "return checkSame()");//為Button1添加onclick()事件 ,Button為服務器控件
        }//注意:checkSame()這是一個寫在aspx面頁的js函數,必須有返回值,為:true 或 false
    }

//接着寫Button1的onclick事件,如果剛才的checkSame()返回為true則招行下面的事件,否則不執行 

    protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
        SqlParameter[] Params = new SqlParameter[2];
        Params[0] = dbs.MakeInParams("@uid", SqlDbType.VarChar, 10, Session["Uid"].ToString());
        Params[1] = dbs.MakeOutParms("@Upwd", SqlDbType.VarChar, 10);
        if (dbs.ExecuteNonQuery(CommandType.StoredProcedure, "selectPwd", Params) > 0)
        {
            string userPwd = Params[1].Value.ToString();
            if (userPwd != this.old_pwd.Text)
            {
                Response.Write("<script>alert('原始密碼錯誤!')</script>");
            }
            else
            {
               
            }
        }
        else 
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失敗!')</script>");
        }

    }

//呵呵。。再寫一個js試例吧
      function checkSame()
    {
      var Obj1=document.getElementById ("new_pwd").value;
      var Obj2=document.getElementById ("re_new_pwd").value;
      
      if(Obj1!=Obj2)
       {
          alert("兩次密碼輸入不一致!");
          document.getElementById("new_pwd").focus();
          return false;
       }
       else
       {
         return true;
       }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM