using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text;
namespace FinUiTest.Movie
{
public partial class FrmMain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 删除选中行
btnAdd.OnClientClick = Window1.GetShowReference("./FrmMainAdd.aspx");
//btnUpdate.OnClientClick = Window2.GetShowReference("./FrmMainUpdate.aspx");
BindGrid();
}
}
#region BindGrid
private void BindGrid()
{
string sql = "select Id,Movie_Name,Movie_Director,Date_Released from Record";
// 1.设置总项数(特别注意:数据库分页一定要设置总记录数RecordCount)
Grid1.RecordCount = GetTotalCount(sql);
// 2.获取当前分页数据
DataTable table = GetPagedDataTable(sql);
//DataTable table = GetDataTable();
// 3.绑定到Grid
Grid1.DataSource = table;
Grid1.DataBind();
}
private DataTable GetPagedDataTable(string sql)
{
int pageIndex = Grid1.PageIndex;
int pageSize = Grid1.PageSize;
string sortField = Grid1.SortField;
string sortDirection = Grid1.SortDirection;
DataTable table2 = GetDataTable(sql);
DataView view2 = table2.DefaultView;
view2.Sort = String.Format("{0} {1}", sortField, sortDirection);
DataTable table = view2.ToTable();
DataTable paged = table.Clone();
int rowbegin = pageIndex * pageSize;
int rowend = (pageIndex + 1) * pageSize;
if (rowend > table.Rows.Count)
{
rowend = table.Rows.Count;
}
for (int i = rowbegin; i < rowend; i++)
{
paged.ImportRow(table.Rows);
}
return paged;
}
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
Grid1.PageIndex = e.NewPageIndex;
BindGrid();
}
private int GetTotalCount(string sql)
{
return GetDataTable2(sql).Rows.Count;
}
private DataTable GetDataTable(string sql)
{
DataTable dt = GetTable(sql);
return dt;
}
private DataTable GetDataTable2(string sql)
{
DataTable dt = GetTable(sql);
return dt;
}
static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
public static DataTable GetTable(string sql, params SqlParameter[] param)
{
SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
//conn.ConnectionString = db.Connection.ConnectionString;
//if (conn.State != ConnectionState.Open)
//{
// conn.Open();
//}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adapter.Fill(table);
conn.Close();//连接需要关闭
conn.Dispose();
return table;
}
#endregion
//static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;
protected void btnDelete_Click(object sender, EventArgs e)
{
//string sql = "Delete from Record where 1=1";
//int n = 0;
//SqlConnection conn = new SqlConnection(connStr);
//SqlCommand cmd = new SqlCommand(sql, conn);
//conn.Open();
//n = cmd.ExecuteNonQuery();
//if (n == 0)
//{
// Alert.ShowInTop("当前未选中,无法删除!");
// return;
//}
//else if (n > 0)
//{
// Alert.ShowInTop("删除成功!");
//}
int id = 0;
int[] selections = Grid1.SelectedRowIndexArray;
if (selections.Length==0)
{
Alert.ShowInTop("当前未选中,无法删除!");
}
foreach (int rowIndex in selections)
{
id = Convert.ToInt32(Grid1.Rows[rowIndex].Values[1]);
}
string sql = "Delete from Record where Id="+id+"";
int n = 0;
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
n = cmd.ExecuteNonQuery();
if (n == 0)
{
Alert.ShowInTop("当前未选中,无法删除!");
return;
}
else if (n > 0)
{
Alert.ShowInTop("删除成功!");
BindGrid();
}
conn.Close();
}
//public void Refresh(bool isAdded = false)
//{
// string sql = "select Id,Movie_Name,Movie_Director,Date_Released from Record";
// SqlConnection conn = new SqlConnection(connStr);
// SqlCommand cmd = new SqlCommand(sql, conn);
// DataTable dt = new DataTable();
// SqlDataAdapter sda = new SqlDataAdapter(cmd);
// sda.Fill(dt);
// Grid1.DataSource = dt;
//}
protected void btnSelect_Click(object sender, EventArgs e)
{
string textSelect = textselect.Text;
string where = "";
if (textSelect != "" && textSelect != null)
{
where += " and (Movie_Name like '%" + textSelect + "%' or Movie_Director like '%" + textSelect + "%')";
}
string sql = "select * from Record where 1=1 "+where;
// 1.设置总项数(特别注意:数据库分页一定要设置总记录数RecordCount)
// 2.获取当前分页数据
DataTable table = GetPagedDataTable(sql);
//DataTable table = GetDataTable();
Grid1.RecordCount = GetTotalCount(sql);
// 3.绑定到Grid
Grid1.DataSource = table;
Grid1.DataBind();
}
}
} |