简体   繁体   English

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

[英]Display a message and perform actions on selected index changed asp.net c#

I have a drop down list that allows an admin to assign a user to a role and it is supposed to automatically do so when the index is changed but unfortunately its not doing anything until I click a checkbox that is totally unrelated. 我有一个下拉列表,允许管理员将用户分配给一个角色,它应该在索引发生变化时自动执行,但遗憾的是它没有做任何事情,直到我点击完全不相关的复选框。 Here is the code 这是代码

    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;
    }
 }

And here is where I make the DDL in the aspx page 这是我在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>

Is there something I'm missing because I thought that it would automatically perform the tasks on selected index changed. 有没有我缺少的东西,因为我认为它会自动执行所选索引更改的任务。 Thank you in advance 先感谢您

Put AutoPostback=true In the drop down list. Put AutoPostback=true在下拉列表中。 Without that does not fire the onselectedindexchanged="DropDownList1_SelectedIndexChanged" 没有onselectedindexchanged="DropDownList1_SelectedIndexChanged"不会触发onselectedindexchanged="DropDownList1_SelectedIndexChanged"

I hope that help. 我希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM