Sprinig.net 双向绑定 Bidirectional data binding and data model management 和 UpdatePanel

 Spring.Web data binding is very easy to use. Simply override the protected 

InitializeDataBindings 

method

and configure data binding rules for the page. You also need to override three model management methods:

InitializeModel, LoadModel and SaveModel.

 This process is illustrated by an example from the SpringAir

reference application. [from spring.net 1.3 document ]

 

 双向绑定事件触发顺序

呈现页面时(第一次加载页面): 

  1.  InitializeDataBindings 
  2.  InitializeModel
  3. Page_Load
  4.  SaveModel

 

回送PostBack时:

  1. InitializeDataBindings
  2.  LoadModel(此处不可以看到回送的新数据)  
    [miracle happens here. ]
  3.  Page_Load(此处可以看到回送的新数据)
  4. 指定事件(btnxx_click etc.) 
  5.  SaveModel

 

e.g.

 <asp:UpdatePanel ID="UpdatePanel2" runat="server">

     < ContentTemplate >
        
< asp:DropDownList  ID ="DropDownList2"  runat ="server"  EnableViewState ="true" >
            
< asp:ListItem  Text ="11"  Value ="11" ></ asp:ListItem >
            
< asp:ListItem  Text ="22"  Value ="22" ></ asp:ListItem >
            
< asp:ListItem  Text ="33"  Value ="33" ></ asp:ListItem >
        
</ asp:DropDownList >
        
< asp:Button  ID ="btnTest"  runat ="server"  OnClick ="btnTest_Click"  class ="pri_button"
            Text
="提交整页"   />
    
</ ContentTemplate >
</ asp:UpdatePanel >

 

 cs:

   protected void btnTest_Click(object sender, EventArgs e)
    {
        string s = this.DropDownList2.SelectedValue;
        
    }

    public T7dDrawnumInfo Info { get; set; }

    protected override void InitializeDataBindings()
    {

        //BindingManager.AddBinding("Select1.Value", "Info.Number1");
        BindingManager.AddBinding("DropDownList2.SelectedValue", "Info.Number2");

    }
    protected override void InitializeModel()
    {
        Info = new T7dDrawnumInfo();
        Info.Number2 = "22";
    }

    protected override void LoadModel(object savedModel)
    {
        Info = (T7dDrawnumInfo)savedModel;
    }
    protected override object SaveModel()
    {
        return Info;
    }

 

 

 config:

 <?xml version="1.0" encoding="UTF-8"?>

< configuration >
    
< configSections >
        
< sectionGroup  name ="spring" >
            
< section  name ="context"  type ="Spring.Context.Support.WebContextHandler, Spring.Web"   />
            
< section  name ="objects"  type ="Spring.Context.Support.DefaultSectionHandler, Spring.Core"   />
        
</ sectionGroup >     
        
< section  name ="log4net"  type ="System.Configuration.IgnoreSectionHandler"   />
        
< sectionGroup  name ="common" >   
            
< section  name ="logging"  type ="Common.Logging.ConfigurationSectionHandler, Common.Logging"   />
        
</ sectionGroup >            
        
< sectionGroup  name ="customConfig" >               
            
< section  name ="allowlevels"  type ="game.Common.ConfigurationSectionHandler, DProject.Common"   />
        
</ sectionGroup >       
        
< sectionGroup  name ="system.web.extensions"  type ="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" >
            
< sectionGroup  name ="scripting"  type ="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" >
                
< section  name ="scriptResourceHandler"  type ="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  requirePermission ="false"  allowDefinition ="MachineToApplication"   />
                
< sectionGroup  name ="webServices"  type ="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" >
                    
< section  name ="jsonSerialization"  type ="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  requirePermission ="false"  allowDefinition ="Everywhere"   />
                    
< section  name ="profileService"  type ="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  requirePermission ="false"  allowDefinition ="MachineToApplication"   />
                    
< section  name ="authenticationService"  type ="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  requirePermission ="false"  allowDefinition ="MachineToApplication"   />
                    
< section  name ="roleService"  type ="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  requirePermission ="false"  allowDefinition ="MachineToApplication"   />
                
</ sectionGroup >   
            
</ sectionGroup >     
        
</ sectionGroup >     
    
</ configSections >      
    
    
< appSettings >           
    
</ appSettings >       
        
    
< connectionStrings >        
    
</ connectionStrings >   
    
    
< customConfig >     
        
< allowlevels >  
            
< arg  key ="Corporate"  value ="true"   />              
        
</ allowlevels >
    
</ customConfig >
    
    
< spring >
        
< context >
            
< resource  uri ="~/App_Data/Config/Spring/objects_all.config"   />
            
< resource  uri ="~/App_Data/Config/Spring/spring.config"   />
            
< resource  uri ="~/App_Data/Config/Spring/pages.config"   />     
        
</ context >
    
</ spring >
    
    
< common >
        
< logging >
            
< factoryAdapter  type ="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net" >
                
< arg  key ="configType"  value ="FILE-WATCH"   />
                
< arg  key ="configFile"  value ="~/App_Data/Config/log4net.xml"   />
            
</ factoryAdapter >
        
</ logging >
    
</ common >
    
    
< system .web >
        
        
< pages >
            
< controls >
                
< add  tagPrefix ="spring"  namespace ="Spring.Web.UI.Controls"  assembly ="Spring.Web"   />
                
< add  tagPrefix ="asp"  namespace ="System.Web.UI"  assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
                
< add  tagPrefix ="asp"  namespace ="System.Web.UI.WebControls"  assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   /></ controls >
        
</ pages >
        
<!--  
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会 
            影响性能,因此只在开发过程中将此值  
            设置为 true。
        
-->
        
< compilation  debug ="true" >
            
< assemblies >
                
< add  assembly ="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   />
                
< add  assembly ="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   />
                
< add  assembly ="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   />
                
< add  assembly ="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"   />
                
< add  assembly ="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"   />
                
< add  assembly ="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"   />
                
< add  assembly ="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   />
                
< add  assembly ="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"   />
                
< add  assembly ="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   />
                
< add  assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
                
< add  assembly ="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   />
                
< add  assembly ="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"   /></ assemblies >
        
</ compilation >
        
<!--
            通过 <authentication> 节可以配置 ASP.NET 使用的 
            安全身份验证模式,
            以标识传入的用户。 
        
-->
        
< authentication  mode ="Windows"   />
        
<!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        
-->
        
<!--  错误页面跳转 
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
-->
        
<!--  Spring Web  -->
        
<!--  Spring的页面处理器和模块(对于使用页面注入非常重要)  -->
        
< httpHandlers >
            
< add  verb ="*"  path ="*.aspx"  type ="Spring.Web.Support.PageHandlerFactory, Spring.Web"   />
            
< remove  verb ="*"  path ="*.asmx"   />
            
< add  verb ="*"  path ="*.asmx"  validate ="false"  type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
            
< add  verb ="*"  path ="*_AppService.axd"  validate ="false"  type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
            
< add  verb ="GET,HEAD"  path ="ScriptResource.axd"  validate ="false"  type ="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
        
</ httpHandlers >
        
< httpModules >
            
< add  name ="Spring"  type ="Spring.Context.Support.WebSupportModule, Spring.Web"   />
            
< add  name ="ScriptModule"  type ="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
        
</ httpModules >
    
</ system.web >
    
    
< system .webServer >
        
< directoryBrowse  enabled ="true"   />
            
< validation  validateIntegratedModeConfiguration ="false"   />
        
< modules >
            
< remove  name ="ScriptModule"   />
            
< add  name ="ScriptModule"  preCondition ="managedHandler"  type ="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
        
</ modules >
        
< handlers >
            
< remove  name ="WebServiceHandlerFactory-Integrated"   />
            
< remove  name ="ScriptHandlerFactory"   />
            
< remove  name ="ScriptHandlerFactoryAppServices"   />
            
< remove  name ="ScriptResource"   />
            
< add  name ="ScriptHandlerFactory"  verb ="*"  path ="*.asmx"  preCondition ="integratedMode"  type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
            
< add  name ="ScriptHandlerFactoryAppServices"  verb ="*"  path ="*_AppService.axd"  preCondition ="integratedMode"  type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
            
< add  name ="ScriptResource"  verb ="GET,HEAD"  path ="ScriptResource.axd"  preCondition ="integratedMode"  type ="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   />
        
</ handlers >
        
< defaultDocument >
            
< files >
                
< clear  />
                
< add  value ="default.aspx"   />
                
< add  value ="index.aspx"   />
                
< add  value ="Default.htm"   />
                
< add  value ="Default.asp"   />
                
< add  value ="index.htm"   />
                
< add  value ="index.html"   />
            
</ files >
        
</ defaultDocument >
    
</ system.webServer >
    
    
< system .codedom >
            
< compilers >
                
< compiler  language ="c#;cs;csharp"  extension =".cs"  type ="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  warningLevel ="4" >
                    
< providerOption  name ="CompilerVersion"  value ="v3.5"   />
                    
< providerOption  name ="WarnAsError"  value ="false"   />
                
</ compiler >
                
< compiler  language ="vb;vbs;visualbasic;vbscript"  extension =".vb"  type ="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  warningLevel ="4" >
                    
< providerOption  name ="CompilerVersion"  value ="v3.5"   />
                    
< providerOption  name ="OptionInfer"  value ="true"   />
                    
< providerOption  name ="WarnAsError"  value ="false"   />
                
</ compiler >
            
</ compilers >
    
</ system.codedom >
    
    
< runtime >
        
< assemblyBinding  xmlns ="urn:schemas-microsoft-com:asm.v1" >
            
< dependentAssembly >
                
< assemblyIdentity  name ="System.Web.Extensions"  publicKeyToken ="31bf3856ad364e35"   />
                
< bindingRedirect  oldVersion ="1.0.0.0-1.1.0.0"  newVersion ="3.5.0.0"   />
            
</ dependentAssembly >
            
< dependentAssembly >
                
< assemblyIdentity  name ="System.Web.Extensions.Design"  publicKeyToken ="31bf3856ad364e35"   />
                
< bindingRedirect  oldVersion ="1.0.0.0-1.1.0.0"  newVersion ="3.5.0.0"   />
            
</ dependentAssembly >
        
</ assemblyBinding >
    
</ runtime >
    
</ configuration >

 

 

之前直接在回送事件中,想获取 postback后控件的值,发现获取不到新值。查了下相关文档,spring.net 支持双向绑定,比原生的asp.net强大得多,感觉很爽。而且可以与updatepanel一起使用。

it's Great !! 

 

 

你可能感兴趣的:(update)