C# 在下拉列表中获取所选值的ID
我试图在数据库中保存数据,作为其中的一部分,我试图在下拉列表中获取所选项目的ID(主键) 它一直保存相同的ID。它只保存任何选定项目的第一个ID。如何调整代码,使其根据我在下拉列表中选择的内容保存不同的IDC# 在下拉列表中获取所选值的ID,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,我试图在数据库中保存数据,作为其中的一部分,我试图在下拉列表中获取所选项目的ID(主键) 它一直保存相同的ID。它只保存任何选定项目的第一个ID。如何调整代码,使其根据我在下拉列表中选择的内容保存不同的ID protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection( WebConfigurationManager.ConnectionStrings["M
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con);
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "RoleType";
DropDownList1.DataValueField = "UserRoleID";
DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("sp_UserInsertUpdate", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
String ID = DropDownList1.SelectedValue;
cmd.Parameters.AddWithValue("UserID", (hfUserID.Value == "" ? 0 : Convert.ToInt32(hfUserID.Value)));
cmd.Parameters.AddWithValue("UserRoleID", ID);
cmd.Parameters.AddWithValue("Username", TextBox1.Text);
cmd.Parameters.AddWithValue("Password", TextBox2.Text);
cmd.Parameters.AddWithValue("Email", TextBox3.Text);
cmd.Parameters.AddWithValue("DateCreate", TextBox4.Text);
cmd.ExecuteNonQuery();
con.Close();
}
这是因为每次你点击按钮。click事件发生,但在此之前它执行页面加载事件,在页面加载事件中,您添加了绑定逻辑下拉列表。这意味着无论何时单击按钮,每次它首次加载下拉列表时,都会出现单击事件。为此,您只需使用
IsPostBack
添加一个if条件
进行如下更改
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) //Added
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con);
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "RoleType";
DropDownList1.DataValueField = "UserRoleID";
DropDownList1.DataBind();
}
}
你应该检查
!iPostBack
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) // Add this If condition
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Select UserRoleID, RoleType from tb_UserRoles", con);
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "RoleType";
DropDownList1.DataValueField = "UserRoleID";
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("sp_UserInsertUpdate", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
String ID = DropDownList1.SelectedValue;
cmd.Parameters.AddWithValue("UserID", (hfUserID.Value == "" ? 0 : Convert.ToInt32(hfUserID.Value)));
cmd.Parameters.AddWithValue("UserRoleID", ID);
cmd.Parameters.AddWithValue("Username", TextBox1.Text);
cmd.Parameters.AddWithValue("Password", TextBox2.Text);
cmd.Parameters.AddWithValue("Email", TextBox3.Text);
cmd.Parameters.AddWithValue("DateCreate", TextBox4.Text);
cmd.ExecuteNonQuery();
con.Close();
}
请不要剽窃内容。