在ASP.Net网络表单上,有几个TextBoxes和2个DropDownLists。其中一个DropDownLists没有按预期工作。如果用户更改DropDownList中的显示值,则更改的值不会保存回数据库。用户可以使用相同的DropDownList将数据插入数据库,而不会出现任何问题。当前更改值的唯一方法是删除数据行并使用DropDownList中的新值重新插入数据。
当数据保存回数据库时,除了“Class”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的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">
问题消失了!