ASP.Net/Data Binding/DataList — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:52, 26 мая 2010
asp:DataList repeat column (C#)
/*
ASP.NET 2.0 Unleashed (Unleashed) (Hardcover)
by Stephen Walther
# Publisher: Sams; Bk&CD-Rom edition (June 6, 2006)
# Language: English
# ISBN: 0672328232
*/
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
protected void btnAdd_Click(object sender, EventArgs e)
{
if (upImage.HasFile)
{
if (CheckFileType(upImage.FileName))
{
upImage.SaveAs(MapPath(upImage.FileName));
}
}
}
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case ".gif":
return true;
case ".png":
return true;
case ".jpg":
return true;
case ".jpeg":
return true;
default:
return false;
}
}
void Page_PreRender()
{
string upFolder = MapPath(".");
DirectoryInfo dir = new DirectoryInfo(upFolder);
dlstImages.DataSource = dir.GetFiles();
dlstImages.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FileUpload File</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblImageFile"
Text="Image File:"
AssociatedControlID="upImage"
Runat="server" />
<asp:FileUpload
id="upImage"
Runat="server" />
<br /><br />
<asp:Button
id="btnAdd"
Text="Add Image"
OnClick="btnAdd_Click"
Runat="server" />
<hr />
<asp:DataList
id="dlstImages"
RepeatColumns="3"
runat="server">
<ItemTemplate>
<asp:Image ID="Image1"
ImageUrl="<%# Eval("Name", "./{0}") %>"
style="width:200px"
Runat="server" />
<br />
<%# Eval("Name") %>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Bind data to asp:datalist (VB.net)
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
<title>Databound Control Example</title>
<script runat="server">
Sub Page_Load()
MyDataGrid.DataSource = CreateData()
MyDataGrid.DataBind()
MyDataList.DataSource = CreateData()
MyDataList.DataBind()
MyRepeater.DataSource = CreateData()
MyRepeater.DataBind()
End Sub
Function CreateData() As DataTable
Dim DT As New DataTable()
Dim Row1, Row2, Row3, Row4 As DataRow
DT.Columns.Add(New DataColumn("name", System.Type.GetType("System.String")))
DT.Columns.Add(New DataColumn("city", System.Type.GetType("System.String")))
Row1 = DT.NewRow()
Row1("name") = "Name 1"
Row1("city") = "City 1"
DT.Rows.Add(Row1)
Row2 = DT.NewRow()
Row2("name") = "Name 2"
Row2("city") = "City 2"
DT.Rows.Add(Row2)
Row3 = DT.NewRow()
Row3("name") = "Name 3"
Row3("city") = "City 3"
DT.Rows.Add(Row3)
Row4 = DT.NewRow()
Row4("name") = "Name 4"
Row4("city") = "City 4"
DT.Rows.Add(Row4)
Return DT
End Function
</script>
</head>
<body>
<h1>Databound Control Example</h1>
<form runat="server">
<asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
<asp:tablerow runat="server">
<asp:tablecell runat="server">
DataGrid Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:datagrid id="MyDataGrid"
allowpaging="true"
allowsorting="true"
alternatingitemstyle-backcolor="LightSkyBlue"
backcolor="Blue"
forecolor="White"
cellpadding="2"
cellspacing="0"
headerstyle-backcolor="DarkBlue"
headerstyle-forecolor="Yellow"
pagerstyle-mode="NumericPages"
pagesize="5"
runat="server"/>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
DataList Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:datalist id="MyDataList"
alternatingitemstyle-backcolor="LightSkyBlue"
backcolor="Blue"
bordercolor="Black"
cellpadding="2"
cellspacing="0"
forecolor="White"
headerstyle-backcolor="DarkBlue"
headerstyle-forecolor="Yellow"
repeatcolumns="1"
repeatdirection="vertical"
repeatlayout="table"
runat="server">
<headertemplate>
Composers
</headertemplate>
<itemtemplate>
<%# databinder.eval(container.dataitem, "name") %>
</itemtemplate>
</asp:datalist>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow runat="server">
<asp:tablecell runat="server">
Repeater Control:
</asp:tablecell>
<asp:tablecell runat="server">
<asp:repeater id="MyRepeater" runat="server">
<headertemplate>
<table cellpadding="5" cellspacing="0">
<tr>
<td>Name<hr/></td>
<td>City<hr/></td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, _
"name") %></td>
<td><%# DataBinder.Eval(Container.DataItem, _
"city") %></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:repeater>
</asp:tablecell>
</asp:tablerow>
</asp:table>
</form>
</body>
</html>