0

在 ASP.Net Web 表单上,有几个 TextBoxes 和 2 DropDownLists。这些 DropDownLists 之一未按预期工作。如果用户更改 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 的数据源:

<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 的数据源:

<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