标题:模糊搜索问题
只看楼主
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
垃圾的沉默
Rank: 1
等 级:新手上路
威 望:2
帖 子:511
专家分:0
注 册:2007-6-25
得分:0 

前台:加一个按钮.一个textbo
<asp:TextBox id="sqls" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="搜索" OnClick="sql_click"/>

后台查询代码块

private void sql_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);
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:58
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
jxnuwy04
Rank: 2
等 级:新手上路
威 望:4
帖 子:768
专家分:0
注 册:2006-9-15
得分:0 

你没有写翻页事件代码。


------------------不为别的,就为你,我的理想!-----------------
2007-10-24 14:36
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.264068 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved