Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
C# 显示消息并对所选索引执行操作更改asp.net c_C#_Asp.net_Drop Down Menu_Selectedindexchanged - Fatal编程技术网

C# 显示消息并对所选索引执行操作更改asp.net c

C# 显示消息并对所选索引执行操作更改asp.net c,c#,asp.net,drop-down-menu,selectedindexchanged,C#,Asp.net,Drop Down Menu,Selectedindexchanged,我有一个下拉列表,允许管理员将用户分配到一个角色,当索引被更改时,它应该自动这样做,但不幸的是,在我单击一个完全无关的复选框之前,它什么也不做。这是密码 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string userName = Request.QueryString["user"]; MembershipUser usr = Membership.Ge

我有一个下拉列表,允许管理员将用户分配到一个角色,当索引被更改时,它应该自动这样做,但不幸的是,在我单击一个完全无关的复选框之前,它什么也不做。这是密码

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string userName = Request.QueryString["user"];
    MembershipUser usr = Membership.GetUser(userName);

    ProfileCommon p = Profile.GetProfile(usr.UserName);

    var item = ((DropDownList)sender).SelectedItem;

    switch (item.Value)
    {
        case "1":
            if (Roles.IsUserInRole(usr.UserName, "Builder") == false)//if the user is not already in the builder role then add them
            {
                Roles.AddUserToRole(usr.UserName, "Builder");//here we add the user into the builder role
                StatusMessage2.Text = "User has been added to the builder role";//Letting the admin know that the user was added to the role

                /* Creating a connection to write to a table in the default database */
                string connection = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                SqlConnection conn = null;
                conn = new SqlConnection(connection);
                conn.Open();

                /* Here we execute the command and add a member into the table */
                using (SqlCommand cmd = new SqlCommand())
                {

                    string query = String.Format("INSERT INTO TestTable (testfirst, testlast, testaddr, testmail, testcomp) VALUES('{0}', '{1}', '{2}', '{3}','{4}')", p.fName, p.lName, p.Address, usr.Email, p.Company);
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                }
            }
            break;
    }
 }
这里是我在aspx页面中创建DDL的地方

    <asp:DropDownList ID="DropDownList1" runat="server" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">Please select from below...</asp:ListItem>
<asp:ListItem Value="1">Builder</asp:ListItem>
<asp:ListItem Value="2">Investor</asp:ListItem>
<asp:ListItem Value="3">Administrator</asp:ListItem>
</asp:DropDownList>
是否有我遗漏的东西,因为我认为它会自动执行所选索引上的任务。提前感谢您

将AutoPostback=true放入下拉列表中。否则不会触发onselectedindexchanged=DropDownList 1\u SelectedIndexChanged


我希望这会有所帮助。

复选框是否会触发回发?是的。我想这就是我错过的。正如@Freak_Droid所说,我只需要添加一个AutoPostBack=true,当表单发布时,事件就会触发。