更新从 DetailView 中的 DDL 选取的值

Updating value picked from DDL in DetailView

(我找到了应该完成的方式,如下面的 答案 所示。
我正在编辑我的问题并删除我最初放在这里的代码,那是一团糟,只留下定义)。

Win7、ASP4.5、empty_web_app 在 VS2013 中使用 C#。在访问两个 table 的非常简单的项目中重新创建:

第一table“学生

第 2 table“课程

在我的网页中,我有 DetailView1 显示 student_ID[=55 学生的详细信息=]取自txbStudent_ID.
DetailView1 有编辑删除和新建。
在更新或插入模式下,我需要在下拉列表中显示 course_name(而不是课程 ID),并相应地显示 update/insert。

没有隐藏代码。

我下面的回答也适用于 GRIDVIEW。

加迪

他们编码 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>

我希望它能为其他人节省一些时间。
Whosebug 是最伟大的,也是迄今为止最好的网络问答网站!!!
加迪.