首页 > Web开发 > 详细

ASP.net一些绑定

时间:2015-01-03 22:22:21      阅读:264      评论:0      收藏:0      [点我收藏+]


<%#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一些绑定

原文:http://www.cnblogs.com/ZLGBloge/p/4199942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!