OnInit和PageLoad这两个方法,我该在什么时候,分别用什么方法??

在Init发生的时候,没有任何动态装载控件,仅仅装入了设计器上“写死”的控件。并且没有任何状态ViewState值,没有任何客户端post值。Init仅仅是说明那些写死的控件已经装载。例如页面上设计有DataGrid,在Init的时候DataGrid控件已经存在但是没有任何Item也没有任何正确的PageSize等属性值。例如页面上设计有TextBox,在Init的时候虽然已经存在Init,但是没有那些基于动态ViewState的属性,更没有客户端最新的Text值。

在Load发生的时候,对于那些动态状态的控件,上述一切已经齐备。对于动态装载的控件,如果是写在CreateChildControls方法中的,也已经齐备。对于那些喜欢写在load中动态装载的控件,则只有状态会立即填入,而客户端post值必须等Load结束之后才能填入。

很难说为什么有这样的设计。我估计本来的设计是应该在CreateChildControls方法中创建子控件,在Load中是在所有控件初始化完成准备触发业务逻辑事件之前给客户程序一个通知。而很多人把动态控件滚利偏要写在Load中,最后只好将错就错,考虑将Load中动态装载的控件在Load结束之后再额外处理一次。

而很多人把动态控件滚利偏要写在Load中     -->     而很多人把动态控件创建偏要写在Load中

由界面触发业务逻辑的部分,要在控件的事件中完成,例如在xxxx_Changed、xxxx_click或者xxxx_Selected事件中完成,而不要在Load中完成。asp.net在Load事件通知已经装载完成之后,实际还要进行一个增量的动态控件的最后装载(post值)的动作(但是对于一般人的程序往往在Load之前已经完成了90%的装入动作了),之后又用LoadComplete来再次通知装入完成,与java、delphi中那些类似的号称干净、高级的框架相比,明显有为了市场因素而故意放慢速度多做一些兼容性处理的意思。

 

添加代码以动态创建 文本框 控件。该控件被创建的每次运行页时。若要执行此操作最好是 WebForm1 类提供在 OnInit 函数中。
override protected void OnInit(EventArgs e)
{
    // Create dynamic controls here.
    // Use "using System.Web.UI.WebControls;"
    TextBox1 = new TextBox();
    TextBox1.ID = "TextBox1";
    TextBox1.Style["Position"] = "Absolute";
    TextBox1.Style["Top"] = "25px";
    TextBox1.Style["Left"] = "100px";
    Form1.Controls.Add(TextBox1);

    TextBox2 = new TextBox();
    TextBox2.ID = "TextBox2";
    TextBox2.Style["Position"] = "Absolute";
    TextBox2.Style["Top"] = "60px";
    TextBox2.Style["Left"] = "100px";
    Form1.Controls.Add(TextBox2);

    this.TextBox1.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
    this.TextBox2.TextChanged += new System.EventHandler(this.TextBox_TextChanged);

    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
}

 

  1. 请注意此函数的这两个动态创建 TextBox 控件处理 TextChanged 事件。默认状态下,AutoPostBackfalse文本框 控件。因此,更改文本在控件中的不会导致回发到服务器。但是,当 提交 按钮单击以将窗体发送到服务器、 触发的 TextChanged 事件,为 文本框 控件和该函数调用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以在shared组件中使用translate翻译文本,例如:{{'Acknowledged'|translate}},只需要在模块中导入TranslateModule即可。 首先,安装ngx-translate库: ``` npm install @ngx-translate/core --save ``` 然后在app.module.ts文件中导入TranslateModule和TranslateLoader: ``` import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientModule, HttpClient } from '@angular/common/http'; import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; @NgModule({ imports: [ BrowserModule, HttpClientModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: (http: HttpClient) => { return new TranslateHttpLoader(http); }, deps: [HttpClient] } }) ], bootstrap: [AppComponent] }) export class AppModule { } ``` 接着,在shared组件中使用translate服务来翻译文本。在构造函数中注入TranslateService,并在HTML模板中使用{{'Acknowledged'|translate}}这样的语法来翻译文本: ``` import { Component, OnInit } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; @Component({ selector: 'app-shared-component', templateUrl: './shared-component.component.html', styleUrls: ['./shared-component.component.css'] }) export class SharedComponentComponent implements OnInit { constructor(private translate: TranslateService) { } ngOnInit() { } } ``` 最后,在app.component.html文件中添加语言切换按钮,用于切换当前的语言: ``` <button (click)="translate.use('en')">English</button> <button (click)="translate.use('fr')">French</button> ``` 当用户点击按钮时,应用程序会自动加载相应的语言文件,并将当前语言设置为所选语言。这样,在shared组件中使用{{'Acknowledged'|translate}}这样的语法获取翻译文本时,会自动根据当前语言返回相应的翻译结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值