- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近读了this article on smart use of ViewState并且我对 ViewState 中没有不必要的静态数据特别感兴趣。但是,我也很好奇我是否可以为父子下拉列表做同样的事情,比如经典的 Country/CountrySubDivision 示例。
所以我有这个标记:
<asp:DropDownList runat="server" ID="ddlCountry" DataTextField="Name" DataValueField="ID" EnableViewState="false" />
<asp:DropDownList runat="server" ID="ddlCountrySubdivision" DataTextField="Name" DataValueField="ID" EnableViewState="false" />
<asp:Button runat="server" ID="btnTest" />
还有这个代码隐藏:
public class Country
{
public string Name { get; set;}
public int Id { get; set; }
}
public class CountrySubdivision
{
public string Name { get; set; }
public int Id { get; set; }
public int CountryId { get; set; }
}
protected override void OnInit(EventArgs e)
{
var l = new List<Country>();
l.Add(new Country { Name = "A", Id = 1 });
l.Add(new Country { Name = "B", Id = 2 });
l.Add(new Country { Name = "C", Id = 3 });
ddlCountry.DataSource = l;
ddlCountry.DataBind();
var l2 = new List<CountrySubdivision>();
l2.Add(new CountrySubdivision { Name = "A1", Id = 1, CountryId = 1 });
l2.Add(new CountrySubdivision { Name = "A2", Id = 2, CountryId = 1 });
l2.Add(new CountrySubdivision { Name = "B1", Id = 4, CountryId = 2 });
l2.Add(new CountrySubdivision { Name = "B2", Id = 5, CountryId = 2 });
l2.Add(new CountrySubdivision { Name = "C1", Id = 7, CountryId = 3 });
l2.Add(new CountrySubdivision { Name = "C2", Id = 8, CountryId = 3 });
// this does not work: always comes out 1 regardless of what's actually selected
var selectedCountryId = string.IsNullOrEmpty(ddlCountry.SelectedValue) ? 1 : Int32.Parse(ddlCountry.SelectedValue);
// this does work:
var selectedCountryIdFromFormValues = Request.Form["ddlCountry"];
ddlCountrySubdivision.DataSource = l2.Where(x => x.CountryId == selectedCountryId).ToList();
ddlCountrySubdivision.DataBind();
base.OnInit(e);
}
所以我注意到的第一件事是,即使 EnableViewstate
为 false
,我的国家控件的值也会在请求中保持不变,无需额外的努力。甜的。这是很多序列化的东西,我不需要在提交表单时通过网络发送。
然后我用一对父子下拉菜单来到上面的示例,我看到 ddlCountry.SelectedValue
是默认值,而 Request.Form["ddlCountry"]
反射(reflect)了控件的值。
有没有一种方法可以保持 EnableViewState = "false"
而无需求助于 Request.Form
来获取依赖控件的值?
最佳答案
Then I came to the example above with a pair of parent-child drop downs, and I see that ddlCountry.SelectedValue is defaulting whereas Request.Form["ddlCountry"] is reflecting the control's value.
您看到此行为的原因是在页面生命周期的那个时刻,尚未加载 Viewstate。加载 Viewstate 时,它取自 Request.Form 对象值,因此您会在那里看到正确的值。
关于c# - 在 OnInit() 中获取控件值的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2320146/
从 angularjs 1.5 迁移到 1.6 的文档说“禁用(默认情况下) Controller 实例上绑定(bind)的预分配,这有助于支持原生 ES6 类。”和“依赖于存在绑定(bind)的初始
所以我一直在阅读 Angular 2,有两种方法可以做同样的事情,但我不知道它是否存在根本区别。 考虑以下问题 构造器 export class MyComponent { myAge: numb
使用 resolver 服务或在 OnInit Hook 中获取数据有什么更好的选择?例如,如果我需要从 3 个不同的来源获取数据到 1 个页面,更好的用户解析器或将代码写入 ngOnInit? 代码
应用程序一直卡在启动画面上的原因是什么?我使用 getx 状态管理。如果 authToken 不为空,它应该转到主页。但是 Controller 类中的 onInit 没有调用。 void main(
我想测试我的 customForm 组件,它使用来自另一个库的组件。首先我想测试我的组件是否初始化了嵌套库组件。让我举个例子: @Component({ selector: 'iris-fiel
无需导入 OnInit 并实现 ngOnInit(){ this.recipies=this.recipeService.getRecipies(); } 这仍然工作正常,没有任何提示,所以我为什
我有以下代码片段 import { Component, OnInit } from "@angular/core"; import { AuthenticationService } from ".
我不断遇到 OnInit 访问同一组件类中的函数的问题。 我的基本设置如下: import {Component, OnInit} from '@angular/core'; ... export c
我有以下代码片段 import { Component, OnInit } from "@angular/core"; import { AuthenticationService } from ".
在 manifest.json 中,我有以下模型定义: { "sap.ui5": { "models": { "SalesInvoices": {
我最近读了this article on smart use of ViewState并且我对 ViewState 中没有不必要的静态数据特别感兴趣。但是,我也很好奇我是否可以为父子下拉列表做同样的事
如何在我的 View 初始化时只运行一次 orderBy 过滤器?我不希望我的列表在运行时重新排序。 最佳答案 改为在 Controller 中使用 orderBy 作为过滤器: app.contr
这个问题在这里已经有了答案: Angular 2 Component Constructor Vs OnInit [duplicate] (3 个答案) 关闭 6 年前。 我只是想弄清楚 Angul
我正在尝试实现 angular 2 onInit 函数,但我在控制台中收到以下错误: Error: ReferenceError: OnInit is not defined(...) 谁能指出我做错
这个问题在这里已经有了答案: Difference between Constructor and ngOnInit (27 个答案) 关闭 6 年前。 如果我希望函数 x 在每次加载组件时都发生,
所以我注意到很多关于这个的话题,但都比较老,而且没有官方的解决方案。也许现在有解决这个问题的方法。但基本上如果你有一个带有用户名输入字段的表单,当页面第一次加载时,如果浏览器自动填充该值(因为你在浏览
在处理 Page_Init 事件或覆盖 Page 的 OnInit 方法之间,使用哪个更好?谢谢。 最佳答案 大约一年前我有这个问题,我决定覆盖而不是 On_X 事件。这是我阅读的文章,介绍了优缺点:
例如,我有两个输入属性: @Input() field1; @Input() field2; 每次初始化 field1 时,我都希望调用 function1();每次我初始化 field2 时,我都希
我的应用中有这个组件: 这是 Controller /组件定义: .component('actionsBar', { controller: 'actionsBarController',
这个问题已经有答案了: How do I invoke onInit() every time I move to a view? [duplicate] (1 个回答) 已关闭 5 年前。 我有两个
我是一名优秀的程序员,十分优秀!