下拉列表选择不正确的值
本文关键字:不正确 选择 下拉列表 | 更新日期: 2023-09-27 18:21:31
>我有一个 DropDownList,我想在其中选择一个 ID 及其名称两次,将其放入DataSet
并绑定到DropDownList
。 下面的代码是我的数据绑定。
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT [profileID], [profileName], [positionID], [positionName] from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn1);
cmd1.Parameters.AddWithValue("@memberID", memberID);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataSet ds = new DataSet();
conn1.Open();
da.Fill(ds);
conn1.Close();
chooseProfile.DataSource = ds;
chooseProfile.DataTextField = "profileName";
chooseProfile.DataValueField = "profileID";
chooseProfile.DataBind();
choosePosition.DataSource = ds;
choosePosition.DataTextField = "positionName";
choosePosition.DataValueField = "positionID";
choosePosition.DataBind();
}
在我的 ASP.NET 页面上,当用户点击提交时,它意味着抓取值并插入到表中。
protected void submitReport_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery = "SELECT * FROM vw_profile";
SqlCommand cmd = new SqlCommand(selectQuery, conn);
conn.Open();
SqlDataReader viewReader;
viewReader = cmd.ExecuteReader();
while (viewReader.Read())
{
var memberID = (string)viewReader["memberID"].ToString();
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT * from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn);
cmd1.Parameters.AddWithValue("@memberID", memberID);
conn1.Open();
int submitProfileID = Convert.ToInt32(chooseProfile.SelectedItem.Value);
int submitPositionID = Convert.ToInt32(choosePosition.SelectedItem.Value);
DateTime dateTime = DateTime.Now;
string date = dateTime.GetDateTimeFormats('d')[0];
int reportNum;
reportNum = Convert.ToInt32(viewReader["reportNumber"]);
reportNum++;
string reportTitle = "#" + reportNum + " - " + date;
SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString);
conn2.Open();
SqlCommand cmd2 = new SqlCommand("INSERT INTO sfgs_reports(profileID,memberID,positionID,reportTitle,reportContent,reportDateStamp,reportNumber) values (@profileID,@memberID,@positionID,@reportTitle,@content,@datestamp,@reportNumber)", conn2);
cmd2.Parameters.AddWithValue("@content", reportContent.Text);
cmd2.Parameters.AddWithValue("@memberID", memberID);
cmd2.Parameters.AddWithValue("@profileID", submitProfileID);
cmd2.Parameters.AddWithValue("@positionID", submitPositionID);
cmd2.Parameters.AddWithValue("@reportTitle", reportTitle);
cmd2.Parameters.AddWithValue("@datestamp", dateTime);
cmd2.Parameters.AddWithValue("@reportNumber", reportNum);
cmd2.ExecuteNonQuery();
conn2.Close();
}
}
conn.Close();
}
}
现在,当有人提交时发生的情况是,它不是从我选择的Value
中抓取,而是抓取默认值,就像默认情况下显示在DropDownList
中的值一样。 任何人都可以在此代码中看到可能导致此问题的任何错误吗?
编辑以显示更新的submitReport_Click方法:
protected void submitReport_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery = "SELECT * FROM vw_profile WHERE profileName=@currentUser";
SqlCommand cmd = new SqlCommand(selectQuery, conn);
cmd.Parameters.AddWithValue("@currentUser", User.Identity.Name);
conn.Open();
SqlDataReader viewReader;
viewReader = cmd.ExecuteReader();
while (viewReader.Read())
{
var memberID = (string)viewReader["memberID"].ToString();
if (!Page.IsPostBack)
{
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT [profileID], [profileName], [positionID], [positionName] from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn1);
cmd1.Parameters.AddWithValue("@memberID", memberID);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataSet ds = new DataSet();
conn1.Open();
da.Fill(ds);
conn1.Close();
chooseProfile.DataSource = ds;
chooseProfile.DataTextField = "profileName";
chooseProfile.DataValueField = "profileID";
chooseProfile.DataBind();
choosePosition.DataSource = ds;
choosePosition.DataTextField = "positionName";
choosePosition.DataValueField = "positionID";
choosePosition.DataBind();
}
}
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT * from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn);
cmd1.Parameters.AddWithValue("@memberID", memberID);
conn1.Open();
int submitProfileID = Convert.ToInt32(chooseProfile.SelectedItem.Value);
int submitPositionID = Convert.ToInt32(choosePosition.SelectedItem.Value);
DateTime dateTime = DateTime.Now;
string date = dateTime.GetDateTimeFormats('d')[0];
int reportNum = 1;
string reportTitle = "#" + reportNum + " - " + date;
SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString);
conn2.Open();
SqlCommand cmd2 = new SqlCommand("INSERT INTO sfgs_reports(profileID,memberID,positionID,reportTitle,reportContent,reportDateStamp,reportNumber) values (@profileID,@memberID,@positionID,@reportTitle,@content,@datestamp,@reportNumber)", conn2);
cmd2.Parameters.AddWithValue("@content", reportContent.Text);
cmd2.Parameters.AddWithValue("@memberID", memberID);
cmd2.Parameters.AddWithValue("@profileID", submitProfileID);
cmd2.Parameters.AddWithValue("@positionID", submitPositionID);
cmd2.Parameters.AddWithValue("@reportTitle", reportTitle);
cmd2.Parameters.AddWithValue("@datestamp", dateTime);
cmd2.Parameters.AddWithValue("@reportNumber", reportNum);
cmd2.ExecuteNonQuery();
conn2.Close();
Page.Response.Redirect("default.aspx?ResponseCode=3", false);
}
}
conn.Close();
}
}
正如您所说,您的下拉绑定代码在页面加载事件中...把你的代码放在这个块中,我相信它会起作用。
if(!Page.IsPostBack)
{
using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SFGSConnectionString1"].ConnectionString))
{
string selectQuery1 = "SELECT [profileID], [profileName], [positionID], [positionName] from vw_profile WHERE memberID=@memberID";
SqlCommand cmd1 = new SqlCommand(selectQuery1, conn1);
cmd1.Parameters.AddWithValue("@memberID", memberID);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd1;
DataSet ds = new DataSet();
conn1.Open();
da.Fill(ds);
conn1.Close();
chooseProfile.DataSource = ds;
chooseProfile.DataTextField = "profileName";
chooseProfile.DataValueField = "profileID";
chooseProfile.DataBind();
choosePosition.DataSource = ds;
choosePosition.DataTextField = "positionName";
choosePosition.DataValueField = "positionID";
choosePosition.DataBind();
}
}
因为当您单击按钮时,POSTBACK
发生第一个pageload
事件,然后运行button click
事件。由于您的 Page Load
事件在Button Click
之前运行,因此您的DropDownList
"再次绑定"设置为"绑定前的默认 Value.So"下拉列表,请检查它是否不应该是回发调用。为此,我使用 Page.IsPostBack,它经受住了它的回发调用或正常调用。
尝试将代码放如下:
如果(!IsPostBack({下拉列表名称.项目.清除((;做绑定值的事情}