编辑ASP.NET控件的标记,然后保存在PostBack上

[英]Editing markup of an ASP.NET control then saving on PostBack


I am using jquery to append or remove a <ListItem> (rendered as <option>) from the <ListBox> (rendered as <select multiple="multiple">).

我正在使用jquery从 追加或删除 (呈现为

The problem arises when I try to save on PostBack. The correct information is saved to the database. However, when the page is loaded on PostBack instead of the new data from the database being loaded, the old data from the initial <ListBox> is still there.

当我尝试保存在PostBack上时出现问题。正确的信息将保存到数据库中。但是,当在PostBack上加载页面而不是正在加载的数据库中的新数据时,来自初始 的旧数据仍然存在。

When I set Response.Expires = 0 and refresh the page that is loaded after PostBack everything is where it needs to be.

当我设置Response.Expires = 0并刷新PostBack之后加载的页面时,一切都在它需要的位置。

I have tried: Response.Cache.SetCacheability(HttpCacheability.NoCache);
Cache.Remove("Add_Product.aspx");

我试过了:Response.Cache.SetCacheability(HttpCacheability.NoCache); Cache.Remove( “Add_Product.aspx”);

Also to bypass the problems with EventValidation I am setting EnableEventValidation="false".

另外为了绕过EventValidation的问题,我设置了EnableEventValidation =“false”。

So my question is how should I approach this problem? Is there an quick and easy way to force PostBack to load the fresh page?

所以我的问题是我应该如何处理这个问题?是否有一种快速简便的方法来强制PostBack加载新页面?

I would rather not have to redirect to the same page after the PostBack.

我不想在PostBack之后重定向到同一页面。

1 个解决方案

#1


If you don't want to redirect to the same page after the postback (following the Post-Redirect-Get pattern, which can actually clean ASP.NET pages up tremendously!), your best options are to:

如果您不希望在回发后重定向到同一页面(在Post-Redirect-Get模式之后,实际上可以非常清理ASP.NET页面!),您最好的选择是:

1) After you save the item to the database, retrieve your ListBox's DataSource and call ListBox.DataBind() again on the postback. (As a sidenote, the necessity of adding all this extra data retrieval and binding code is one of several reasons people move to the PRG pattern when working with ASP.NET.)

1)将项目保存到数据库后,检索ListBox的DataSource并在回发上再次调用ListBox.DataBind()。 (作为旁注,添加所有这些额外数据检索和绑定代码的必要性是人们在使用ASP.NET时转移到PRG模式的几个原因之一。)

2) Modify your codebehind class so that it adds the list item to the relevant WebControl when you post back to the page.

2)修改您的代码隐藏类,以便在您回发到页面时将列表项添加到相关的WebControl。

That is, when you save the new value to the database, add the new ListItem to your ListBox control too:

也就是说,当您将新值保存到数据库时,也将新的ListItem添加到ListBox控件:

protected void Save_Click(object sender, EventArgs e) {

  // ... Code to save the new value to your database ...
  ListItem newItem = new ListItem(text, value);
  ListBox.Items.Add(listItem);
}

Now the value will be there when ASP.NET renders your HTML.

现在,当ASP.NET呈现您的HTML时,该值将存在。


Edit: added some more detail, to try to explain why there isn't a better solution.

编辑:添加更多细节,试图解释为什么没有更好的解决方案。

When you add <option> elements to a <select> control on the client, those new option elements are not communicated to the server - there just isn't any mechanism for it.

当您将

That can be confusing because, as you've seen, you can still post the dynamically added option values to the server (though only if you turn off EnableEventValidation, which is risky as womp pointed out in comments).

这可能会令人困惑,因为正如您所见,您仍然可以将动态添加的选项值发布到服务器(尽管只有在您关闭EnableEventValidation时才会发出这种情况,这在评论中指出了womp指出的风险)。

However, unselected option elements are not communicated to web servers during post requests.

但是,在发布请求期间,未选择的选项元素不会传递给Web服务器。

The question then is, how can ASP.NET rebuild the correct ListBox contents after a post request? First, it does whatever the code-behind tells it (ListBox.DataBind, ListBox.Items.Add, etc.). Second, if viewstate is enabled, it adds any option values that were saved to viewstate.

那么问题是,ASP.NET如何在发布请求后重建正确的ListBox内容?首先,它执行代码隐藏的任何操作(ListBox.DataBind,ListBox.Items.Add等)。其次,如果启用了viewstate,则会添加保存到viewstate的所有选项值。

ASP.NET could conceivably have added a third rule, "if the posted value for the select list is not already in the ListBox, add that value to the ListBox." But since it is a security risk to allow arbitrary values to be posted to a page (at least when you have established a set of expected values, as with a ListBox), Microsoft decided against it. (Additionally, consider what you would do about dynamically added ListBox values that failed validation - should those be added when the ListBox is rebuilt or not?)

可以想象,ASP.NET可以添加第三条规则,“如果选择列表的已发布值不在ListBox中,则将该值添加到ListBox中。”但是,由于允许将任意值发布到页面是一种安全风险(至少当您已经建立了一组预期值时,就像使用ListBox一样),Microsoft决定反对它。 (另外,考虑一下动态添加的ListBox值会对验证失败的方法做什么 - 是否应该在重建ListBox时添加?)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/10/5e52c0dc4b3467f612e3dfa0bce5c8dc.html



 
  © 2014-2022 ITdaan.com 联系我们: