首页 > 代码库 > ASP.net一些绑定

ASP.net一些绑定


<%#Container.DataItem%>适用于DataList控件:
例子:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
数字 :<%#Container.DataItem %>>

</td>
</tr>

<tr>
<td>
绝对值:<%#AbsoluteValue((int)Container.DataItem) %>>
</td>
</tr>


</table>

</ItemTemplate>


</asp:DataList>

</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList number = new ArrayList();
number.Add(-8);
number.Add(-2);
number.Add(-5);
DataList1.DataSource = number;
DataList1.DataBind();

}


}

public int AbsoluteValue(int number)
{
if (number > 0)
return number;
else
return (-number);

}
}


绑定DataTable对象,要用GridView控件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<form id="form1" runat="server">
<div>

<table>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>

</table>

</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
double interest;
double total;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("月份", typeof(int)));
dt.Columns.Add(new DataColumn("现存款数", typeof(double)));
dt.Columns.Add(new DataColumn("利息", typeof(double)));
dt.Columns.Add(new DataColumn("本息和", typeof(double)));
for (int i = 0; i <= 11; i++)
{
if (!(i < 0))
{
interest = 0;
total = 500;
}

else {
interest = (total * 0.05);
total = (500 + interest + total);

}
dr = dt.NewRow();
dr[0] = i + 1;
dr[1] = 500;
dr[2] = interest;
dr[3] = total;
dt.Rows.Add(dr);


}

GridView1.DataSource = dt;
GridView1.DataBind();


}


}


}

ASP.net一些绑定