解决视图状态/命名空间冲突

发布于 2024-08-09 03:47:53 字数 1446 浏览 9 评论 0原文

我有一个列表框控件。当用户单击它时,它会告诉自定义控件使用某个 ID 来使用。

自定义控件每次(动态)绘制相同的内容,只是根据此 ID 加载不同的内容(它从数据库加载到像控件一样的动态表单中)。

好的,现在我遇到了视图状态溢出的问题。当您单击列表框加载 ID #1 时,一切看起来都不错。然后,单击 ID #2,在自定义控件内创建的所有文本框控件都具有与 ID #1 中放入的相同内容。因此,当列表框索引更改时,我需要清除视图状态,但我无法让它工作。

所有控件也是在 Page_Load 中创建的。

我在 Page_Load 尝试了 ViewState.Clear() 但没有做任何事情。

我从 INamingInterface 派生了自定义控件,但我猜 ID 仍然与视图状态匹配。

我尝试将自定义控件 ID 更改为唯一的内容(例如“CONROL_”+id.ToString()),我也尝试对包含自定义控件的面板执行相同的操作。

我似乎无法摆脱这种视图状态!

编辑 问题的代码

public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
    if (ddl.SelectedValue == "1")
    {
        Create("ID #1");
    }
    else if (ddl.SelectedValue == "2")
    {
        Create("ID #2");
    }
 }
void Create(string text)
{
    TextBox t = new TextBox();
    t.Text = text;
    pnl.Controls.Add(t);
}
}

好的,这里是演示标记

<div>
    <asp:Panel ID="pnl" runat="server">
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="True">
        <asp:ListItem Text="id 1" Value="1">
        </asp:ListItem>
        <asp:ListItem Text="id 2" Value="2"></asp:ListItem>
        </asp:DropDownList>
    </asp:Panel>
</div>

:如果您运行此代码,您会注意到,如果您更改文本框中的内容,然后更改下拉列表,那么您之前输入的内容将保留在那里,而不是它被覆盖..

我的基本目标是得到它,以便当您更改为 ID #2 时,无论如何都会将“ID #2”放入文本框中(最好不完全禁用视图状态)

I have a listbox control. When the user clicks it, it tells a custom control to use a certain ID to use.

The custom control draws the same thing everytime(dynamically), just loads different content depending on this ID(it's loaded from a database into a dynamic form like control).

Ok, Now I'm having trouble with viewstate spillage. When you click the listbox to load say ID #1, it'll all look good. Then, you click on ID #2 and all the textbox controls created inside the custom control has the same thing that was put in ID #1. So when the listbox index changes I need to clear the view state, but I can't get this to work.

All of the controls are created at Page_Load also.

I tried ViewState.Clear() at Page_Load but that didn't do anything.

I had the custom control derive from INamingInterface, but I guess the IDs still match for viewstate.

I've tried changing the custom controls ID to something unique(like "CONROL_"+id.ToString()) I've also tried doing the same thing with the panel containing the custom control.

I can not seem to get rid of this view state!

EDIT
Ok here is code that demonstrates the problem

public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
    if (ddl.SelectedValue == "1")
    {
        Create("ID #1");
    }
    else if (ddl.SelectedValue == "2")
    {
        Create("ID #2");
    }
 }
void Create(string text)
{
    TextBox t = new TextBox();
    t.Text = text;
    pnl.Controls.Add(t);
}
}

the markup:

<div>
    <asp:Panel ID="pnl" runat="server">
        <asp:DropDownList ID="ddl" runat="server" AutoPostBack="True">
        <asp:ListItem Text="id 1" Value="1">
        </asp:ListItem>
        <asp:ListItem Text="id 2" Value="2"></asp:ListItem>
        </asp:DropDownList>
    </asp:Panel>
</div>

If you run this code you'll notice that if you change what is in the textbox and then you change the dropdown list, then what you typed earlier will be kept in there instead of it being overwritten..

My basic goal with this is to get it so that when you change to ID #2, it puts "ID #2" in the textbox no matter what(preferably without disabling viewstate altogether)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-08-16 03:47:53

如果我设置文本控件的 ID,则它不会保留旧值。您是否为所有控件指定了唯一的 id?

void Create(string text)
{ 
    TextBox t = new TextBox();
    t.ID = text;
    t.Text = text;
    pnl.Controls.Add(t);
}

If I set the ID of the Text control then it doesn't retain the old value. Are you giving all controls a unique id?

void Create(string text)
{ 
    TextBox t = new TextBox();
    t.ID = text;
    t.Text = text;
    pnl.Controls.Add(t);
}
遗忘曾经 2024-08-16 03:47:53

你不能那样做。

为了使视图状态正常工作,所有控件都必须在加载之前创建并具有相同的 id。因此,您必须将控件定义存储在会话中,然后使用相同的 id 重新创建,以便 ASP.NET 从视图状态加载它们的属性。 page_load来得太晚了,在PreLoad时进行。

但更容易在设计时创建所有控件,将可见设置为 false,并更改其可见性,以便视图状态正常工作。

You can't do it that way.

For viewstate to work properly all controls must be created before it is loaded and with the same id's. So you must store the control definitions in session and recreate then with the same ids to ASP.NET load their properties from the view state. Page_load is too late, do it at PreLoad.

But it is easier to have all controls created at design time with visible set to false, and alternate their visibility so viewstate will work properly.

妖妓 2024-08-16 03:47:53

事实上这已经不再重要了。我们通过禁用动态创建的控件的视图状态来修复它。这并非在所有情况下都有效,但在我们的例子中,用户可以按下两个按钮(与动态控件有关)或一个列表框来切换表单。这两个按钮都将控件的状态保存到数据库,因此实际上不需要视图状态。(在考虑视图状态以及它如何与控件交互时,我总是感到困惑。)

所以基本建议:如果您在控制视图状态时遇到困难,确保您确实需要它。

Actually this is no longer relevant. We fixed it by just disabling viewstate for our dynamically created controls. This would not work in all instances, but in our case there are two buttons the user can push(that are to do with the dynamic controls) or a list box to switch forms. The two buttons both save the state of the controls to database, so viewstate is not actually needed.(I always get confused when thinking about viewstate and how it interacts with controls.. )

So basic advice: If your having trouble controlling the viewstate, be sure you actually need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文