Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my DropDownList empty on Postback?
    primarykey
    data
    text
    <p>I've looked at answers to some similar question but I still can't seem to figure this out. I think I'm misunderstanding something about how ASP.NET works.</p> <p>In the standard ASP.Net 4.0 'Create a new Account' form, I've added a DropDownList containing Roles to be selected for the new account. in the aspx page, the control looks like this:</p> <pre><code>&lt;asp:DropDownList ID="RoleList" Width="100px" runat="server"&gt;&lt;/asp:DropDownList&gt; </code></pre> <p>I am then populating the List in the Page_Load Event:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"]; if (Page.IsPostBack) { return; } //Set the Role List Selections DropDownList roleList = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleList"); //set the role list String[] roles = Roles.GetAllRoles(); foreach (String role in roles) { roleList.Items.Add(new ListItem(role, role)); } } </code></pre> <p>I can see/select a role from the generated html. The problem arises when the 'Submit' button for creating a User is clicked:</p> <pre><code>protected void RegisterUser_CreatedUser(object sender, EventArgs e) { FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */); string continueUrl = RegisterUser.ContinueDestinationPageUrl; if (String.IsNullOrEmpty(continueUrl)) { continueUrl = "~/"; } //set user role DropDownList roleList = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleList"); Roles.AddUserToRole(RegisterUser.UserName, roleList.SelectedValue); Response.Redirect(continueUrl); } </code></pre> <p>Here, The roleList object contains zero items, and has no selected values. Somehow, I am losing the populating items between selection of the item, and the submit. Any idea what I'm doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload