ViewState在PostBack上覆盖动态添加的UserControl(占位符)上的更改(ViewState overwrites changes on dynamically added Use

编程入门 行业动态 更新时间:2024-10-24 10:26:34
ViewState在PostBack上覆盖动态添加的UserControl(占位符)上的更改(ViewState overwrites changes on dynamically added UserControl (in Placeholder) on PostBack)

在我的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"); }

更多推荐

本文发布于:2023-07-30 23:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   UserControl   ViewState   PostBack   added

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!