下拉列表所选值

本文关键字:下拉列表 | 更新日期: 2023-09-27 18:35:12

这看起来可能很微不足道,我曾经曾经知道如何做到这一点,但由于某种原因,这次我似乎无法理解它:

我有两个表:HotelHotelRoomsHotel表有HotelID和其他酒店详细信息,HotelIDHotelRooms找到不同的房型,每种房型都有描述。一家酒店可以有多种房型。

我有一个包含HotelIDRoomIDsDropDownListHotelID来自会话变量。 HotelIDDropDownList; but the list displays房型. When房型are selected I want to display a GridView'的值,其中包含酒店房间详细信息,如描述,价格等。

我不能这样做,因为我的下拉列表的值是映射到Session IDHotelID。请问如何从下拉列表的选定值中获取酒店房间详细信息?

更新:

网格视图的代码:

        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>

下拉列表所选值

'DropDownList1.SelectedValue' 应该给你@intResortID的值。
"DropDownList2.SelectedValue"应该为您提供@strRoomType的值。

DropDownList2_SelectedIndexChanged应该通过填充某些内容(如数据表)来运行查询。
填充的数据表应绑定到 GridView。

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();

尝试SelectedItem.Text下拉列表。

听起来您需要在 HotelRooms 表中使用一个新 ID(主键)来唯一标识每个房间。如果你有它,你可以使用它作为下拉列表的值,问题就消失了。