熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

讓UserControl 成為 Asp.Net ajax

2022-06-13   來源: .NET編程 

  很多時候我們需要用到User Control將部份UI或業務邏輯包裝下面將UserControl包裝成AspNet ajax 控件:
簡單示例:
(ASCX) 這一段代碼就不解釋了:

  
  <table>
      <tr>
          <td>
              Login Name:
          td>
          <td>
              <asp:TextBox ID=UserName Ruant=Server>asp:TextBox>
          td>
     tr>
     <tr>
         <td>
             Password:
         td>
         <td>
             <asp:TextBox ID=Password TextMode=Password Ruant=Server>asp:TextBox>
         td>
     tr>
table>

  (LoginPaneljs)

   <script type=text/javascript>
      ///
      TyperegisterNamespace(CsharpFarmer);
      CsharpFarmerLoginPanel= function(element) {
          CsharpFarmerLoginPanelinitializeBase(this [element]);
          thisuserName = null;
          thispassword = null;
      }
      CsharpFarmerLoginPanelprototype = {
         initialize: function() {
             CsharpFarmerLoginPanelcallBaseMethod(this initialize);
             // Add custom initialization here
         }
         get_userName: function() {
             return thisuserName ;
         }
         set_userName: function(value) {
             thisuserName = value;
         }
         get_password: function() {
             return thispassword ;
         }
         set_password: function(value) {
             thispassword = value;
         }
         dispose: function() {
             //Add custom dispose actions here
             CsharpFarmerLoginPanelcallBaseMethod(this dispose);
             delete thisuserName;
             delete thispassword;
         }
     }
     CsharpFarmerLoginPanelregisterClass(CsharpFarmerLoginPanel SysUIControl);
</script>

  (控件相對應的js注意get和set方法必須成對出現與屬性以_隔開)至於為什麼只是一種規定查看MsAjax:
中:Sys$Component$_setProperties:

  Code
function Sys$Component$_setProperties(target properties) {
    ///
    ///
    ///
    var e = Function_validateParams(arguments [
        {name: target}
        {name: properties}
    ]);
    if (e) throw e;
    var current;
    var targetType = ObjectgetType(target);
    var isObject = (targetType === Object) || (targetType === SysUIDomElement);
    var isComponent = SysComponentisInstanceOfType(target) && !targetget_isUpdating();
    if (isComponent) targetbeginUpdate();
    for (var name in properties) {
        var val = properties[name];
        var getter = isObject ? null : target[get_ + name];
        if (isObject || typeof(getter) !== function) {
            var targetVal = target[name];
            if (!isObject && typeof(targetVal) === undefined) throw ErrorinvalidOperation(Stringformat(SysRespropertyUndefined name));
            if (!val || (typeof(val) !== object) || (isObject && !targetVal)) {
                target[name] = val;
            }
            else {
                Sys$Component$_setProperties(targetVal val);
            }
        }
        else {
            var setter = target[set_ + name];
            if (typeof(setter) === function) {
                setterapply(target [val]);
            }
            else if (val instanceof Array) {
                current = getterapply(target);
                if (!(current instanceof Array)) throw new ErrorinvalidOperation(Stringformat(SysRespropertyNotAnArray name));
                for (var i = j = currentlength l= vallength; i < l; i++ j++) {
                    current[j] = val[i];
                }
            }
            else if ((typeof(val) === object) && (ObjectgetType(val) === Object)) {
                current = getterapply(target);
                if ((typeof(current) === undefined) || (current === null)) throw new ErrorinvalidOperation(Stringformat(SysRespropertyNullOrUndefined name));
                Sys$Component$_setProperties(current val);
            }
            else {
                throw new ErrorinvalidOperation(Stringformat(SysRespropertyNotWritable name));
            }
        }
    }
    if (isComponent) targetendUpdate();
}

  LoginPanelcs

   using System;
  using SystemCollectionsGeneric;
  using SystemLinq;
  using SystemWeb;
  using SystemWebUI;
  using SystemWebUIWebControls;
  using SystemText;
 
  ///
/// LoginPanel
///
public partial class LoginPanel : SystemWebUIUserControl IScriptControl
{

     protected void Page_Load(object sender EventArgs e)
     {

     }

     protected override void Render(HtmlTextWriter writer)
     {
         writerAddAttribute(HtmlTextWriterAttributeId thisClientID);
         writerRenderBeginTag(Div);
         baseRender(writer);
         writerRenderEndTag();
     }

     protected override void OnPreRender(EventArgs e)
     {
         baseOnPreRender(e);
         var sm = ScriptManagerGetCurrent(thisPage);
         smRegisterScriptControl<LoginPanel>(this);
         smRegisterScriptDescriptors(this);
     }

    
     #region IScriptControl

     public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
     {
         var descriptor = new ScriptControlDescriptor(CsharpFarmerLoginPanel thisClientID);
         descriptorAddElementProperty(userName UserNameClientID);
         descriptorAddElementProperty(password PasswordClientID);
         yield return descriptor;
     }

     public IEnumerable<ScriptReference> GetScriptReferences()
     {
         var sr = new ScriptReference(~/LoginPaneljs);
         yield return sr;
     }

     #endregion
}

這一段代碼中應當注意

  必須實現IScriptControl接口
另個OnPreRender 和 Render 方法應注意
在OnPresRender裡注冊這個控件:
對於一個可視化的控件必段承載在HTML元素之上所以
在Render方法中將其放入Div中注意ID
 
當我們把控件放入Aspx page 時:
我們在js中可以得用$find(id) 得到這個控件:


From:http://tw.wingwit.com/Article/program/net/201311/11389.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.