简体   繁体   English

下拉列表选择的值

[英]dropdownlist selected value

This may seem very trivial, I used to know how to do this at one time but for some reason I can't seem to get my head around it this time: 这似乎很琐碎,我曾经一次知道如何执行此操作,但是由于某种原因,我这次似乎无法解决这个问题:

I have two tables: Hotel and HotelRooms The Hotel table has HotelID and other hotel details and the HotelID is found in HotelRooms with different roomtypes, each roomtype has a description. 我有两个表: HotelHotelRooms Hotel表中包含HotelID和其他酒店详细信息,并且可以在具有不同HotelID HotelRooms找到HotelID ,每种房型都有描述。 One Hotel can have many rooms types. 一间酒店可以有多种房型。

I have a DropDownList that contains HotelID and RoomIDs . 我有一个包含HotelIDRoomIDsDropDownList HotelID comes from a session variable. HotelID来自会话变量。 HotelID is the value for the DropDownList; but the list displays HotelIDDropDownList; but the list displays的值DropDownList; but the list displays DropDownList; but the list displays RoomType . When DropDownList; but the list displays RoomType . When . When Roomtypes are selected I want to display a GridView` with the hotel room details like description, price etc.... . When Roomtypes are selected I want to display a与酒店房间的细节,如描述,价格等GridView` ....

I can't do this because the value for my DropDownList is HotelID which is mapped to the Session ID . 我无法执行此操作,因为我的DropDownList的值是被映射到Session ID HotelID How do I get the HotelRoom details from the selected value of the DropDownList, please? 请问如何从DropDownList的选定值中获取HotelRoom的详细信息?

Update: 更新:

Code for Gridview: Gridview的代码:

        string intResortID = Request.QueryString("intResortID ")
        string strRoomType = DropDownList2.SelectedValue;
        string connStr = ConfigurationManager.ConnectionStrings["bdsConnectionString"].ConnectionString;
        SqlConnection Con = new SqlConnection(connStr);
        SqlDataAdapter sdr = new SqlDataAdapter("SELECT TOP (100) PERCENT tblAvail.dtm, tblResortsRooms.strRoomType, tblResortsRooms.strDescription, tblAvail.intQty, tblAvail.curPrice, tblAvail.intResortID, tblResortsRooms.intWSCode FROM tblAvailable INNER JOIN tblResortsRooms ON tblAvail.intResortID = tblResortsRooms.intResortID AND tblAvail.strRoomType = tblResortsRooms.strRoomType WHERE (tblResortsRooms.curRecRate > 0) AND (tblAvail.intResortID = @intResortID) AND (tblAvail.strRoomType = @strRoomType) AND (tblAvailable.dtm >= { fn CURDATE() }) ORDER BY tblResortsRooms.strRoomType",Con);
        SqlParameter ResID = new SqlParameter("@intResortID", intResortID);
        SqlParameter RoomType = new SqlParameter("@strRoomType", strRoomType);
        sdr.SelectCommand.Parameters.Add(ResID);
        sdr.SelectCommand.Parameters.Add(RoomType);

      <asp:DropDownList ID="DropDownList2" runat="server" 
        DataSourceID="SqlDataSource2" DataTextField="strRoomType" 
        DataValueField="intResortID" 
        onselectedindexchanged="DropDownList2_SelectedIndexChanged" 
        AutoPostBack="True">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
       SelectCommand="SELECT [intResortID], [strRoomType] FROM [tblResortsRooms] WHERE ([intResortID] = @intResortID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="intResortID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

尝试下拉列表的SelectedItem.Text

'DropDownList1.SelectedValue' should give you the value for @intResortID . 'DropDownList1.SelectedValue'应该为您提供@intResortID的值。
'DropDownList2.SelectedValue' should give you the value for @strRoomType . 'DropDownList2.SelectedValue'应该为您提供@strRoomType的值。

DropDownList2_SelectedIndexChanged should run the query by filling something (like a DataTable ). DropDownList2_SelectedIndexChanged应该通过填充某些内容(例如DataTable )来运行查询。
The filled DataTable should be bound to your GridView. 填充的DataTable应该绑定到您的GridView。

Try something like the following in your DropDownList2_SelectedIndexChanged method: DropDownList2_SelectedIndexChanged方法中尝试以下操作:

string intResortID = Request.QueryString("intResortID ");
string strRoomType = DropDownList2.SelectedValue;
string connStr = ConfigurationManager.ConnectionStrings["bdsConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(connStr);
SqlDataAdapter sdr = new SqlDataAdapter("SELECT TOP (100) PERCENT tblAvail.dtm, tblResortsRooms.strRoomType, tblResortsRooms.strDescription, tblAvail.intQty, tblAvail.curPrice, tblAvail.intResortID, tblResortsRooms.intWSCode FROM tblAvailable INNER JOIN tblResortsRooms ON tblAvail.intResortID = tblResortsRooms.intResortID AND tblAvail.strRoomType = tblResortsRooms.strRoomType WHERE (tblResortsRooms.curRecRate > 0) AND (tblAvail.intResortID = @intResortID) AND (tblAvail.strRoomType = @strRoomType) AND (tblAvailable.dtm >= { fn CURDATE() }) ORDER BY tblResortsRooms.strRoomType", Con);
SqlParameter ResID = new SqlParameter("@intResortID", intResortID);
SqlParameter RoomType = new SqlParameter("@strRoomType", strRoomType);
sdr.SelectCommand.Parameters.Add(ResID);
sdr.SelectCommand.Parameters.Add(RoomType);
DataTable results = new DataTable();

sdr.Fill(results);

resultsGridView.DataSource = results;   //Assuming resultsGridView is the name of the GridView on your ASPX page.
resultsGridView.DataBind();

Sounds like you need a new ID (Primary Key) in your HotelRooms table which uniquely identifies each room. 听起来您需要在HotelRooms表中使用一个新ID(主键)来唯一标识每个房间。 If you had that, you could use that as the value for the dropdown and the problem goes away. 如果有,可以将其用作下拉菜单的值,然后问题就会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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