简体   繁体   中英

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. In ASP.NET page

<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

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

Initial I had 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

If I refresh four times the page then the list has 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)

You have to do

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

!Page.IsPostBack ==> if is NOT postback. If you execute the code every postback, it will insert duplicate records

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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