SQLDataSource绑定条件网格 [英] Sqldatasource bind grid with condition
本文介绍了SQLDataSource绑定条件网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Sqldatasource
我想根据条件运行查询
假设
I am using Sqldatasource
I want to run query according to condition
suppose
I session("userid")="1"
{
run this to query to bind grid
}
else
{
this Query to bind grid
}
推荐答案
那么,问题出在哪里?
可以有多种方式...让我们选择其中一种:
So, what''s the problem in it?
There can be multiple ways... lets take one of them:
if(Session["userid"] == "1")
{
myDataGrid.DataSource = PopulateDataSetUsingQuery1();
}
else
{
myDataGrid.DataSource = PopulateDataSetUsingQuery2();
}
//where as the two methods PopulateDataSetUsingQuery1 & PopulateDataSetUsingQuery2 will fetch the records based on Query1 & Query2
您也可以在Page_Load事件中尝试类似的操作:
You may also try something like that in Page_Load event:
If condition
{
SqlDataSource1.SelectCommand = "select * from TableA";
}
else
{
SqlDataSource1.SelectCommand = "select * from TableB";
}
或者,也许您可以使用数据源中的参数来获得结果:
Or maybe you can get the result using parameters in your datasource:
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%
ConnectionStrings:MyNorthwind%>" selectcommand="SELECT * FROM Table WHERE userid = @userid"> <selectparameters> <asp:parameter name="userid" type="int32" /> </selectparameters> </asp:sqldatasource> private void Page_Load(object sender, System.EventArgs e) { If condition { SqlDataSource1.SelectParameters["userid"].DefaultValue = 123; } else { SqlDataSource1.SelectParameters["userid"].DefaultValue = 456; } }
ConnectionStrings:MyNorthwind%>" selectcommand="SELECT * FROM Table WHERE userid = @userid"> <selectparameters> <asp:parameter name="userid" type="int32" /> </selectparameters> </asp:sqldatasource> private void Page_Load(object sender, System.EventArgs e) { If condition { SqlDataSource1.SelectParameters["userid"].DefaultValue = 123; } else { SqlDataSource1.SelectParameters["userid"].DefaultValue = 456; } }
希望对您有用.
-------------------------------------------------- ---------------
我的网站:瓜鲁鲁斯的威尼斯公寓
I hope it is useful.
-----------------------------------------------------------------
My site: Apartamentos a venda em Guarulhos
这篇关于SQLDataSource绑定条件网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文