下拉列表选择的值

时间:2012-05-24 23:48:26

标签: c# asp.net gridview drop-down-menu selectedvalue

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

我有两张牌桌:HotelHotelRooms Hotel表格包含HotelID和其他酒店详细信息,HotelID位于HotelRooms,且房型不同,每种房型都有说明。 一个酒店可以有很多房型。

我的DropDownList包含HotelIDRoomIDsHotelID来自会话变量。 HotelIDDropDownList; but the list displays RoomType . When Roomtypes are selected I want to display a GridView`的值,其中包含描述,价格等酒店房间详情....

我不能这样做,因为我的DropDownList的值是HotelID,它被映射到Session ID。如何从DropDownList的选定值中获取HotelRoom详细信息?

更新

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>

3 个答案:

答案 0 :(得分:2)

尝试下拉列表的SelectedItem.Text

答案 1 :(得分:2)

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

DropDownList2_SelectedIndexChanged应按filling某些内容运行查询(例如DataTable)。
填充的DataTable应绑定到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();

答案 2 :(得分:0)

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