主题:GridView数据筛选不用重新绑定数据的问题
我使用GridView绑定数据之后,想要进行搜索,按文章标题来进行,但是总是搜不出数据,出现的都是一片空白,不知道问题出现在哪里
下面是页面后台代码
public partial class _page_opPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
binding();
}
private void binding() //绑定数据的方法
{
operation op = new operation();
DataSet ds = new DataSet();
ds = op.SelectPostInfo();
this.list.DataSource = ds.Tables[0].DefaultView;
this.list.DataBind();
}
//编辑数据的事件
protected void list_RowEditing(object sender, GridViewEditEventArgs e)
{
this.list.EditIndex = e.NewEditIndex;
binding();
}
//取消的事件
protected void list_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.list.EditIndex = -1;
binding();
}
//编辑数据的事件
protected void list_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
operation op = new operation();
function func = new function();
int id = Convert.ToInt32(this.list.DataKeys[e.RowIndex].Value);
int categoryid = Convert.ToInt32(((TextBox)this.list.Rows[e.RowIndex].Cells[1].FindControl("tb_categoryid")).Text.ToString());
string title = ((TextBox)this.list.Rows[e.RowIndex].Cells[2].FindControl("tb_title")).Text.ToString();
string content = ((TextBox)this.list.Rows[e.RowIndex].Cells[3].FindControl("tb_content")).Text.ToString();
op.UpdatePostInfo(id, categoryid, title, func.UBBtoHTML(content));
this.list.EditIndex = -1;
binding();
}
//分页
protected void list_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.list.PageIndex = e.NewPageIndex;
binding();
}
//删除
protected void list_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
operation op = new operation();
int id = Convert.ToInt32(this.list.DataKeys[e.RowIndex].Value.ToString());
op.DelPostInfo(id);
this.list.EditIndex = -1;
binding();
}
//搜索功能
protected void btn_search_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.list.Rows.Count; i++)
{
if (((Label)this.list.Rows[i].Cells[2].FindControl("lb_title")).Text.ToString() != this.tb_search.Text.Trim())
this.list.Rows[i].Visible = false;
else
this.list.Rows[i].Visible = true;
}
}
}
下面是前台代码:
<%@ Page Language="C#" MasterPageFile="~/_page/bgMasterPage.master" AutoEventWireup="true" CodeFile="opPost.aspx.cs" Inherits="_page_opPost" Title="Lisen-Background" ValidateRequest="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="search">
<asp:TextBox ID="tb_search" runat="server" Text="按标题搜索" CssClass="tb_search f_14px f_art_black"></asp:TextBox><asp:Button ID="btn_search" runat="server" Text="搜" CssClass="btn_search f_white f_12px" OnClick="btn_search_Click" />
</div>
<p class="f_12px f_red">*categoryid提示:1-表示记事簿;2-表示我的小说;3-表示资料架;</p>
<asp:GridView ID="list" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" CssClass="gridview" DataKeyNames="id" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="list_RowCancelingEdit" OnRowEditing="list_RowEditing" OnRowUpdating="list_RowUpdating" OnPageIndexChanging="list_PageIndexChanging" AllowSorting="True" OnRowDeleting="list_RowDeleting" PageSize="40">
<PagerSettings Position="TopAndBottom" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True">
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderText="分类ID">
<EditItemTemplate>
<asp:TextBox ID="tb_categoryid" runat="server" Text='<%# Eval("categoryid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Font-Size="12px" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:Label ID="lb_categoryid" runat="server" Text='<%# Bind("categoryid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="标题">
<EditItemTemplate>
<asp:TextBox ID="tb_title" runat="server" Text='<%# Eval("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Font-Size="12px" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:Label ID="lb_title" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="内容">
<EditItemTemplate>
<asp:TextBox ID="tb_content" runat="server" Text='<%# Eval("content") %>' CssClass="tb_area" TextMode="MultiLine" MaxLength="8000"></asp:TextBox>
</EditItemTemplate>
<ItemStyle Font-Size="12px" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:Label ID="lb_content" runat="server" Text='<%# Bind("content") %>' CssClass="f_overflow tb_edit"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="createdate" HeaderText="发表时间" ReadOnly="True">
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="commentcount" HeaderText="评论数" ReadOnly="True">
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True">
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:CommandField>
<asp:CommandField ShowDeleteButton="True">
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Content>
各位大虾帮帮忙,谢谢啦
下面是页面后台代码
public partial class _page_opPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
binding();
}
private void binding() //绑定数据的方法
{
operation op = new operation();
DataSet ds = new DataSet();
ds = op.SelectPostInfo();
this.list.DataSource = ds.Tables[0].DefaultView;
this.list.DataBind();
}
//编辑数据的事件
protected void list_RowEditing(object sender, GridViewEditEventArgs e)
{
this.list.EditIndex = e.NewEditIndex;
binding();
}
//取消的事件
protected void list_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.list.EditIndex = -1;
binding();
}
//编辑数据的事件
protected void list_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
operation op = new operation();
function func = new function();
int id = Convert.ToInt32(this.list.DataKeys[e.RowIndex].Value);
int categoryid = Convert.ToInt32(((TextBox)this.list.Rows[e.RowIndex].Cells[1].FindControl("tb_categoryid")).Text.ToString());
string title = ((TextBox)this.list.Rows[e.RowIndex].Cells[2].FindControl("tb_title")).Text.ToString();
string content = ((TextBox)this.list.Rows[e.RowIndex].Cells[3].FindControl("tb_content")).Text.ToString();
op.UpdatePostInfo(id, categoryid, title, func.UBBtoHTML(content));
this.list.EditIndex = -1;
binding();
}
//分页
protected void list_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.list.PageIndex = e.NewPageIndex;
binding();
}
//删除
protected void list_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
operation op = new operation();
int id = Convert.ToInt32(this.list.DataKeys[e.RowIndex].Value.ToString());
op.DelPostInfo(id);
this.list.EditIndex = -1;
binding();
}
//搜索功能
protected void btn_search_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.list.Rows.Count; i++)
{
if (((Label)this.list.Rows[i].Cells[2].FindControl("lb_title")).Text.ToString() != this.tb_search.Text.Trim())
this.list.Rows[i].Visible = false;
else
this.list.Rows[i].Visible = true;
}
}
}
下面是前台代码:
<%@ Page Language="C#" MasterPageFile="~/_page/bgMasterPage.master" AutoEventWireup="true" CodeFile="opPost.aspx.cs" Inherits="_page_opPost" Title="Lisen-Background" ValidateRequest="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="search">
<asp:TextBox ID="tb_search" runat="server" Text="按标题搜索" CssClass="tb_search f_14px f_art_black"></asp:TextBox><asp:Button ID="btn_search" runat="server" Text="搜" CssClass="btn_search f_white f_12px" OnClick="btn_search_Click" />
</div>
<p class="f_12px f_red">*categoryid提示:1-表示记事簿;2-表示我的小说;3-表示资料架;</p>
<asp:GridView ID="list" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" CssClass="gridview" DataKeyNames="id" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="list_RowCancelingEdit" OnRowEditing="list_RowEditing" OnRowUpdating="list_RowUpdating" OnPageIndexChanging="list_PageIndexChanging" AllowSorting="True" OnRowDeleting="list_RowDeleting" PageSize="40">
<PagerSettings Position="TopAndBottom" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True">
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderText="分类ID">
<EditItemTemplate>
<asp:TextBox ID="tb_categoryid" runat="server" Text='<%# Eval("categoryid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Font-Size="12px" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:Label ID="lb_categoryid" runat="server" Text='<%# Bind("categoryid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="标题">
<EditItemTemplate>
<asp:TextBox ID="tb_title" runat="server" Text='<%# Eval("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Font-Size="12px" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:Label ID="lb_title" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="内容">
<EditItemTemplate>
<asp:TextBox ID="tb_content" runat="server" Text='<%# Eval("content") %>' CssClass="tb_area" TextMode="MultiLine" MaxLength="8000"></asp:TextBox>
</EditItemTemplate>
<ItemStyle Font-Size="12px" HorizontalAlign="Left" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:Label ID="lb_content" runat="server" Text='<%# Bind("content") %>' CssClass="f_overflow tb_edit"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="createdate" HeaderText="发表时间" ReadOnly="True">
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="commentcount" HeaderText="评论数" ReadOnly="True">
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True">
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:CommandField>
<asp:CommandField ShowDeleteButton="True">
<HeaderStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle Font-Size="12px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Content>
各位大虾帮帮忙,谢谢啦