在onchange之后,dropdownlist选择的asp值重置为0

时间:2016-03-04 10:28:55

标签: c# asp.net

我有一个带有下拉列表的页面,您可以在其中选择一个学期。每个人都拥有自己独特的身份。选择一个学期后,我想从存储过程(sp)填充课程下拉列表。这个sp使用一个名为semesterID的参数,因此它将获得具有所选semesterID的所有课程。我的问题是学期列表总是返回0.为什么会发生这种情况?

这是我的C#代码:

 private void PopulateSemesterList()
        {
            string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);

            SqlCommand cmd = new SqlCommand("dbo.GetSemesters", conn);
            conn.Open();

            DDSemesters.DataSource = cmd.ExecuteReader();
            DDSemesters.DataTextField = "semesterName";
            DDSemesters.DataValueField = "semesterId";
            this.DataBind();

            conn.Close();
            conn.Dispose();

            DDSemesters.Items.Insert(0, new ListItem("Select Semester", "0"));
            DDSemesters.SelectedIndex = 0;
        }

        protected void DDSemesters_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);

            SqlCommand cmd = new SqlCommand("dbo.GetCourses", conn);
            cmd.Parameters.Add(new SqlParameter("@semesterId", DDSemesters.SelectedItem.Value));
            selectedCourseLbl.Text = DDSemesters.SelectedItem.Value;

            conn.Open();

            DDCourses.DataSource = cmd.ExecuteReader();
            DDCourses.DataTextField = "courseName";
            DDCourses.DataValueField = "courseId";
            this.DataBind();

            conn.Close();
            conn.Dispose();

            DDCourses.Items.Insert(0, new ListItem("Select Course", "0"));
            DDCourses.SelectedIndex = 0;
            DDCourses.Visible = true;
            CoursesLbl.Visible = true;
        }

这是我的ASP:

<asp:Label ID="Label1" runat="server" Text="Select Semester"></asp:Label><br />
        <asp:DropDownList ID="DDSemesters" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDSemesters_SelectedIndexChanged" DataTextField="semesterName" DataValueField="semesterId"></asp:DropDownList><br />
        <asp:Label ID="selectedCourseLbl" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="CoursesLbl" runat="server" Text="Select Course" Visible="false"></asp:Label><br />
        <asp:DropDownList visible="false" ID="DDCourses" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDCourses_SelectedIndexChanged" DataTextField="courseName" DataValueField="courseId"></asp:DropDownList><br />

2 个答案:

答案 0 :(得分:4)

我打赌你的Page_Load看起来像这样的钱:

protected void Page_Load(Object sender, EventArgs e)
{
    PopulateSemesterList();
    // maybe other code
}

将其更改为

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
        PopulateSemesterList();
    // maybe other code
}

问题是PopulateSemesterList清除了选择,因此您应该仅在初始加载时执行此操作,而不是在每次回发时执行此操作。 Page_Load发生在所有事件之前,例如SelectedIndexChanged的{​​{1}}事件。

答案 1 :(得分:0)

使用Datatable获取下拉列表中的值。

 string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);
            SqlCommand cmd = new SqlCommand("dbo.GetSemesters", conn);
            conn.Open();
           DataTable dt  = new DataTable();
           dt = cmd.ExecuteReader();
           if (dt.Rows.Count > 0)
           {
            DDSemesters.DataSource = dt;
            DDSemesters.DataTextField = "semesterName";
            DDSemesters.DataValueField = "semesterId";
            DDSemesters.DataBind();
            }
            conn.Close();
            conn.Dispose();
            DDSemesters.Items.Insert(0, "Select Semester");

string connstring; connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString; SqlConnection conn = new SqlConnection(connstring); SqlCommand cmd = new SqlCommand("dbo.GetSemesters", conn); conn.Open(); DataTable dt = new DataTable(); dt = cmd.ExecuteReader(); if (dt.Rows.Count > 0) { DDSemesters.DataSource = dt; DDSemesters.DataTextField = "semesterName"; DDSemesters.DataValueField = "semesterId"; DDSemesters.DataBind(); } conn.Close(); conn.Dispose(); DDSemesters.Items.Insert(0, "Select Semester");