在TextBox中输入值以后,点Button,然后在GridView里面列出包含输入关键字的项目,怎么做呢?
解决方案 »
- 急,读取Excel表格数据丢失的问题
- Asp.net分页
- 数据库中的数据导到Excel的问题
- 我的ASP。NET不能调试了,我的环镜是WIN2000SERVER,VS。NET2003
- 我是新手.有一個有關前端后端的問題.請教大家!幫忙一下
- 见鬼了,服务器控件的样式居然会无故丢失,200分相赠
- js关闭浏览器自动清除cookie
- 请问如何根据Text取得dropdownlist的某一项?
- 请教图表插件Anychart和fusioncharts的选择问题
- 请问自己输出的控件如何才能在后台代码中引用POST值?
- 生成解决方案和生成网站以后为什么bin目录下没有dll文件呢?
- 从 DateTime 到 Int32 的转换无效..
查询后得到的dataset绑定到gridview的datasource就可以啦
不过先要把gridview的datasourceID置为null,因为datasource和datasourceID不能同时可用
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Test;User ID=sa;pwd=sa"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Student] where Name like '%'+@Name+'%'">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="Name" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form> protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.DataBind();
}
首先感谢您回答我的问题,我按照您的做法做了,在测试查询里面能查到我需要的数据了,但是我运行后,在TextBox里输入了要查询的字符,点确定按钮,就不能实现,请问是哪儿没设置对吗?
{
sql = "select ID,city_cname,city_ename,city_code from PD_City order by city_cname desc";
if (!Page.IsPostBack)
{
GridviewBind(sql);
}
}private void GridviewBind(string sql)
{
conn.Open();
SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
myda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}protected void btnFind_Click(object sender, EventArgs e)
{
sql = "select ID,city_cname,city_ename,city_code from PD_City where city_cname like '%" + tbSearch.Text + "%'";
GridviewBind(sql);
}
{
this.GridView1.DataBind();
}我绑定了按钮事件的,还是没对呢
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseManageDBConnectionString %>" SelectCommand="SELECT * FROM [AnJianTable] where AJName like '%'+@AJName+'%'">
<SelectParameters>
<asp:Parameter Name="AJName" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
</div>
</form>//按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.DataBind(); }在TextBox里面输入了东西,但是就是查不出来,不能在GridView里面显示。
感觉就是Textbox里面的东西发不回去,查询不了
/// 模糊查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
string sqlSelect = "select * from t_users ,t_myinfo where u_username like '%" + txtUserName.Text + "%' or i_name like'%" + txtUserName.Text + "%'";
SQLTool tool = new SQLTool();
this.gvUserManager.DataSource = tool.dataTable(sqlSelect);
this.gvUserManager.DataBind();
}
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CaseManageDBConnectionString %>"
SelectCommand="SELECT * FROM [AnJianTable] WHERE ([AJName] LIKE '%' + @AJName + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="1" Name="AJName" PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
//按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.DataBind();
}
/// <summary>
/// 模糊查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
string sqlSelect = "select * from t_users ,t_myinfo where u_username like '%" + txtUserName.Text + "%' or i_name like'%" + txtUserName.Text + "%'";
SQLTool tool = new SQLTool();
this.gvUserManager.DataSource = tool.dataTable(sqlSelect);
this.gvUserManager.DataBind();
}
正解