显示消息并对所选索引更改执行操作
本文关键字:执行 操作 索引 消息 显示 | 更新日期: 2023-09-27 18:19:19
我有一个下拉列表,允许管理员分配一个用户的角色,它应该自动这样做,当索引被改变,但不幸的是它不做任何事情,直到我点击一个复选框是完全不相关的。下面是代码
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="DropDownList1_SelectedIndexChanged"