简体   繁体   English

页面回发或刷新后在ASP.NET中刷新通用列表

[英]Refresh Generic List in ASP.NET after page is postback or refreshed

I have other trouble and I cannot solve it. 我还有其他麻烦,无法解决。

In ASP.NET I have DropDownList object which takes data from a Generic List. 在ASP.NET中,我有DropDownList对象,该对象从通用列表中获取数据。 In ASP.NET page 在ASP.NET页面

<tr><td>Choose category</td>
<td><asp:DropDownList id="list_categories" runat="server"></asp:DropDownList></td>
<td>
<asp:RequiredFieldValidator ControlToValidate="list_categories" runat="server" Display="Static" ErrorMessage="Select category" ID="verify_category" ValidationGroup="add_link_group">
</asp:RequiredFieldValidator></td></tr>

and the behind code : 和后面的代码:

private void generate_select( DropDownList select ) {

         BusinessLayerArcht layer = LoadDataFromBL();

         foreach ( CategoriesCtrlDto cto in layer.Categories ) {          
            select.Items.Add( cto.Name );
         }
      }

and the Page_Load 和Page_Load

generate_select( list_categories );

I tried with Clear() but when I added a new link to a selected category , it will be inserted at first category If I don't call .Clear() function then the inseration it's ok but after page refreshing, the list contains duplicate categories 我尝试过Clear(),但是当我向选定的类别添加新链接时,它将插入第一个类别。如果我不调用.Clear()函数,则可以执行插入操作,但刷新页面后,该列表将包含重复项分类

Initial I had GMail YMail AOL 最初我有GMail YMail AOL

after inserting a new link or other event which refresh page, the list will shows as : GMail YMAil AOL GMail YMAil AOL 插入新链接或其他刷新页面的事件后,列表将显示为:GMail YMAil AOL GMail YMAil AOL

If I refresh four times the page then the list has GMail YMAil AOL GMail YMAil AOL GMail YMAil AOL GMail YMAil AOL 如果我刷新页面四次​​,则列表中包含GMail YMAil AOL GMail YMAil AOL GMail YMAil AOL GMail YMAil AOL

Repeats four times, so it duplciates at each page postback or refresh. 重复四次,因此每次回发或刷新时都会重复。 I tried also to clear cache if (Page.isPostBack) 我也尝试清除缓存是否(Page.isPostBack)

You have to do 你必须做

if(!Page.IsPostBack)
generate_select(list_categories);

!Page.IsPostBack ==> if is NOT postback. !Page.IsPostBack ==>如果不是回发。 If you execute the code every postback, it will insert duplicate records 如果在每次回发时执行代码,它将插入重复的记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM