2015-03-31 54 views
0

(我覺得它應該做如圖我回答以下的方式。
我編輯我的問題,並刪除我最初把這裏的代碼,這是一個爛攤子,只留下定義)。更新值從DDL中拾取的DetailView

VS7中的Win7,ASP4.5,empty_web_app與C#。在一個非常簡單的項目訪問兩個表重建:

1表 「學生

  • student_id數據
  • student_name
  • 小號tudent_course_ID(被Forign鍵)

第2表 「課程

  • COURSE_ID
  • 課程名稱

在我的網頁我有DetailView1顯示學生誰是student_id數據txbStudent_ID拍攝的細節。
DetailView1有編輯刪除和新建。
在UPDATE或INSERT模式下,我需要在下拉列表中顯示course_name(而不是課程ID),並相應地更新/插入。

沒有代碼背後。

我的回答也適用於GRIDVIEW。

Gadi

回答

0

我錯了他們的方式我編碼了我的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是最棒的,迄今爲止最好的Q &網站上的網站!
Gadi。