我有一个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();
}
}
答案 0 :(得分:2)
正如你所说的你的DropDown绑定代码在页面加载事件中...把你的代码放在这个块中我相信它会工作..
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
Binded再次设置为其默认值之前运行。因此,在Binding下拉列表之前检查它不应该是PostBack Call.For我使用Page。 IsPostBack它的PostBack Call或正常通话。
答案 1 :(得分:1)
尝试将代码放在这样的代码:
如果(!的IsPostBack) { dropdownlistname.items.clear(); 做一些事情来约束价值 }