[英]Execute a SQL SELECT query and display the results in a GridView on button click
上下文:我正在设计一个简单的ASP.NET网页,其中有一个搜索框,用户可以在其中输入用户ID(int),并且当他们单击搜索按钮时,GridView控件将显示一个SQL SELECT查询的结果,该查询将检查如果存在具有指定用户ID的条目。 此功能都可以使用,我相信这非常简单。
我添加了另一个按钮,我想执行一个不同的SQL SELECT查询,该查询返回表中的所有条目。
这是第一个SELECT查询,它是GridView使用的SqlDataSource的SelectCommand: SELECT * FROM [tblUser] WHERE [UserID] = @UserID
这是用户单击新按钮时要执行的新SELECT查询: SELECT * FROM tblUser
问题:如何使用“ OnClick”事件(在后面的代码中)执行此新查询并在同一GridView中显示结果?
我尝试过的事情:这似乎是执行新语句的一种合乎逻辑的方式:更改GridView控件使用的SqlDataSource的SelectCommand,执行该SelectCommand,然后在GridView中显示该命令的结果。 不幸的是,这只会导致GridView为空,没有列标签或其他任何内容。
string selectAll = "SELECT * FROM tblUser"; //new query to execute
string oldSelect = SqlDataSource1.SelectCommand; //stores old query in temp variable
SqlDataSource1.SelectCommand = selectAll; //sets the SelectCommand of the DataSource to the new query
SqlDataSource1.Select(DataSourceSelectArguments.Empty); //executes the SelectCommand
GridView1.DataBind(); //binds the GridView to the DataSource
SqlDataSource1.SelectCommand = oldSelect; //returns the select command to its original value
问题是您忘记了在更改SelectCommand之后将DataSource和DataSourceID分配给GridView:
string selectAll = "SELECT * FROM tblUser"; //new query to execute
string oldSelect = SqlDataSource1.SelectCommand; //stores old query in temp variable
SqlDataSource1.SelectCommand = selectAll; //sets the SelectCommand of the DataSource to the new query
SqlDataSource1.Select(DataSourceSelectArguments.Empty); //executes the SelectCommand
GridView1.DataSourceID = "";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind(); //binds the GridView to the DataSource
SqlDataSource1.SelectCommand = oldSelect;
并返回到您的旧选择,在另一个按钮单击事件中:
SqlDataSource1.Select(DataSourceSelectArguments.Empty); //executes the SelectCommand
GridView1.DataSourceID = "";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
编辑
考虑到您使用实体框架,
在代码背后:
Public Shared Function getUsers(optional byval userid as integer = 0) as list(of myusers)
Using db as new your_user_entity
dim users = (from u in db.Users select u)
if userid = 0 then
return users.toList()
else
return users.where(function (x) x.userid = userid).toList()
End If
End Using
End
之后,您可以单击以下按钮来调用此功能:
gridview.datasource = getUsers() ' will give you all users
gridview.databind
在另一个按钮上:
gridview.datasource = getUsers(10) ' will only give you the user with id=10
girdview.databind
稍微改变一下逻辑。 在后面的代码中,创建两个函数
这两个函数都应返回List(of Your_user)
然后在两个按钮的按钮点击事件中,
例如btnGetSingle_Click
gridview.datasource = getSingleUserDetails(10)
gridview.databind
btnGetAllUsers_Click
gridview.datasource = getAllUsers
gridview.databind
注意:我忽略了一些事实,例如您可以使用重载函数,实体框架等。
这是我要做的:
1)更改SelectCommand之后,分配GridView的DataSource和DataSourceID。
2)在执行新的SelectCommand之前,删除搜索文本框的SelectParameter,然后在进行常规搜索之前将其重新添加。
这是“全部显示”按钮的按钮单击事件的代码:
protected void ShowAllUsers(object sender, EventArgs e)
{
UserIDTextBox.Text = "";
SqlDataSource1.SelectCommand = selectAll;
SqlDataSource1.SelectParameters.Remove(SqlDataSource1.SelectParameters["UserID"]);
GridView1.DataSourceID = "";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
SqlDataSource1.SelectCommand = selectUser;
}
然后对于我的常规搜索按钮:
if (SqlDataSource1.SelectParameters.Count == 0)
{
SqlDataSource1.SelectParameters.Add("UserID", UserIDTextBox.Text);
}
SqlDataSource1.SelectCommand = selectUser;
selectUser
和selectAll
是保存SQL查询的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.