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.OleDb;
namespace sitemapdemo
{
/// <summary>
/// 2011-03-24
/// 涂聚文
/// </summary>
public partial class FormViewDemo : System.Web.UI.Page
{
string GalleryID = string.Empty;
string GalleryName = string.Empty;
string Picture = string.Empty;
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "FormView 添加,删除,修改,分页";
if (!Page.IsPostBack)
{
BindData();
}
}
/// <summary>
/// 绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void myFormView_DataBound(object sender, EventArgs e)
{
//*** Image ***//
Image Image1 = (Image)(myFormView.FindControl("Image1"));
if (Image1 != null)
{
Image1.ImageUrl = "images/" + (string)DataBinder.Eval(myFormView.DataItem, "Picture");
Image1.Attributes.Add("OnClick", "window.open('images/" + (string)DataBinder.Eval(myFormView.DataItem, "Picture") + "')");
Image1.Style.Add("cursor", "hand");
Image1.ToolTip = (string)DataBinder.Eval(myFormView.DataItem, "GalleryName");
}
//*** GalleryName ***//
Label lblGalleryName = (Label)(myFormView.FindControl("lblGalleryName"));
if (lblGalleryName != null)
{
lblGalleryName.Text = (string)DataBinder.Eval(myFormView.DataItem, "GalleryName");
}
//Label lblGalleryID = (Label)(myFormView.FindControl("lblGalleryID"));
//if (lblGalleryID != null)
//{
// lblGalleryID.Text = (string)DataBinder.Eval(myFormView.DataItem, "GalleryID");
//}
}
/// <summary>
/// 显示分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ShowPageCommand(object sender, FormViewPageEventArgs e)
{
myFormView.PageIndex = e.NewPageIndex;
BindData();
}
/// <summary>
///
/// </summary>
void BindData()
{
DataTable dt;
//*** DataSet To DataTable ***//
dt = CreateDsToDt();
//*** DataTable ***//
//dt = CreateDataTable();
//*** DataSet ***//
//DataSet ds;
//ds = CreateDataSet();
//dt = ds.Tables[0]; //*** Convert DataSet to DataTable ***//
//*** TableRows ***//
//dt = DataTableRows();
//*** BindData to FormView ***//
myFormView.DataSource = dt;
myFormView.DataBind();
}
/// <summary>
/// //*** DataTable ***//
/// </summary>
/// <returns></returns>
protected DataTable CreateDataTable()
{
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbDataAdapter dtAdapter = new System.Data.OleDb.OleDbDataAdapter();
DataTable dt = new DataTable();
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("~/App_Data/db1.mdb") + ";Jet OLEDB:Database Password=;";
objConn = new System.Data.OleDb.OleDbConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM gallery";
dtAdapter = new System.Data.OleDb.OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
return dt; //*** Return DataTable ***//
}
/// <summary>
/// //*** DataSet ***//
/// </summary>
/// <returns></returns>
protected DataSet CreateDataSet()
{
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand();
System.Data.OleDb.OleDbDataAdapter dtAdapter = new System.Data.OleDb.OleDbDataAdapter();
DataSet ds = new DataSet();
String strConnString, strSQL;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("~/App_Data/db1.mdb") + ";Jet OLEDB:Database Password=;";
strSQL = "SELECT * FROM gallery ";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dtAdapter = null;
objConn.Close();
objConn = null;
return ds; //*** Return DataSet ***//
}
/// <summary>
/// //*** DataSet to DataTable ***//
/// </summary>
/// <returns></returns>
protected DataTable CreateDsToDt()
{
//ViewState["tmp"] = tmp; //创建数据池
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand();
System.Data.OleDb.OleDbDataAdapter dtAdapter = new System.Data.OleDb.OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt;
String strConnString, strSQL;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("~/App_Data/db1.mdb") + ";Jet OLEDB:Database Password=;";
strSQL = "SELECT * FROM gallery";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dt = ds.Tables[0];
dtAdapter = null;
objConn.Close();
objConn = null;
return dt; //*** Return DataTable ***//
}
/// <summary>
///*** TableRows (DataTable) ***
/// </summary>
/// <returns></returns>
protected DataTable DataTableRows()
{
DataTable dt = new DataTable();
DataRow dr;
//*** Column ***//
dt.Columns.Add("GalleryID");
dt.Columns.Add("GalleryName");
dt.Columns.Add("Picture");
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "1";
dr["GalleryName"] = "My Picture 1";
dr["Picture"] = "1.jpg";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "2";
dr["GalleryName"] = "My Picture 2";
dr["Picture"] = "2.jpg";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "3";
dr["GalleryName"] = "My Picture 3";
dr["Picture"] = "1.jpg";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "4";
dr["GalleryName"] = "My Picture 4";
dr["Picture"] = "2.jpg";
dt.Rows.Add(dr);
return dt; //*** Return DataTable ***//
}
/// <summary>
/// 选择操作状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void myFormView_ModeChanging(object sender, FormViewModeEventArgs e)
{
//判断模式
if (e.NewMode == FormViewMode.Insert)
{
myFormView.ChangeMode(FormViewMode.Insert);
}
else if (e.NewMode == FormViewMode.Edit)
{
myFormView.ChangeMode(FormViewMode.Edit);
}
else if (e.CancelingEdit)
{
myFormView.ChangeMode(FormViewMode.ReadOnly);
}
BindData();
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void myFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
GalleryID = ((Label)this.myFormView.FindControl("Label1")).Text;
GalleryName = ((TextBox)this.myFormView.FindControl("EditGalleryNameTextBox")).Text.Trim();
Picture = ((TextBox)this.myFormView.FindControl("EditPictureTextBox")).Text.Trim();
Response.Write(" " + GalleryID + " " + GalleryName + " " + Picture);
//Label lbl_no = (Label)this.FormView1.FindControl("Label1");
//TextBox TextBox1 = (TextBox)this.FormView1.FindControl("TextBox1");
//TextBox TextBox2 = (TextBox)this.FormView1.FindControl("TextBox2");
//TextBox TextBox3 = (TextBox)this.FormView1.FindControl("TextBox3");
//TextBox TextBox4 = (TextBox)this.FormView1.FindControl("TextBox4");
//DataTable temp = BindData();
//DataRow dr = temp.Select("id=" + lbl_no.Text + " and mid=" + ViewState["DataKey"].ToString())[0];
//dr["nr"] = TextBox1.Text;
//dr["czr"] = TextBox2.Text;
//dr["cs"] = TextBox3.Text;
//dr["rq"] = TextBox4.Text;
//ViewState["temp"] = temp;//覆盖数据池
//myFormView.ChangeMode(FormViewMode.ReadOnly);
//BindData();
}
/// <summary>
/// 添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void myFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
GalleryName = ((TextBox)this.myFormView.FindControl("InsertGalleryNameTextBox")).Text.Trim();
Picture = ((TextBox)this.myFormView.FindControl("InsertPictureTextBox")).Text.Trim();
Response.Write(" " + GalleryID + " " + GalleryName + " " + Picture);
//myFormView.ChangeMode(FormViewMode.ReadOnly);
//BindData();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void myFormView_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
GalleryID = ((Label)this.myFormView.FindControl("lblGalleryID")).Text;
Response.Write(" " + GalleryID + " " + GalleryName + " " + Picture);
//myFormView.ChangeMode(FormViewMode.ReadOnly);
//BindData();
}
}
}
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)