Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 为什么回邮时我的DropDownList是空的?_Asp.net_Drop Down Menu - Fatal编程技术网

Asp.net 为什么回邮时我的DropDownList是空的?

Asp.net 为什么回邮时我的DropDownList是空的?,asp.net,drop-down-menu,Asp.net,Drop Down Menu,我已经看过了一些类似问题的答案,但我似乎仍然不明白这一点。我想我误解了ASP.NET的工作原理 在标准ASP.NET4.0“创建新帐户”表单中,我添加了一个DropDownList,其中包含要为新帐户选择的角色。在aspx页面中,控件如下所示: <asp:DropDownList ID="RoleList" Width="100px" runat="server"></asp:DropDownList> 我可以从生成的html中查看/选择角色。单击用于创建用户的“提交”

我已经看过了一些类似问题的答案,但我似乎仍然不明白这一点。我想我误解了ASP.NET的工作原理

在标准ASP.NET4.0“创建新帐户”表单中,我添加了一个DropDownList,其中包含要为新帐户选择的角色。在aspx页面中,控件如下所示:

<asp:DropDownList ID="RoleList" Width="100px" runat="server"></asp:DropDownList>
我可以从生成的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对象包含零项,并且没有选定的值。不知何故,我在选择项目和提交之间丢失了填充项目。知道我做错了什么吗?

下面的代码绕过了页面加载上的正确数据绑定

if (Page.IsPostBack) 
{ 
    return; 
}

每次都需要绑定此控件,以便在调用click事件时显示这些值。对不再存在的选定项目进行操作时,您可能还会收到事件错误。

您是否尝试在以下条件下使用绑定

if (!Page.IsPostBack)  
{  
    //Binding goes here
}

将dropdownlist加载放在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));
    }
}

我有一个类似的问题,更改单选按钮选择将自动回发页面,下拉列表项将在页面返回后消失

解决方案

检查IIS->您的网站->页面和控件->启用视图状态和启用会话状态是否应设置为true


希望这有帮助。

但如果我这样做,所选索引将始终为0,因为在单击“提交”之后,但在调用RegisterUser_CreatedUser之前,它将重新填充。@SP-没错,但ncakmak的格式是执行此类操作的更标准的方法,因此,您可能希望以这种格式执行操作,以便其他人更容易理解您的代码。就是这样!谢谢,现在我只需要理解为什么在Asp.Net将值从窗体加载到其对象之前会发生@SP-OnInit。页面加载发生在加载事件期间,该事件发生在查询表单之后。查看此页面以了解Asp.Net中的页面事件:我阅读了该页面,但仍然不理解为什么我的原始代码不能正常工作。为什么下拉列表中的项目会消失?@SP-它们会消失,因为它们没有加载。您的代码告诉Page_Load返回,这意味着它在有机会运行该代码之前离开函数。你需要记住,网络基本上是无状态的,所以如果你想在页面上显示一些东西,你需要在每次请求页面时都这样做。这对我来说很有吸引力。谢谢@Charles Boyung。你为我节省了很多时间!
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));
    }
}