在我的ASP.NET应用程序中 - 启用了ViewState - 我可以通过简单地设置它们来重置输入。 这是一个基本的简单示例:
_TEST.aspx
<asp:ScriptManager runat="server" /> <asp:DropDownList ID="ddl" runat="server" DataSource='<%# new string[] { "A", "B", "C" } %>' /> <asp:Button ID="b" runat="server" UseSubmitBehavior="false" OnClick="b_Click" Text="Test 1" />_TEST.aspx.cs
protected void Page_Load(object sender, EventArgs e) { this.DataBind(); this.ddl.SelectedValue = "B"; } protected void b_Click(object sender, EventArgs e) { Console.WriteLine("Updated"); }在这个例子中, ddl将在Page_Load上与“A”,“B”和“C”绑定,并且它将始终选择“B”。 因此,当您更改选择并提交时,它将被重置。 想象一下,这用于重置某些特定条件的控件。
在我的情况下,我需要在动态加载的UserControl上添加一个Placeholder这种行为。 由于Placeholder不会持续保留内容,因此我需要在每次回发中重新创建控件。 ViewState似乎没有问题。 这是一个示例:
_TEST.aspx
<asp:ScriptManager runat="server" /> <asp:PlaceHolder ID="ph" runat="server" />_TEST.aspx.cs
protected void Page_Load(object sender, EventArgs e) { _TESTUC uc = (_TESTUC)this.Page.LoadControl("~/Controls/_TESTUC.ascx"); this.ph.Controls.Add(uc); }_TESTUC.ascx
<asp:DropDownList ID="ddl" runat="server" DataSource='<%# new string[] { "A", "B", "C" } %>' /> <asp:Button ID="b" runat="server" UseSubmitBehavior="false" OnClick="b_Click" Text="Test 2" />_TESTUC.ascx.cs
protected void Page_Load(object sender, EventArgs e) { this.DataBind(); this.ddl.SelectedValue = "B"; } protected void b_Click(object sender, EventArgs e) { Console.WriteLine("Updated"); }同样,在每次回发时,我想重置DropDownList的选择以进行测试,但有趣的是它记住了我的选择。
我能做些什么来解决这个问题?
In my ASP.NET application - having ViewState enabled - I can reset inputs by simply setting them again. Here is a basic simple sample that works:
_TEST.aspx
<asp:ScriptManager runat="server" /> <asp:DropDownList ID="ddl" runat="server" DataSource='<%# new string[] { "A", "B", "C" } %>' /> <asp:Button ID="b" runat="server" UseSubmitBehavior="false" OnClick="b_Click" Text="Test 1" />_TEST.aspx.cs
protected void Page_Load(object sender, EventArgs e) { this.DataBind(); this.ddl.SelectedValue = "B"; } protected void b_Click(object sender, EventArgs e) { Console.WriteLine("Updated"); }In this example ddl will be bound with "A", "B" and "C" on Page_Load and it will always select "B". So when you change the selection and submit, it will be reset. Imagine this is used to reset some controls on specific conditions.
In my case I need this kind of behavior on a dynamically loaded UserControl that is added to a Placeholder. Because a Placeholder does not persist the content I need to recreate the controls on every postback. The ViewState seems to be okay with it. Here is a sample:
_TEST.aspx
<asp:ScriptManager runat="server" /> <asp:PlaceHolder ID="ph" runat="server" />_TEST.aspx.cs
protected void Page_Load(object sender, EventArgs e) { _TESTUC uc = (_TESTUC)this.Page.LoadControl("~/Controls/_TESTUC.ascx"); this.ph.Controls.Add(uc); }_TESTUC.ascx
<asp:DropDownList ID="ddl" runat="server" DataSource='<%# new string[] { "A", "B", "C" } %>' /> <asp:Button ID="b" runat="server" UseSubmitBehavior="false" OnClick="b_Click" Text="Test 2" />_TESTUC.ascx.cs
protected void Page_Load(object sender, EventArgs e) { this.DataBind(); this.ddl.SelectedValue = "B"; } protected void b_Click(object sender, EventArgs e) { Console.WriteLine("Updated"); }Again, on every postback I want to reset the selection of the DropDownList for test purposes, but interestingly it remembers my selection.
What can I do to fix this?
最满意答案
在写这个问题时,我找到了解决方案,但决定将它发布给其他人,无论如何。
看起来ASP.NET会在Page_Init和Page_Load之间加载在ASPX中定义的ViewState之间的ViewState ,而在加载代码隐藏的控件中的Page_Load和Page_LoadComplete之间加载ViewState 。
设置Page_LoadComplete的值可以解决问题:
protected void Page_Init(object sender, EventArgs e) { this.Page.LoadComplete += new EventHandler(this.Page_LoadComplete); } protected void Page_Load(object sender, EventArgs e) { this.DataBind(); } protected void Page_LoadComplete(object sender, EventArgs e) { this.ddl.SelectedValue = "B"; } protected void b_Click(object sender, EventArgs e) { Console.WriteLine("Updated"); }While writing this question I came to the solution but decided to post it for other peoples, anyways.
It seems ASP.NET will load the ViewState between Page_Init and Page_Load in controls that are defined in ASPX, while it loads the ViewState between Page_Load and Page_LoadComplete in controls that are loaded in code-behind.
Setting the values in Page_LoadComplete solves the issue:
protected void Page_Init(object sender, EventArgs e) { this.Page.LoadComplete += new EventHandler(this.Page_LoadComplete); } protected void Page_Load(object sender, EventArgs e) { this.DataBind(); } protected void Page_LoadComplete(object sender, EventArgs e) { this.ddl.SelectedValue = "B"; } protected void b_Click(object sender, EventArgs e) { Console.WriteLine("Updated"); }更多推荐
发布评论