我有这段代码,我正在尝试从下拉框中执行sql查询。
<html>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="a" DataValueField="a">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString7 %>"
SelectCommand="SELECT DISTINCT [a] FROM [Table_1]"></asp:SqlDataSource>
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource2" DataTextField="b" DataValueField="b">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString8 %>"
SelectCommand="SELECT DISTINCT [b] FROM [Table_1]"></asp:SqlDataSource>
<br />
<br />
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource3" DataTextField="c" DataValueField="c">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString9 %>"
SelectCommand="SELECT DISTINCT [c] FROM [Table_1]"></asp:SqlDataSource>
<br />
<br />
<br />
<asp:DropDownList ID="DropDownList4" runat="server"
DataSourceID="SqlDataSource4" DataTextField="d" DataValueField="d">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString10 %>"
SelectCommand="SELECT DISTINCT [d] FROM [Table_1]"></asp:SqlDataSource>
<br />
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource5">
<Columns>
<asp:BoundField DataField="a" HeaderText="a" SortExpression="a" />
<asp:BoundField DataField="b" HeaderText="b" SortExpression="b" />
<asp:BoundField DataField="c" HeaderText="c" SortExpression="c" />
<asp:BoundField DataField="d" HeaderText="d" SortExpression="d" />
<asp:BoundField DataField="e" HeaderText="e" SortExpression="e" />
<asp:BoundField DataField="f" HeaderText="f" SortExpression="f" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_firstConnectionString11 %>"
SelectCommand="SELECT [a], [b], [c], [d], [e], [f] FROM [Table_1] WHERE (([a] = @a) AND ([b] = @b) AND ([c] = @c) AND ([d] = @d))">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="a"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownList2" Name="b"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownList3" Name="c"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownList4" Name="d"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
我有一个小的SQL表,我希望在从下拉框中选择值时,它应该在该表中显示结果。我无法做到这一点。有什么想法吗?
答案 0 :(得分:1)
你的逻辑听起来很合理。
如果您希望在更改每个DropDownList
的值后立即反映更改,则需要将AutoPostBack="True"
属性添加到 每个 > 即可。举个例子,这就是第一个看起来的样子:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="a" DataValueField="a">
</asp:DropDownList>
我能看到的另一件事会导致您的表格无法填写,因为GridView
的SQL查询过于严格。也许您打算在OR
条款中使用AND
而不是WHERE
?
编辑:还有一个想法,可能是您的GridView
在所有DropDownList
被数据绑定之前被数据绑定 。这会导致他们还没有SelectedValues,所以当ControlParameter
获得数据绑定时,GridView
s将没有值。您需要找到一种等待所有DropdownLists
数据绑定的方法,然后使用此代码重新绑定GridView
:
SqlDataSource5.DataBind();
GridView1.DataBind();
提示:要检查并确保这是您的问题,您可以将该代码放入每个的 _DataBound
事件中你的 DropDownList
。效率不高,但应该有效。