标题:模糊搜索问题
取消只看楼主
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
 问题点数:0 回复次数:6 
模糊搜索问题
模糊搜索的结果怎么赋值给gridview ?
最好能把前台的代码,和程序的代码完整的讲下
在此先谢谢了。
搜索更多相关主题的帖子: 搜索 模糊 赋值 gridview 前台 
2007-10-20 10:40
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
得分:0 
能说的详细点吗?谢谢了

问题我在补充下,搜索页面和搜索结果页面是在同一个页面上
并且没有输入搜索数据的话,页面默认显示的是所有数据
输入搜索数据的话,就在同一个页面上显示搜索结果。

比如下面asp的效果:
set rs=server.CreateObject("ADODB.RecordSet")
sql="select * from user where name like '%"&request("name")&"%' order by id desc"
rs.open sql,conn,1,1

2007-10-24 08:51
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
得分:0 
类似asp
set rs=server.CreateObject("ADODB.RecordSet")
sql="select * from user where name like '%"&request("name")&"%' order by id desc"
rs.open sql,conn,1,1
这样的效果
没有传值的时候,显示所有数据
有传值的时候,根据传值显示数据
搜索输入框,列表显示,搜索结果显示,全部在一个页面,asp只需要上面的一个查询就可以全部实现
在.net中需要怎么做?

书上说把搜索结果重新绑定给 gridview控件
但是我总是绑定不上,要么和默认显示的冲突,要么就是出错。

请求帮助。

2007-10-24 10:59
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
得分:0 

前台代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
CellPadding="3" DataSourceID="SqlDataSource1" PageSize="30" Width="100%" BackColor="#F1F9FE"
CellSpacing="2" GridLines="None" AutoGenerateColumns="False" DataKeyNames="articleid">
<PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NumericFirstLast" PageButtonCount="20" />
<PagerStyle HorizontalAlign="Center" />
<HeaderStyle BackColor="#DBEEFB" Wrap="False" />
<AlternatingRowStyle BackColor="#DBEEFB" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="articleid" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="articleid">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="文章标题" SortExpression="title">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="作者" SortExpression="name">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="dateandtime" HeaderText="发布日期" SortExpression="dateandtime">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="typeid" HeaderText="分类" SortExpression="typeid">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:CommandField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:loveconn %>"
DeleteCommand="DELETE FROM [learning] WHERE [articleid] = @articleid" InsertCommand="INSERT INTO [learning] ([user_id], [name], [title], [content], [hits], [dateandtime], [images], [typeid], [review], [reviewcount], [fenshu], [persons]) VALUES (@user_id, @name, @title, @content, @hits, @dateandtime, @images, @typeid, @review, @reviewcount, @fenshu, @persons)"
SelectCommand="SELECT * FROM [learning] ORDER BY [articleid] DESC" UpdateCommand="UPDATE [learning] SET [user_id] = @user_id, [name] = @name, [title] = @title, [content] = @content, [hits] = @hits, [dateandtime] = @dateandtime, [images] = @images, [typeid] = @typeid, [review] = @review, [reviewcount] = @reviewcount, [fenshu] = @fenshu, [persons] = @persons WHERE [articleid] = @articleid">
<DeleteParameters>
<asp:Parameter Name="articleid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="content" Type="String" />
<asp:Parameter Name="hits" Type="Int32" />
<asp:Parameter Name="dateandtime" Type="DateTime" />
<asp:Parameter Name="images" Type="String" />
<asp:Parameter Name="typeid" Type="Int32" />
<asp:Parameter Name="review" Type="String" />
<asp:Parameter Name="reviewcount" Type="Int32" />
<asp:Parameter Name="fenshu" Type="Single" />
<asp:Parameter Name="persons" Type="Int32" />
<asp:Parameter Name="articleid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="content" Type="String" />
<asp:Parameter Name="hits" Type="Int32" />
<asp:Parameter Name="dateandtime" Type="DateTime" />
<asp:Parameter Name="images" Type="String" />
<asp:Parameter Name="typeid" Type="Int32" />
<asp:Parameter Name="review" Type="String" />
<asp:Parameter Name="reviewcount" Type="Int32" />
<asp:Parameter Name="fenshu" Type="Single" />
<asp:Parameter Name="persons" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>


后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class huiyuan_zhiding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
private void GridViewBind()
{
string sqlstr = "";
if (name.Text.Trim() == "")
{
sqlstr = "select * from learning order by articleid desc";
}
else
{
sqlstr = "select * from learning where name like '" + name.Text.Trim() + "' order by articleid desc";
}
string connstr = ConfigurationManager.ConnectionStrings["loveconn"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstr);
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open")
conn.Close();
}
}
}


现在这个是没有任何错误提示,就是搜索不管用。


2007-10-24 13:43
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
得分:0 

搜索解决了,谢谢

还有一点问题,就是默认的页面是空的,只有点击搜索之后,才会出来数据。
怎么打开这个页面的时候,默认的就显示数据。

还有就是搜索数据为空,显示出来的数据,点击翻页的时候,提示:

GridView“GridView1”激发了未处理的事件“PageIndexChanging”。


2007-10-24 14:20
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
得分:0 
后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
string sqlstr = "";
if (name.Text.Trim() == "")
{
sqlstr = "select * from learning order by articleid desc";
}
else
{
sqlstr = "select * from learning where name like '" + name.Text.Trim() + "' order by articleid desc";
}
string connstr = ConfigurationManager.ConnectionStrings["loveconn"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstr);
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
if (conn.State.ToString() == "Open")
conn.Close();
}


前台代码:
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td style="text-align: center;">
作者:<asp:TextBox ID="name" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="Button1"
runat="server" Text="搜索" OnClick="Button1_Click" /></td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
CellPadding="3" PageSize="30" Width="100%" BackColor="#F1F9FE"
CellSpacing="2" GridLines="None" AutoGenerateColumns="False" DataKeyNames="articleid">
<PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NumericFirstLast" PageButtonCount="20" />
<PagerStyle HorizontalAlign="Center" />
<HeaderStyle BackColor="#DBEEFB" Wrap="False" />
<AlternatingRowStyle BackColor="#DBEEFB" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="articleid" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="articleid">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="文章标题" SortExpression="title">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="作者" SortExpression="name">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="dateandtime" HeaderText="发布日期" SortExpression="dateandtime">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="typeid" HeaderText="分类" SortExpression="typeid">
<ItemStyle Wrap="False" />
<HeaderStyle Wrap="False" />
</asp:BoundField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:CommandField>
</Columns>
</asp:GridView>

2007-10-24 14:25
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
得分:0 
默认显示问题我已经弄好了

现在只有一个问题了
没加搜索时候正常,加了搜索代码
string sqlstr = "";
if (name.Text.Trim() == "")
{
sqlstr = "select * from learning order by articleid desc";
}
else
{
sqlstr = "select * from learning where name like '" + name.Text.Trim() + "' order by articleid desc";
}
string connstr = ConfigurationManager.ConnectionStrings["loveconn"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connstr);
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
if (conn.State.ToString() == "Open")
conn.Close();
这个加了之后,提示:
异常详细信息: System.InvalidOperationException: “GridView1”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。


在前台里面,
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
CellPadding="3" DataSourceID="SqlDataSource1" PageSize="30" Width="100%" BackColor="#F1F9FE"
CellSpacing="2" GridLines="None" AutoGenerateColumns="False" DataKeyNames="articleid">

DataSourceID="SqlDataSource1"删除之后,就不能翻页了

请问怎么改下?

2007-10-24 14:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-179240-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.325006 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved