0

我有一个 DropDownList ,我想在其中选择一个 ID 及其名称两次,将其放入 aDataSet并绑定到 a 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();
    }
}
4

2 回答 2

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 ClickDropDownListBinded Again 设置为其默认值之前运行。所以在 Binding 下拉列表之前检查它不应该是 PostBack 调用。为此,我使用 Page.IsPostBack 来风化它的 PostBack 调用或正常调用。

于 2013-07-09T03:36:04.143 回答
1

试着把代码写成这样:

if(!IsPostBack) { dropdownlistname.items.clear(); 做一些事情来绑定值}

于 2013-07-09T04:19:24.990 回答