日期:2010-06-25  浏览次数:20359 次

在一个页面中要实现数据全部的显示和搜索.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace NetBarView.AdminManage
{
 /// <summary>
 /// showlog 的摘要说明。
 /// </summary>
 public class showlog : System.Web.UI.Page
 {
  private string conStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  protected System.Web.UI.WebControls.TextBox tbSearch;
  protected System.Web.UI.WebControls.Button btnSearch;
  protected System.Web.UI.WebControls.DataGrid dg;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.IsPostBack)
   {
    BindToDG();
   }
  }
  private void BindToDG()
  {
   dg.DataSource = SqlHelper.ExecuteDataset(conStr,CommandType.StoredProcedure,"mediaLogList");
   dg.DataKeyField = "id";
   dg.DataBind();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
   this.dg.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dg_PageIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void dg_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   dg.CurrentPageIndex = e.NewPageIndex;
   if(this.tbSearch.Text == "")
   {
    BindToDG();
   }
   else
   {
    SearchBind();
   }
  }
  private void SearchBind()
  {
//   string sql = "select id,(select barname from baruser where id=barid)as barname,medianame,addtime from medialog where barname=@barname order by id desc";
   SqlParameter pm = new SqlParameter("@barname",SqlDbType.VarChar,50);
   pm.Value = this.tbSearch.Text;
   dg.DataSource = SqlHelper.ExecuteDataset(conStr,CommandType.StoredProcedure,"mediaLogSearch",pm);
   dg.CurrentPageIndex = 0;
   dg.DataKeyField = "id";
   dg.DataBind();
  }

  private void btnSearch_Click(object sender, System.EventArgs e)
  {
//   SearchBind();
  }
 }
}

<%@ Page language="c#" Codebehind="showlog.aspx.cs" AutoEventWireup="false" Inherits="NetBarView.AdminManage.showlog" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >