0

この問題は簡単な解決策があると思いますが、私はここ数日頭を悩ませています。ストアドプロシージャから学生のリストを動的に取得するWebアプリケーションがあります。各生徒の詳細情報とその後の授業情報を見たいと思います。[生徒の詳細]ページには、生徒が参加しているすべてのクラスを含むドロップダウンリストがあり、クラスを選択すると、[コミュニティパートナー]フィールドが更新されます。

SelectedIndexChangedメソッドを使用していますが、これを機能させるには、AutoPostBackをTrueに設定する必要があります。これにより、ページがリロードされ、ドロップダウンリストと選択された値もリロードされます。このコードのいくつかの異なる構成を試しましたが、結果はありませんでした。

これが私のascxファイルです

<asp:DropDownList ID="StudentCourses" runat="server"></asp:DropDownList>

そしてこれが私のascx.csファイルです

protected void Page_PreRender(object sender, EventArgs e)
    {
        if (Session["StudentID"] != null)
        {

            int studentId = Convert.ToInt32(Session["StudentID"]);

            Student student = studentRepository.GetStudent(studentId);
            StudentCourses_SelectedIndexChanged(sender, e);
            StudentCommunityPartner.Text = StudentCourses.SelectedItem.Value;
                ...

そしてこれが私のSelectedIndexChangedメソッドです

protected void StudentCourses_SelectedIndexChanged(object sender, EventArgs e)
    {
        IList<KeyValuePair<Course, CommunityPartner>> courseList = studentRepository.GetStudentCourses(Convert.ToInt32(Session["StudentID"]));
        StudentCourses.DataSource = courseList;
        StudentCourses.DataBind();

        int ctr = 0;
        foreach (KeyValuePair<Course, CommunityPartner> kvp in courseList)
        {
            if (ctr < StudentCourses.Items.Count)
            {
                StudentCourses.Items[ctr].Text = kvp.Key.CourseCode;
                StudentCourses.Items[ctr].Value = kvp.Value.PartnerName;
                ctr++;
            }
            else ctr = 0;
        }
        StudentCommunityPartner.Text = StudentCourses.SelectedItem.Value;
    }

いくつかの組み合わせを試しましたが、ドロップダウンリストを毎回更新せずにページのコンテンツを適切に変更する方法がわかりません。よろしくお願いします。

4

1 に答える 1

0

ドロップダウンの変更からテキストボックスを設定するには、次を参照してください。

jQueryでドロップダウンリストの値をテキストボックスに設定する

やりたいことがもっとある場合は、ドロップダウンから選択した値をポストバック時にビューステートに保持する必要があります。その値を保存してみてください

var Selected = StudentCourses.SelectedValue;

ドロップダウンに入力し、選択した値を保存された値で設定します

StudentCourses.SelectedValue = 選択済み;

于 2013-02-28T21:42:22.213 に答える