【问题标题】:dropdownlist selected value下拉列表选择的值
【发布时间】:2012-05-24 23:48:25
【问题描述】:

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

我有两张桌子:Hotel 和 HotelRooms Hotel 表有HotelID 和其他酒店详细信息,HotelID 位于HotelRooms 中,具有不同的房型,每种房型都有描述。 一间酒店可以有多种房型。

我有一个包含HotelID 和RoomIDs 的DropDownList。 HotelID 来自会话变量。 HotelID 是 DropDownList; but the list displaysRoomType. WhenRoomtypesare selected I want to display aGridView` 的值,其中包含酒店房间的详细信息,如描述、价格等......

我不能这样做,因为我的 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>

【问题讨论】:

  • 你能给我们看一些代码吗? ^^
  • 您在哪里以及如何填写您的下拉列表?你能证明一下吗?
  • 又更新了,见上
  • 我认为你搞错了。具有相同值的 DataValueField 有什么意义?如果您的 DataTextField 是 RoomID,您是否向用户显示了 Room Id 的列表?这是不好的。您可以发布您正在使用的表格以及列名吗?
  • 不确定你所说的相同值是什么意思,但已经用下拉列表的代码更新了我上面的问题

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


【解决方案1】:

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

【讨论】:

  • 我的 dropdownlist2 应该启用回发吗?
  • @user1270384 可以,只要您希望在用户选择选项时更新 GridView。
  • @user1270384 很酷,您是否尝试过在DropDownList2_SelectedIndexChanged 中放置一个断点以确保正在调用该方法?另外,您是否检查过 intResortID 和 strRoomType 返回的值是否正确?
  • @user1270384 等等,strRoomType 是否应该是用户正在查看的内容?如果是这样,那么前两行应该是string intResortID = DropDownList2.SelectedValue;string strRoomType = DropDownList2.SelectedText;
  • @user1270384 是的,类似的。如果您在调试器中运行,您可以检查这些值是否正确。
【解决方案2】:

试试下拉列表的SelectedItem.Text。

【讨论】:

  • 我得到错误对象引用未设置为对象实例
  • 下拉列表中有哪些字段? HotelID 作为 DataValueField 和 RoomID 作为 DataTextField?
  • 所以要测试一下,您是否在 DropDownList 的 OnSelectedIndexChange 事件上运行代码?您应该能够从事件的发送者访问 DropDownList 属性。
  • 是的,我正在下拉列表的 selectedindexchange 事件中运行代码
  • 看来这是您现阶段唯一遇到的问题。您可以发布您尝试在事件中检索值的代码吗?
【解决方案3】:

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

【讨论】:

  • 好吧,@John 我的 HotelRoom ID 是主键以及 HotelRoom 表的 ID,但是因为我的 hotelID 是数据字段值而不是酒店房间 ID,所以我无法在我的网格视图中显示任何内容,因为没有房间类型通过
  • 您可以添加一个 id 字段作为新的主键,而不是复合主键,并将其用作值?这取决于用户首先选择酒店,然后在下拉列表中填写该酒店的房间。
  • 我不明白你的意思,我已经尝试了酒店列表的一个下拉列表和房间列表的另一个下拉列表,但是当我选择房间类型时,gridview 没有任何反应
  • 据我了解,没有唯一标识房间的单一 ID。唯一 id 来自 HotelID 和 RoomID 的组合?这意味着您无法从下拉列表中加载房间数据,因为下拉列表只有一个值字段。您需要做的是在房间表中添加另一个字段,即 IDENTITY 字段并将其设为主键。然后,您将其用作下拉列表中的值字段。因此,您有一个酒店列表,选择时,选择HotelID填充使用新键作为值字段的其他房间列表...希望有助于... span>
  • 这就是为什么我需要保留度假村 ID 的数据字段值,但我还需要从下拉列表中拉出 RoomType,是不是有 selectedindex 之类的东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多