0

ASP.Net Web フォームには、いくつかの TextBoxes と 2 つの DropDownLists があります。これらの DropDownLists の 1 つが期待どおりに機能していません。ユーザーが DropDownList に表示された値を変更した場合、変更された値はデータベースに保存されません。ユーザーは、問題なく同じ DropDownList を使用してデータベースにデータを挿入できます。現在、値を変更する唯一の方法は、データの行を削除し、DropDownList からの新しい値で再挿入することです。

データがデータベースに保存されると、他の DropDownList に対するすべての変更と、「クラス」DropDownList を除くフォーム上の他のすべてのコントロールが保存されます。私たちのコーディングを見て、どこでエラーが発生したかを特定できますか?

これは、作業中の DropDownList のマークアップです。

                <asp:TemplateField HeaderText="Student:" SortExpression="StudentID">
                    <EditItemTemplate>

                        <asp:DropDownList 
                            ID="DropDownListStudent" 
                            Runat="server"
                            DataSourceID="SqlDataSourceStudents"
                            DataTextField = "StudentName"
                            DataValueField="ID"
                            SelectedValue='<%# Bind("StudentID") %>'
                            ForeColor="Blue">
                        </asp:DropDownList>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidatorEditStudent" runat="server" ControlToValidate="DropDownListStudent" 
                            ErrorMessage="Please select a Student here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
                            SetFocusOnError="True" Display="Dynamic">
                        </asp:RequiredFieldValidator>

                    </EditItemTemplate>

                    <InsertItemTemplate>

                        <asp:DropDownList 
                            ID="DropDownListStudent" 
                            Runat="server"
                            DataSourceID="SqlDataSourceStudents"
                            DataTextField = "StudentName"
                            DataValueField="ID"
                            SelectedValue='<%# Bind("StudentID") %>'
                            ForeColor="Blue">
                        </asp:DropDownList>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertStudent" runat="server" ControlToValidate="DropDownListStudent" 
                            ErrorMessage="Please select a Student here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
                            SetFocusOnError="True" Display="Dynamic">

                        </asp:RequiredFieldValidator>
                     </InsertItemTemplate>

                    <ItemTemplate>

                        <asp:DropDownList 
                            ID="DropDownListStudent" 
                            Runat="server"
                            DataSourceID="SqlDataSourceStudents"
                            DataTextField = "StudentName"
                            DataValueField="ID"
                            SelectedValue='<%# Bind("StudentID") %>'
                            Enabled="false"
                            ForeColor="Blue"
                            Font-Bold="true"> 
                        </asp:DropDownList>

                     </ItemTemplate>
                </asp:TemplateField>

これは、機能していない DropDownList のマークアップです。

               <asp:TemplateField HeaderText="Class:" SortExpression="ClassID">
                    <EditItemTemplate>

                        <asp:DropDownList 
                            ID="DropDownListClassEdit" 
                            Runat="server"
                            DataSourceID="SqlDataSourceClasses"
                            DataTextField = "ClassName"
                            DataValueField="ID"
                            SelectedValue='<%# Bind("ClassID") %>'
                            ForeColor="Blue">
                        </asp:DropDownList>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidatorEditClass" runat="server" ControlToValidate="DropDownListClassEdit" 
                            ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
                            SetFocusOnError="True" Display="Dynamic">
                        </asp:RequiredFieldValidator>

                    </EditItemTemplate>

                    <InsertItemTemplate>

                        <asp:DropDownList 
                            ID="DropDownListClassInsert" 
                            Runat="server"
                            DataSourceID="SqlDataSourceClasses"
                            DataTextField = "ClassName"
                            DataValueField="ID"
                            SelectedValue='<%# Bind("ClassID") %>'
                            AppendDataBoundItems="True"
                            ForeColor="Blue"
                            OnDataBinding="DropDownListClassInsert_DataBinding">
                        </asp:DropDownList>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertClass" runat="server" ControlToValidate="DropDownListClassInsert" 
                            ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
                            SetFocusOnError="True" Display="Dynamic">
                        </asp:RequiredFieldValidator>
                     </InsertItemTemplate>

                    <ItemTemplate>

                        <asp:DropDownList 
                            ID="DropDownListClass" 
                            Runat="server"
                            DataSourceID="SqlDataSourceClasses"
                            DataTextField = "ClassName"
                            DataValueField="ID"
                            SelectedValue='<%# Bind("ClassID") %>'
                            Enabled="false"
                            ForeColor="Blue"
                            Font-Bold="true"> 
                        </asp:DropDownList>

                     </ItemTemplate>
                </asp:TemplateField>

これは、作業中の DropDownList の DataSource です。

<asp:SqlDataSource 
    ID="SqlDataSourceStudents" 
    runat="server" 

    ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>" 

    SelectCommand=
        "SELECT NULL AS ID, NULL AS StudentName 
   UNION SELECT ID, Surname + ', ' + Forename AS StudentName 
    FROM Students
ORDER BY 2">
</asp:SqlDataSource>

これは、機能していない DropDownList の DataSource です。

<asp:SqlDataSource 
    ID="SqlDataSourceClasses" 
    runat="server" 

    ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>" 

    SelectCommand=
        "SELECT NULL AS ID, NULL AS ClassName, NULL AS Grade
   UNION SELECT ID, ClassName + ' *** Grade: ' + Grade AS ClassName, Grade 
           FROM Classes
       ORDER BY 2, 3">
</asp:SqlDataSource>

* アップデート *

原因がわかりました。リンク フィールドの一部ではないデータベース テーブル フィールド名を DataKeyNames に配置しないという難しい方法があることがわかりました。私は ClassID を次のようにそこに持っていました:

<asp:DetailsView 
            ID="DetailsView" 
            runat="server" 
            AutoGenerateRows="False" 
            Height="50px" 
            Width="207px" 
            DataSourceID="SqlDataSourceDetails"
            DataKeyNames="ID,ClassID"
            OnItemCommand="DetailsViewDetails_ItemCommand">

私はそれを次のように変更しました:

<asp:DetailsView 
            ID="DetailsView" 
            runat="server" 
            AutoGenerateRows="False" 
            Height="50px" 
            Width="207px" 
            DataSourceID="SqlDataSourceDetails"
            DataKeyNames="ID"
            OnItemCommand="DetailsViewDetails_ItemCommand">

そして問題は解決しました!

4

0 に答える 0