Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改后,dropdownlist选择的asp值重置为0_C#_Asp.net - Fatal编程技术网

C# 更改后,dropdownlist选择的asp值重置为0

C# 更改后,dropdownlist选择的asp值重置为0,c#,asp.net,C#,Asp.net,我有一个页面,上面有一个下拉列表,你可以在其中选择一个学期。每个都有自己的唯一id。选择学期后,我想从存储过程中填充课程的下拉列表。此sp使用一个名为semesterID的参数,因此它获取具有选定semesterID的所有课程。我的问题是学期列表总是返回0。为什么会发生这种情况 这是我的C代码: private void PopulateSemesterList() { string connstring; connstring

我有一个页面,上面有一个下拉列表,你可以在其中选择一个学期。每个都有自己的唯一id。选择学期后,我想从存储过程中填充课程的下拉列表。此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 />

我敢打赌,你的页面负载是这样的:

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
}

问题是PopulateseMeterList会清除选择,因此您应该仅在初始加载时执行此操作,而不是在每次回发时执行此操作。页面加载发生在所有事件之前,如DropDownList的SelectedIndexChanged事件。

我敢打赌,您的页面加载如下所示:

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
}

问题是PopulateseMeterList会清除选择,因此您应该仅在初始加载时执行此操作,而不是在每次回发时执行此操作。页面加载发生在所有事件之前,如DropDownList的SelectedIndexChanged事件。

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


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