简体   繁体   中英

how to get a dropdownlist to show a selected value in a gridview?

some of you might find this stupid but i lack the knowledge to get this page to work. I am trying to create a page for registered customers to see their booking with a booking number and to add extra requirements. I have selected dropdownlist and gridview from toolbox and used an sqldatasource to make the connection and get the select value from the database. my problem is when i select from dropdownlist nothings happens and the gridview is showing me the information for all the customers. How can i get the selected value to display in the gridview? help please!I'm using asp.net c# this is the code i have:

<%@ Page Title="" Language="C#" MasterPageFile="~/masterpage.master" AutoEventWireup="true" CodeFile="CustomerBooking.aspx.cs" Inherits="Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <p>
     Booking ID 
     <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Customer_ID" DataValueField="Booking_ID">
         <asp:ListItem>booking_ID</asp:ListItem>
     </asp:DropDownList>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Booking]"></asp:SqlDataSource>
 </p>
 <p>
     <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Booking_ID" DataSourceID="SqlDataSource2">
         <Columns>
             <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
             <asp:BoundField DataField="Booking_ID" HeaderText="Booking_ID" InsertVisible="False" ReadOnly="True" SortExpression="Booking_ID" />
             <asp:BoundField DataField="Customer_ID" HeaderText="Customer_ID" SortExpression="Customer_ID" />
             <asp:BoundField DataField="Cus_FName" HeaderText="Cus_FName" SortExpression="Cus_FName" />
             <asp:BoundField DataField="Cus_LName" HeaderText="Cus_LName" SortExpression="Cus_LName" />
             <asp:BoundField DataField="Bo_num_stay" HeaderText="Bo_num_stay" SortExpression="Bo_num_stay" />
             <asp:BoundField DataField="Bo_Start_Date" HeaderText="Bo_Start_Date" SortExpression="Bo_Start_Date" />
             <asp:BoundField DataField="Bo_End_Date" HeaderText="Bo_End_Date" SortExpression="Bo_End_Date" />
             <asp:BoundField DataField="Bo_status" HeaderText="Bo_status" SortExpression="Bo_status" />
             <asp:BoundField DataField="Bo_extra_req" HeaderText="Bo_extra_req" SortExpression="Bo_extra_req" />
             <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
         </Columns>
     </asp:GridView>
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Booking] WHERE [Booking_ID] = ?" InsertCommand="INSERT INTO [Booking] ([Booking_ID], [Customer_ID], [Cus_FName], [Cus_LName], [Bo_num_stay], [Bo_Start_Date], [Bo_End_Date], [Bo_status], [Bo_extra_req], [UserName]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Booking]" UpdateCommand="UPDATE [Booking] SET [Customer_ID] = ?, [Cus_FName] = ?, [Cus_LName] = ?, [Bo_num_stay] = ?, [Bo_Start_Date] = ?, [Bo_End_Date] = ?, [Bo_status] = ?, [Bo_extra_req] = ?, [UserName] = ? WHERE [Booking_ID] = ?">
         <DeleteParameters>
             <asp:Parameter Name="Booking_ID" Type="Int32" />
         </DeleteParameters>
         <InsertParameters>
             <asp:Parameter Name="Booking_ID" Type="Int32" />
             <asp:Parameter Name="Customer_ID" Type="Int32" />
             <asp:Parameter Name="Cus_FName" Type="String" />
             <asp:Parameter Name="Cus_LName" Type="String" />
             <asp:Parameter Name="Bo_num_stay" Type="Int32" />
             <asp:Parameter Name="Bo_Start_Date" Type="DateTime" />
             <asp:Parameter Name="Bo_End_Date" Type="DateTime" />
             <asp:Parameter Name="Bo_status" Type="String" />
             <asp:Parameter Name="Bo_extra_req" Type="String" />
             <asp:Parameter Name="UserName" Type="String" />
         </InsertParameters>
         <UpdateParameters>
             <asp:Parameter Name="Customer_ID" Type="Int32" />
             <asp:Parameter Name="Cus_FName" Type="String" />
             <asp:Parameter Name="Cus_LName" Type="String" />
             <asp:Parameter Name="Bo_num_stay" Type="Int32" />
             <asp:Parameter Name="Bo_Start_Date" Type="DateTime" />
             <asp:Parameter Name="Bo_End_Date" Type="DateTime" />
             <asp:Parameter Name="Bo_status" Type="String" />
             <asp:Parameter Name="Bo_extra_req" Type="String" />
             <asp:Parameter Name="UserName" Type="String" />
             <asp:Parameter Name="Booking_ID" Type="Int32" />
         </UpdateParameters>
     </asp:SqlDataSource>
 </p>
 </asp:Content>

It's because you don't use parameter in your gridview datasource select command. Try this

SelectCommand="SELECT * FROM [Booking] WHERE Booking_ID = @Id"

And add the selectparameters in your SQLDataSource

<selectparameters>
    <asp:controlparameter name="Id" controlid="DropDownList1" propertyname="SelectedValue"/>
</selectparameters>

EDIT

you need to modify gridview datasource which is SqlDataSource2 in this case. so it should be look like this

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<p>
 Booking ID 
 <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Customer_ID" DataValueField="Booking_ID">
     <asp:ListItem>booking_ID</asp:ListItem>
 </asp:DropDownList>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Booking]"></asp:SqlDataSource>
 </p>
 <p>
 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Booking_ID" DataSourceID="SqlDataSource2">
     <Columns>
         <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
         <asp:BoundField DataField="Booking_ID" HeaderText="Booking_ID" InsertVisible="False" ReadOnly="True" SortExpression="Booking_ID" />
         <asp:BoundField DataField="Customer_ID" HeaderText="Customer_ID" SortExpression="Customer_ID" />
         <asp:BoundField DataField="Cus_FName" HeaderText="Cus_FName" SortExpression="Cus_FName" />
         <asp:BoundField DataField="Cus_LName" HeaderText="Cus_LName" SortExpression="Cus_LName" />
         <asp:BoundField DataField="Bo_num_stay" HeaderText="Bo_num_stay" SortExpression="Bo_num_stay" />
         <asp:BoundField DataField="Bo_Start_Date" HeaderText="Bo_Start_Date" SortExpression="Bo_Start_Date" />
         <asp:BoundField DataField="Bo_End_Date" HeaderText="Bo_End_Date" SortExpression="Bo_End_Date" />
         <asp:BoundField DataField="Bo_status" HeaderText="Bo_status" SortExpression="Bo_status" />
         <asp:BoundField DataField="Bo_extra_req" HeaderText="Bo_extra_req" SortExpression="Bo_extra_req" />
         <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
     </Columns>
 </asp:GridView>
 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Booking] WHERE [Booking_ID] = ?" InsertCommand="INSERT INTO [Booking] ([Booking_ID], [Customer_ID], [Cus_FName], [Cus_LName], [Bo_num_stay], [Bo_Start_Date], [Bo_End_Date], [Bo_status], [Bo_extra_req], [UserName]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Booking] WHERE Booking_ID = @Id" UpdateCommand="UPDATE [Booking] SET [Customer_ID] = ?, [Cus_FName] = ?, [Cus_LName] = ?, [Bo_num_stay] = ?, [Bo_Start_Date] = ?, [Bo_End_Date] = ?, [Bo_status] = ?, [Bo_extra_req] = ?, [UserName] = ? WHERE [Booking_ID] = ?">
     <selectparameters>
         <asp:controlparameter name="Id" controlid="DropDownList1" propertyname="SelectedValue"/>
     </selectparameters>
     <DeleteParameters>
         <asp:Parameter Name="Booking_ID" Type="Int32" />
     </DeleteParameters>
     <InsertParameters>
         <asp:Parameter Name="Booking_ID" Type="Int32" />
         <asp:Parameter Name="Customer_ID" Type="Int32" />
         <asp:Parameter Name="Cus_FName" Type="String" />
         <asp:Parameter Name="Cus_LName" Type="String" />
         <asp:Parameter Name="Bo_num_stay" Type="Int32" />
         <asp:Parameter Name="Bo_Start_Date" Type="DateTime" />
         <asp:Parameter Name="Bo_End_Date" Type="DateTime" />
         <asp:Parameter Name="Bo_status" Type="String" />
         <asp:Parameter Name="Bo_extra_req" Type="String" />
         <asp:Parameter Name="UserName" Type="String" />
     </InsertParameters>
     <UpdateParameters>
         <asp:Parameter Name="Customer_ID" Type="Int32" />
         <asp:Parameter Name="Cus_FName" Type="String" />
         <asp:Parameter Name="Cus_LName" Type="String" />
         <asp:Parameter Name="Bo_num_stay" Type="Int32" />
         <asp:Parameter Name="Bo_Start_Date" Type="DateTime" />
         <asp:Parameter Name="Bo_End_Date" Type="DateTime" />
         <asp:Parameter Name="Bo_status" Type="String" />
         <asp:Parameter Name="Bo_extra_req" Type="String" />
         <asp:Parameter Name="UserName" Type="String" />
         <asp:Parameter Name="Booking_ID" Type="Int32" />
     </UpdateParameters>
 </asp:SqlDataSource>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM