(C#) ASP.NET FormView & DataList Control |
(C#) ASP.NET FormView & DataList Control ถ้าต้องการใช้ DataList ในการแสดงผลแบ่ง Column และใช้ FormView ในการแสดงคล้ายๆ กับ Gallery ก็สามารถทำได้เช่นเดียวกันครับ
Language Code : VB.NET || C#
Framework : 2,3,4
FormViewDataList.aspx
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
int strGalleryID;
String strSQL;
void Page_Load(object sender,EventArgs e)
{
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb") + ";";
objConn = new OleDbConnection(strConnString);
objConn.Open();
if(!Page.IsPostBack)
{
DataListBindData();
}
}
void DataListBindData()
{
strSQL = "SELECT * FROM gallery";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DataList ***//
myDataList.DataSource = dtReader;
myDataList.DataBind();
dtReader.Close();
dtReader = null;
}
void FormViewBindData()
{
strSQL = "SELECT * FROM gallery WHERE GalleryID = " + strGalleryID;
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to FormView ***//
myFormView.DataSource = dtReader;
myFormView.DataBind();
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
void btnButton_Click(Object sender, System.EventArgs e)
{
myFormView.Visible = false;
myDataList.Visible =true;
btnButton.Visible = false;
}
void myDataList_ItemCommand(Object source, DataListCommandEventArgs e)
{
if(e.CommandName == "View")
{
myDataList.Visible = false;
strGalleryID = (int)this.myDataList.DataKeys[e.Item.ItemIndex];
FormViewBindData();
myFormView.Visible = true;
btnButton.Visible = true;
}
}
void myDataList_ItemDataBound(Object sender, DataListItemEventArgs e)
{
//*** Image ***//
ImageButton Image1 = (ImageButton)(e.Item.FindControl("Image1"));
if(Image1 != null)
{
Image1.ImageUrl = "images/" + (string)DataBinder.Eval(e.Item.DataItem, "Picture");
Image1.Width = 100;
}
//*** GalleryName ***//
Label lblGalleryName = (Label)(e.Item.FindControl("lblGalleryName"));
if(lblGalleryName != null)
{
lblGalleryName.Text = (string)DataBinder.Eval(e.Item.DataItem, "GalleryName");
}
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - FormView & DataList</title>
</head>
<body>
<form id="form1" runat="server">
<!-- DataList -->
<asp:DataList id="myDataList" runat="server"
DataKeyField ="GalleryID"
OnItemDataBound="myDataList_ItemDataBound"
OnItemCommand="myDataList_ItemCommand"
RepeatColumns="2">
<ItemTemplate>
<table width="100" cellpadding="5" border="0">
<tr>
<td valign="top" align="center">
<asp:ImageButton id="Image1" CommandName="View" runat="server"></asp:ImageButton>
<asp:Label id="lblGalleryName" runat="server"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<!-- End DataList -->
<!-- FormView -->
<asp:FormView id="myFormView" runat="server" Visible="False">
<ItemTemplate>
<table width="500" cellpadding="5" border="0">
<tr>
<td valign="top" align="center">
<asp:Image id="Image1" runat="server" ImageUrl='<%# Eval("Picture", "images/{0}") %>' />
<br />
<h2><%# DataBinder.Eval(Container.DataItem, "GalleryName") %></h2>
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<!-- End FormView -->
<br />
<asp:Button id="btnButton" runat="server" Visible="False" OnClick="btnButton_Click" Text="< Back" />
</form>
</body>
</html>
Screenshot
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-11-15 09:03:20 /
2017-03-28 21:37:53 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|