(C#) ASP.NET FormView Control - DataSet,DataTable,TableRows |
(C#) ASP.NET FormView Control - DataSet,DataTable,TableRows การแสดงผลใน FormView ความของสามารถของคอนโทรล FormView สามารถอ่านข้อมูลในรูปแบบของ DataReader , DataSet และ DataTable ทั้งนี้เพื่อความหลากหลายของข้อมูลที่ผู้ใช้ได้นำมาเข้าในระบบ ในตัวอย่างนี้ผมได้ยกตัวอย่างการใช้งานในรูปแบบต่าง ๆ
Language Code : VB.NET || C#
Framework : 1,2,3,4
FormViewDataSetDataTable.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
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();
}
//*** DataTable ***//
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("database/mydatabase.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 ***//
}
//*** DataSet ***//
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("database/mydatabase.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 ***//
}
//*** DataSet to DataTable ***//
protected DataTable CreateDsToDt()
{
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("database/mydatabase.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 ***//
}
//*** TableRows (DataTable) ***//
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"] = "DSC01683.jpg";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "2";
dr["GalleryName"] = "My Picture 2";
dr["Picture"] = "DSC01719.jpg";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "3";
dr["GalleryName"] = "My Picture 3";
dr["Picture"] = "DSC01805.jpg";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["GalleryID"] = "4";
dr["GalleryName"] = "My Picture 4";
dr["Picture"] = "DSC01806.jpg";
dt.Rows.Add(dr);
return dt; //*** Return DataTable ***//
}
void myFormView_DataBound(Object sender, System.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");
}
}
void ShowPageCommand(Object sender , FormViewPageEventArgs e)
{
myFormView.PageIndex = e.NewPageIndex;
BindData();
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - FormView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FormView id="myFormView" runat="server"
OnDataBound="myFormView_DataBound"
OnPageIndexChanging="ShowPageCommand"
AllowPaging="True">
<ItemTemplate>
<table width="500" cellpadding="5" border="0">
<tr>
<td valign="top" align="center">
<asp:Image id="Image1" runat="server"/>
<br />
<h2><asp:Label id="lblGalleryName" runat="server"></asp:Label></h2>
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
Screenshot
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-15 12:25:00 /
2009-07-07 10:18:36 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|