Eri*_*rix 0 asp.net drop-down-menu
我看过一些类似问题的答案,但我似乎仍然无法弄清楚这一点.我想我误解了ASP.NET的工作方式.
在标准ASP.Net 4.0"创建新帐户"表单中,我添加了一个DropDownList,其中包含要为新帐户选择的角色.在aspx页面中,控件如下所示:
<asp:DropDownList ID="RoleList" Width="100px" runat="server"></asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
然后我在Page_Load事件中填充List:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
我可以从生成的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);
}
Run Code Online (Sandbox Code Playgroud)
这里,roleList对象包含零项,并且没有选定的值.不知何故,我在选择项目和提交之间丢失了填充项目.知道我做错了什么吗?
Cha*_*ung 10
将您的下拉列表加载到OnInit函数中 - 然后在调用RegisterUser_CreatedUser时应该正确加载它:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8407 次 |
| 最近记录: |