0

I have this dropdownlist populated and everything. The only problem is that whenever I add a new item in the database through my website, the dropdownlist doesn't update for some reason.

private CurrentUser _cu = new CurrentUser();//just to check if use is an admin or not.

    protected void Page_Load(object sender, EventArgs e)
    {           
        _cu = (CurrentUser)Session[Common.SessVariables.CurUser];
        if (!_cu.CanReport) { Response.Redirect("~/default.aspx"); }

        CurrentUser cu = (CurrentUser)Session[Common.SessVariables.CurUser];
        if (!IsPostBack)
        {
            foreach (PrefixAdd loc in cu.Prefix)//Prefix is a Property
            {
                ListItem x = new ListItem(loc.Prefix);
                PrefixID.Items.Add(x);
            }
        }

    }

@Wayne I'm using a store procedure to just insert a Prefix like Pre,yes,sey, etc. Then the list is populated with prefixes.

StringBuilder sbSQL = new StringBuilder(255);
sbSQL.Append(string.Format("exec insPrefix @Prefix=N'{0}';", PrefixBox.Text.Trim()));
string msg = string.Empty;
msg = (_oDAW.ExecuteNonQuery(sbSQL.ToString())) ? string.Format(Common.GetAppSetting(Common.ConfigKeys.User_Submit_Success), 
PrefixBox.Text.Trim()) : Common.GetAppSetting(Common.ConfigKeys.SubmitFail); //this is a somewhat custom method for CS and databinding.

@ Yuriy Rozhovetskiy Yea I add new items to this page with the dropdownlist.

2
  • You should also tell us how you are updating the database. is it providing a postback event? if so, since you are not processing those events your dropdownlist won't update. Oct 27, 2011 at 19:29
  • Do you add new item on this page or not? If not, do you update user in session after you have add new item? Oct 27, 2011 at 19:30

2 Answers 2

3

Whenever you add an item to your database, you have to rebind your drop down list.

yourDropDown.DataSource = //...
yourDropDown.DataBind();

That is, DropDownLists (and other controls) have no way of knowing that their data has changed behind the scenes, they can't automatically detect it. You have to tell the controls to rebind their data manually.

Good job on the Page_Load(...){ if !(IsPostback) part.

1

Since you add new prefix on this page with some postback item you need to add this new item to PrefixID dropdown's Items collection and update the CurrentUser instance in Session right after you have add new prefix to database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.