所以我希望有一个DDL(SELECT name FROM sys.databases(nolock)),它会在选中后返回一个值(可能在gridview中显示)。
我遇到的问题是查询需要根据用户从DDL中选择的内容建立与不同数据库的连接,并返回gridview中显示的值。我想要类似下面的查询:
我怎么能做到这一点?
use ([name]=@name) select sum(t.table1.column1) as Total from database inner join database2 on db1.table1.id= ([name]=@name).table2.id where([appname]=@appname) and Date <= GetDate()AND YEAR(Date) = year(GetDate())
到目前为止我得到了这个(不确定这是否是最好的方法。)这是我的代码,如何使用依赖于DDL的gridview显示结果?
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
{
if (DropDownList2.SelectedValue == "test1")
{
SqlDataSource3.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString;
{
SqlDataSource3.SelectCommandType = SqlDataSourceCommandType.Text;
SqlDataSource3.SelectCommand = "select top 10 * from table1 (nolock)";
}
}
else if (DropDownList2.SelectedValue == "test2")
{
SqlDataSource4.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test2ConnectionString"].ConnectionString;
{
SqlDataSource4.SelectCommandType = SqlDataSourceCommandType.Text;
SqlDataSource4.SelectCommand = "select top 1 * from table1 (nolock)";
}
}
以下是我的DDL的aspx代码。
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem>test1</asp:ListItem>
<asp:ListItem>test2</asp:ListItem>
</asp:DropDownList>
答案 0 :(得分:0)
据我所知,当用户从DropDownList中选择一个项目时,您希望运行不同的SQL。无论如何,您应该将代码放在控件的SelectedIndexChanged事件中。例如,您的标记可能如下所示:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
在您的代码隐藏中:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// call your sql
}
修改强>
我已经添加了一个链接来启动。它们使用SelectedIndexChanged事件显示一些代码。
http://www.devasp.net/net/articles/display/1294.html
http://www.c-sharpcorner.com/UploadFile/092589/working-with-dropdownlist-slectedindexchanged-event/