【问题标题】:Updating value picked from DDL in DetailView在 DetailView 中更新从 DDL 中选取的值
【发布时间】:2015-06-04 14:09:44
【问题描述】:
(我在下面的Answer中找到了它应该完成的方式。
我正在编辑我的问题并删除我最初放在这里的代码,这是一团糟,只留下定义)。
在 VS2013 中使用 C# 的 Win7、ASP4.5、empty_web_app。在访问两个表的非常简单的项目中重新创建:
第一个表“学生”
-
student_ID
- 学生姓名
-
student_course_ID(是外键)
第二张桌子“课程”
在我的网页中,我的 DetailView1 显示了 student_ID 取自 txbStudent_ID 的学生的详细信息。
DetailView1 具有编辑删除和新建。
在 UPDATE 或 INSERT 模式下,我需要在下拉列表中显示 course_name(而不是课程 ID)并相应地更新/插入。
后面没有代码。
我在下面的回答也适用于 GRIDVIEW。
加迪
【问题讨论】:
标签:
asp.net
visual-studio
drop-down-menu
updating
detailview
【解决方案1】:
我用 aspx 编码的方式大错特错。
因此,对于像我现在这样的未来初学者来说,这是正确的方法...
(在https://msdn.microsoft.com/en-us/library/ms178294(v=vs.140).aspx的帮助下)
<asp:Label ID="Label1" runat="server" Text="Student_ID"></asp:Label>
<asp:TextBox ID="txbStudent_ID" runat="server"></asp:TextBox>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="student_ID" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="student_ID" HeaderText="student_ID" InsertVisible="False" ReadOnly="True" SortExpression="student_ID" />
<asp:BoundField DataField="student_name" HeaderText="student_name" SortExpression="student_name" />
<asp:TemplateField HeaderText="student_course_ID" SortExpression="student_course_ID">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
DataSourceID="SqlDataSource2"
DataTextField="course_name"
DataValueField="course_ID"
SelectedValue='<%# Bind("student_course_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList
ID="DropDownList2"
runat="server"
DataSourceID="SqlDataSource2"
DataTextField="course_name"
DataValueField="course_ID"
SelectedValue='<%# Bind("student_course_ID") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label
ID="Label1"
runat="server"
Text='<%# Bind("student_course_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" ShowDeleteButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Course_Student_Connection %>"
InsertCommand="INSERT INTO [Students] ([student_name], [student_course_ID]) VALUES (@student_name, @student_course_ID)"
SelectCommand="SELECT * FROM [Students] WHERE ([student_ID] = @student_ID)"
UpdateCommand="UPDATE [Students] SET [student_name] = @student_name, [student_course_ID] = @student_course_ID WHERE [student_ID] = @student_ID"
DeleteCommand="DELETE FROM [Students] WHERE [student_ID] = @student_ID">
<DeleteParameters>
<asp:Parameter Name="student_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="student_name" Type="String" />
<asp:Parameter Name="student_course_ID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="txbStudent_ID" Name="student_ID" PropertyName="Text" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="student_name" Type="String" />
<asp:Parameter Name="student_course_ID" Type="Int32" />
<asp:Parameter Name="student_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:Course_Student_Connection %>"
SelectCommand="SELECT [course_ID], [course_name] FROM [Courses]">
</asp:SqlDataSource>
我确实希望它可以为其他人节省一些时间。
Stackoverflow 是网络上最好的也是迄今为止最好的问答网站!!!
加迪。