【发布时间】:2011-11-23 09:37:17
【问题描述】:
我已经查看了一些类似问题的答案,但我似乎仍然无法弄清楚这一点。我想我误解了 ASP.NET 的工作原理。
在标准的 ASP.Net 4.0“创建新帐户”表单中,我添加了一个 DropDownList,其中包含要为新帐户选择的角色。在 aspx 页面中,控件如下所示:
<asp:DropDownList ID="RoleList" Width="100px" runat="server"></asp:DropDownList>
然后我在 Page_Load 事件中填充列表:
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));
}
}
我可以从生成的 html 中查看/选择一个角色。单击创建用户的“提交”按钮时会出现问题:
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);
}
这里,roleList 对象包含零个项目,并且没有选定的值。不知何故,我在选择项目和提交之间丢失了填充项目。知道我做错了什么吗?
【问题讨论】: