一个DataGrid多次绑定不同数据的时候我的用法
一个DataGrid多次绑定不同数据的时候我的用法
一个DataGrid多次绑定不同数据的时候我的用法 在一个页面中要实现数据全部的显示和搜索.
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' >
<HTML>
<HEAD>
<title>showlog</title>
<meta name='GENERATOR' Content='Microsoft Visual Studio .NET 7.1'>
<meta name='CODE_LANGUAGE' Content='C#'>
<meta name='vs_defaultClientScript' content='JavaScript'>
<meta name='vs_targetSchema' content='http://schemas.microsoft.com/intellisense/ie5'>
</HEAD>
<body MS_POSITIONING='GridLayout' bgcolor='#799ae1' style='margin-left:0px'>
<form id='Form1' method='post' runat='server'>
<TABLE id='Table1' style='FONT-SIZE: 9pt; WIDTH: 600px' borderColor='#ccccff' cellSpacing='1'
cellPadding='0' width='907' border='1'>
<TR>
<TD style='HEIGHT: 25px' align='center' bgColor='#6699ff' colSpan='3'>网吧电影观看日志</TD>
</TR>
<TR>
<TD style='HEIGHT: 25px' align='center' colSpan='3'><FONT face='宋体'>网吧搜索
<asp:TextBox id='tbSearch' runat='server'></asp:TextBox>
<asp:Button id='btnSearch' runat='server' Text='搜索'></asp:Button></FONT></TD>
</TR>
<TR>
<TD style='WIDTH: 575px' vAlign='top' align='left' width='575' colSpan='2'>
<asp:DataGrid id='dg' runat='server' Width='384px' AutoGenerateColumns='False' AllowPaging='True'
PageSize='30' Font-Size='X-Small' BorderColor='#3366CC' BorderStyle='None' BorderWidth='1px'
BackColor='White' CellPadding='4'>
<FooterStyle ForeColor='#003399' BackColor='#99CCCC'></FooterStyle>
<SelectedItemStyle Font-Bold='True' ForeColor='#CCFF99' BackColor='#009999'></SelectedItemStyle>
<ItemStyle ForeColor='#003399' BackColor='White'></ItemStyle>
<HeaderStyle Font-Bold='True' ForeColor='#CCCCFF' BackColor='#003399'></HeaderStyle>
<Columns>
<asp:BoundColumn DataField='id' HeaderText='编号'>
<HeaderStyle Width='40px'></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField='barname' HeaderText='网吧名称'>
<HeaderStyle Width='120px'></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField='medianame' HeaderText='观看电影'>
<HeaderStyle Width='120px'></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField='addtime' HeaderText='观看时间' DataFormatString='{0:d}'>
<HeaderStyle Width='60px'></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign='Left' ForeColor='#003399' BackColor='#99CCCC' Mode='NumericPages'></PagerStyle>
</asp:DataGrid>
</TD>
<TD width='2'></TD>
</TR>
<TR>
<TD style='WIDTH: 1px'></TD>
<TD align='center' style='WIDTH: 291px'>
</TD>
<TD width='2'><FONT face='宋体'></FONT></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>