C# 下拉列表选择值
这可能看起来很琐碎,我曾经知道如何做到这一点,但由于某种原因,这次我似乎无法集中注意力:C# 下拉列表选择值,c#,asp.net,gridview,drop-down-menu,selectedvalue,C#,Asp.net,Gridview,Drop Down Menu,Selectedvalue,这可能看起来很琐碎,我曾经知道如何做到这一点,但由于某种原因,这次我似乎无法集中注意力: <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="strRoomType" DataValueField="intResortID" onselectedindexchanged="DropDo
<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>
我有两张桌子:Hotel
和HotelRooms
Hotel
表包含HotelID
和其他酒店详细信息,HotelID
可在HotelRooms
中找到,每个房间类型都有描述。
一家酒店可以有多种房间类型
<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>
我有一个DropDownList
,其中包含HotelID
和roomid
HotelID
来自会话变量HotelID
是DropDownList的值;但列表显示的是
RoomType。
当选中
Roomtypes时,我想显示一个
GridView`显示酒店房间的详细信息,如描述、价格等
<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>
我不能这样做,因为我的DropDownList的值是HotelID
,它映射到会话ID
。我如何从下拉列表的选定值中获取酒店房间详细信息
<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>
更新:
<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>
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>
<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>
试试dropdownlist的SelectedItem.Text
。试试dropdownlist的SelectedItem.Text
。听起来您需要在HotelRooms表中添加一个新的ID(主键),它唯一地标识每个房间。如果有,您可以使用该值作为下拉列表的值,问题就会消失。听起来您需要在HotelRooms表中添加一个新的ID(主键),以唯一地标识每个房间。如果您有,您可以使用该值作为下拉列表的值,问题就会消失。'DropDownList1.SelectedValue'应该为您提供@intrestorid
<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>
“DropDownList2.SelectedValue”应该为您提供@stroomtype
的值
<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>
DropDownList2\u SelectedIndexChanged
应通过某种方式(如a)运行查询。填充的数据表应该绑定到GridView
<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>
在DropDownList2\u SelectedIndexChanged
方法中尝试以下操作:
<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>
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();
“DropDownList1.SelectedValue”应该为您提供
@intrestorid
的值“DropDownList2.SelectedValue”应该为您提供
@stroomtype
的值
<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>
DropDownList2\u SelectedIndexChanged
应通过某种方式(如a)运行查询。填充的数据表应该绑定到GridView
<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>
在DropDownList2\u SelectedIndexChanged
方法中尝试以下操作:
<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>
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();
你能至少给我们看一些代码吗^^您在哪里以及如何填写您的下拉列表?你能展示一下吗?再次更新,请看上面我想你搞错了。拥有具有相同值的DataValueField有什么意义?如果您的DataTextField是RoomID,您是否向用户显示房间Id的列表?那不好。你能把你正在使用的表格和列名一起发布吗?不知道相同的值是什么意思,但已经用dropdownlist的代码更新了我上面的问题。你能至少给我们展示一些代码吗^^您在哪里以及如何填写您的下拉列表?你能展示一下吗?再次更新,请看上面我想你搞错了。拥有具有相同值的DataValueField有什么意义?如果您的DataTextField是RoomID,您是否向用户显示房间Id的列表?那不好。您可以发布您正在使用的表以及列名吗?不确定相同的值是什么意思,但已经用dropdownlistI get error object reference的代码更新了我上面的问题没有设置为object的实例下拉列表中有哪些字段?HotelID作为DataValueField,RoomID作为DataTextField?那么为了测试,您是否在DropDownList的OnSelectedIndexChange事件上运行代码?您应该能够从事件的发送方访问DropDownList属性。是的,我正在DropDownList的selectedindexchange事件中运行代码。这似乎是您在此阶段遇到的唯一问题。您可以发布您试图在事件中检索值的位置的代码吗?I get error object reference未设置为对象的实例下拉列表中有哪些字段?HotelID作为DataValueField,RoomID作为DataTextField?那么为了测试,您是否在DropDownList的OnSelectedIndexChange事件上运行代码?您应该能够从事件的发送方访问DropDownList属性。是的,我正在DropDownList的selectedindexchange事件中运行代码。这似乎是您在此阶段遇到的唯一问题。您可以发布您试图在事件中检索值的位置的代码吗?@John My HotelRoom ID是HotelRoom表的主键和ID,但因为它是My hotelID,是数据字段值,而不是hotel Room ID,我无法在gridview中显示任何内容,因为没有roomtype来代替复合主键,您可以添加单个id字段作为新的主键,并将其用于值?这取决于用户先选择酒店,然后在下拉列表中填入该酒店的房间。我不明白你的意思,我已经尝试了一个dropdownlist用于酒店列表,另一个dropdownlist用于房间列表,但是当我选择roomtype时,GridViews没有发生任何变化。据我所知,没有唯一标识房间的id。唯一id来自HotelID和RoomID的组合?这意味着您无法从下拉列表加载房间数据,因为下拉列表只有一个值字段。您需要做的是将另一个字段添加到房间表中,这是一个标识字段,并将其作为主键。然后将其用作下拉列表中的值字段。所以你有一个酒店列表,当你选择的时候