C# 在运行时更改 sqldatasource 的选择命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9854296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change select command of sqldatasource at runtime
提问by user1263390
HTML
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:database1ConnectionString %>"
SelectCommand="SELECT * from tblCourse"></asp:SqlDataSource>
Code
代码
SqlDataSource1.SelectCommand =
"SELECT * from tblCourse where name='"+textbox1.text+"'";
SqlDataSource1.DataBind();
But Gridview does not change based on the new select command, even when I'm using DataBind()
但是 Gridview 不会根据新的 select 命令而改变,即使我使用的是 DataBind()
How can we change grid view base on select command of sql data source?
我们如何根据sql数据源的select命令更改网格视图?
回答by shriek
You can either edit your BoundField's or change the property AutoGenerateColumnsto true.
您可以编辑您的 BoundField 或将属性更改AutoGenerateColumns为true.
回答by PraveenVenu
string strSql= "SELECT * from tblCourse where name='abc'";
ViewState["SQL"]=strSql;
SqlDataSource1.SelectCommand =strSql;
SqlDataSource1.DataBind();
Now in the Page_Load
现在在 Page_Load
if(IsPostback)
SqlDataSource1.SelectCommand=ViewState["SQL"].ToString();
回答by Jacque
Set autogenerate to true and be sure to remove all the fields in the edit fields wizard. If you change the select command it causes the conflict.
将 autogenerate 设置为 true 并确保删除编辑字段向导中的所有字段。如果您更改选择命令,则会导致冲突。
回答by geo
Try adding the following :
尝试添加以下内容:
SqlDataSource.select(DataSourceSelectArguments.Empty);
before the line SqlDataSource.DataBind();
行前 SqlDataSource.DataBind();
回答by R.C
This is happening because of GridView's viewstate.
这是由于GridView的视图状态而发生的。
When postback happens, gridview stores its data from ViewState. So, You can either turn off view state for GridView ( a good practice ?? ) OR you call GridView.DataBind()in addition to SqlDataSource.Databind();
当回发发生时,gridview 存储来自 ViewState 的数据。因此,您可以关闭 GridView 的视图状态(一个很好的做法??)或者GridView.DataBind()除了调用SqlDataSource.Databind();
METHOD 1: Calling GridView.DataBind();
方法一:打电话GridView.DataBind();
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
string command = SqlDataSource1.SelectCommand; // added just for debug purpose
SqlDataSource1.SelectCommand = "SELECT * from tblCourse where
name='"+textbox1.text+"'";
SqlDataSource1.DataBind();
gridview1.DataBind();
}
}
METHOD 2: Turn off View State for GridView ( Is this a good Practice?). When you set this false, there is NO need to call GridView.DataBind()in your page_Loadas seen in above METHOD 1.
方法 2:关闭 GridView 的视图状态(这是一个好的做法吗?)。当你设置这个时false,没有必要 像上面方法1中看到GridView.DataBind()的page_Load那样调用你的。
<asp:GridView runat="server" ID="gridview1" EnableViewState="false" ... />
Now the part comes that should must be taken care of::
现在应该注意的部分来了:
Make sure the <asp:BoundField>or in general any fields declared and bound to GridViewmarkup are also present in your new query else an error will be thrown saying something similar as below:
确保在您的新查询中也存在<asp:BoundField>声明并绑定到GridView标记的任何字段,否则将抛出一个错误,内容类似于以下内容:
A field or property with the name 'ID' was not found on the selected data source

