属性绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>属性绑定</title>
</head>
<body><form id="form1" runat="server"><div>name:<%# name  %><br />sex:<%# sex  %></div></form>
</body>
</html>

.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{public string name{get{return "苦涩精灵";}}public string sex{get{return "男";}}protected void Page_Load(object sender, EventArgs e){Page.DataBind();}
}

Demo:

表达式绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>表达式绑定</title>
</head>
<body><form id="form1" runat="server"><div><table><tr><td colspan="2">表达式绑定</td><td style="width: 100px"></td></tr><tr><td style="width: 82px">单价:</td><td style="width: 100px"><asp:TextBox ID="TextBox1" runat="server">0</asp:TextBox></td><td style="width: 100px"><asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="输入数字" ControlToValidate="TextBox1" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td></tr><tr><td style="width: 82px">数量:</td><td style="width: 100px"><asp:TextBox ID="TextBox2" runat="server">0</asp:TextBox></td><td style="width: 100px"><asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="输入数字" ControlToValidate="TextBox2" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator></td></tr><tr><td colspan="2"><asp:Button ID="btnOk" runat="server" Text="确定" /></td><td style="width: 100px"></td></tr><tr><td colspan="2"><asp:Label ID="Label1" runat="server" Text='<%#"总金额为:"+Convert.ToString(Convert.ToDecimal (TextBox1.Text)*Convert.ToInt32(TextBox2.Text)) %>'></asp:Label></td><td style="width: 100px"></td></tr></table></div></form>
</body>
</html>

.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){Page.DataBind();}
}

Demo:

集合绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>集合绑定</title>
</head>
<body><form id="form1" runat="server"><div><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList></div></form>
</body>
</html>

.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){System.Collections.ArrayList arraylist = new ArrayList();arraylist.Add("土豆咖喱鸡");arraylist.Add("黄焖鸡米饭");arraylist.Add("麦辣鸡腿堡");DropDownList1.DataSource = arraylist;DropDownList1.DataBind();}
}

Demo:

方法绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>绑定方法调用的结果</title>
</head>
<body><form id="form1" runat="server"><div>&nbsp;<table><tr><td colspan="2">绑定方法调用的结果</td><td style="width: 100px"></td></tr><tr><td style="width: 100px">第一个数:</td><td style="width: 100px"><asp:TextBox ID="txtNum1" runat="server" Width="60px">0</asp:TextBox></td><td style="width: 100px"><asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtNum1"ErrorMessage="输入数字" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td></tr><tr><td style="width: 100px">第二个数:</td><td style="width: 100px"><asp:TextBox ID="txtNum2" runat="server" Width="60px">0</asp:TextBox></td><td style="width: 100px"><asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtNum2"ErrorMessage="输入数字" Operator="DataTypeCheck" Type="Double"></asp:CompareValidator></td></tr><tr><td style="width: 100px; height: 24px">运算符号:</td><td style="width: 100px; height: 24px"><asp:DropDownList ID="ddlOperator" runat="server"><asp:ListItem>--请选择运算符号--</asp:ListItem><asp:ListItem>+</asp:ListItem><asp:ListItem>-</asp:ListItem><asp:ListItem>*</asp:ListItem><asp:ListItem>/</asp:ListItem></asp:DropDownList></td><td style="width: 100px; height: 24px"></td></tr><tr><td style="width: 100px"></td><td style="width: 100px"><asp:Button ID="btnOk" runat="server" Text="确定" /></td><td style="width: 100px"></td></tr><tr><td style="width: 100px">运算结果:</td><td style="width: 100px"><asp:Label ID="Label1" runat="server" Text='<%#operation(ddlOperator.SelectedValue) %>'/></td><td style="width: 100px"></td></tr></table></div></form>
</body>
</html>

.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){       Page.DataBind();}public string operation(string VarOperator){double num1=Convert.ToDouble(txtNum1.Text);double num2=Convert.ToDouble(txtNum2.Text);double result = 0;switch (VarOperator){case "+":result = num1 + num2;break ;case "-":result = num1 - num2;break ;case "*":result = num1 * num2;break ;case "/":result = num1 / num2;break ;}return result.ToString ();}
}

Demo:

DropDownList集合绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title>
</head>
<body><form id="form1" runat="server"><div><asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList></div></form>
</body>
</html>

.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){SqlConnection con = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Database=Experiment;Uid=;Pwd=");SqlDataAdapter da = new SqlDataAdapter(@"select id,age from users", con);DataSet ds = new DataSet();da.Fill(ds);DropDownList1.DataSource = ds;DropDownList1.DataTextField = "id";DropDownList1.DataValueField = "age";DropDownList1.DataBind();}}protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){Response.Write("Id:" + DropDownList1.SelectedValue + "Age:" + DropDownList1.SelectedItem.Text);}
}

Demo:

DataList绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>无标题页</title></head>
<body><form id="form1" runat="server"><div><table height="128" border="0" cellpadding="0" cellspacing="0" style="width: 653px"><tr><td align="left"><asp:DataList ID="DataList1" runat="server" Width="693px" style="font-size: small" onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound" DataKeyField="bh"><ItemTemplate><table><tr style="border-bottom-style: groove; border-bottom-width: medium; border-bottom-color: #FFFFFF"><td rowspan="3" align="center" class="style3"><a href='#'><img border="0" height="80" src='images/showimg.gif' width="80"> </img></a></td><td align="left"><asp:Image ID="Image4" runat="server" ImageUrl="~/images/ico2.gif" /><a><%#Eval("name")%></a></td><td align="left">&nbsp;</td><td>&nbsp;</td></tr><tr><td align="left">编号:<a><%#Eval("bh") %></a></td><td align="left">--名称:<a><%#Eval("name") %></a></td><td>&nbsp;</td></tr><tr><td align="left" colspan="3">价格:<a ><%#Eval("price")%></a></td></tr></table></ItemTemplate><FooterTemplate><div style="text-align: center"><table id="Page" border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; width: 68%"><tr><td ><asp:Label ID="labCurrentPage" runat="server"></asp:Label>/<asp:Label ID="labPageCount" runat="server"></asp:Label><asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first" Font-Underline="False" ForeColor="Black">首页</asp:LinkButton><asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre" Font-Underline="False" ForeColor="Black">上一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next" Font-Underline="False" ForeColor="Black">下一页</asp:LinkButton><asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last" Font-Underline="False" ForeColor="Black">尾页</asp:LinkButton>&nbsp;&nbsp; 跳转至:<asp:TextBox ID="txtPage" runat="server" Width="35px" Height="21px"></asp:TextBox><asp:Button ID="Button1" runat="server" CommandName="search" Text="GO" Height="19px" /><br /></td></tr></table></div></FooterTemplate></asp:DataList></td></tr></table></div></form>
</body>
</html>

.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{static PagedDataSource pds = new PagedDataSource();SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Database=Experiment;Uid=;Pwd=");protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){BindDataList(0);}}private void BindDataList(int currentpage){pds.AllowPaging = true;//允许分页pds.PageSize = 3;//每页显示3条数据pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值string strSql = "SELECT * FROM flower_detail";//定义一条SQL语句conn.Open();//打开数据库连接 SqlDataAdapter sda = new SqlDataAdapter(strSql,conn);DataSet ds = new DataSet();sda.Fill(ds);//把执行得到的数据放在数据集中pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中DataList1.DataSource = pds;//绑定DatalistDataList1.DataBind();conn.Close();}protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e){switch (e.CommandName){//以下5个为 捕获用户点击 上一页 下一页等时发生的事件case "first"://第一页pds.CurrentPageIndex = 0;BindDataList(pds.CurrentPageIndex);break;case "pre"://上一页pds.CurrentPageIndex = pds.CurrentPageIndex - 1;BindDataList(pds.CurrentPageIndex);break;case "next"://下一页pds.CurrentPageIndex = pds.CurrentPageIndex + 1;BindDataList(pds.CurrentPageIndex);break;case "last"://最后一页pds.CurrentPageIndex = pds.PageCount - 1;BindDataList(pds.CurrentPageIndex);break;case "search"://页面跳转页if (e.Item.ItemType == ListItemType.Footer){int PageCount = int.Parse(pds.PageCount.ToString());TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;int MyPageNum = 0;if (!txtPage.Text.Equals(""))MyPageNum = Convert.ToInt32(txtPage.Text.ToString());if (MyPageNum <= 0 || MyPageNum > PageCount)Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");elseBindDataList(MyPageNum - 1);}break;}}protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e){if (e.Item.ItemType == ListItemType.Footer){//以下六个为得到脚模板中的控件,并创建变量.Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;Label PageCount = e.Item.FindControl("labPageCount") as Label;LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页PageCount.Text = pds.PageCount.ToString();//绑定显示总页数if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用{FirstPage.Enabled = false;PrePage.Enabled = false;}if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用{NextPage.Enabled = false;LastPage.Enabled = false;}}}
}

Demo:

GridView绑定
.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title>
</head>
<body><form id="form1" runat="server"><div></div><asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" PageSize="5"><AlternatingRowStyle BackColor="White" ForeColor="#284775" /><Columns><asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="Id" /><asp:BoundField DataField="age" HeaderText="age" SortExpression="age" /><asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /><asp:CommandField ShowEditButton="True" /><asp:CommandField ShowDeleteButton="True"  /></Columns><EditRowStyle BackColor="#999999" /><FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /><RowStyle BackColor="#F7F6F3" ForeColor="#333333" /><SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /><SortedAscendingCellStyle BackColor="#E9E7E2" /><SortedAscendingHeaderStyle BackColor="#506C8C" /><SortedDescendingCellStyle BackColor="#FFFDF8" /><SortedDescendingHeaderStyle BackColor="#6F8DAE" /></asp:GridView><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ExperimentConnectionString %>" SelectCommand="SELECT [Id], [age], [name] FROM [users]" DeleteCommand="DELETE FROM [users] WHERE [Id] = @Id" InsertCommand="INSERT INTO [users] ([Id], [age], [name]) VALUES (@Id, @age, @name)" UpdateCommand="UPDATE [users] SET [age] = @age, [name] = @name WHERE [Id] = @Id"><DeleteParameters><asp:Parameter Name="Id" Type="Int32" /></DeleteParameters><InsertParameters><asp:Parameter Name="Id" Type="Int32" /><asp:Parameter Name="age" Type="Int32" /><asp:Parameter Name="name" Type="String" /></InsertParameters><UpdateParameters><asp:Parameter Name="age" Type="Int32" /><asp:Parameter Name="name" Type="String" /><asp:Parameter Name="Id" Type="Int32" /></UpdateParameters></asp:SqlDataSource><asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource><asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource></form>
</body>
</html>

Demo:

ASP.NET的属性绑定、表达式绑定、集合绑定、方法绑定、DropDownList集合绑定、DataList绑定、GridView绑定相关推荐

  1. Java List<T>去重方法,引用类型集合去重

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 一.实体类中要重写比较方法equals,最好也重写hashcode方法 public class W ...

  2. python set集合_玩转Python集合,这一篇就够了!

    点击 机器学习算法与Python学习 ,选择加星标 精彩内容不迷路 选自DataCamp, 作者:Michael Galarnyk 本文由机器之心编译 了解 Python 集合: 它们是什么,如何创建 ...

  3. 集合论——集合表示方法,关系及特殊集合

    表示 方法 能否表示 无限集 备注 列举法 N 列举出集合中的所有元素,元素之间用逗号分开,然后用花括号把所有元素括起来. 描述法 Y 用某个英文字母来统一表示集合的元素,并指出元素的共同特征. 表1 ...

  4. salesforce lightning零基础学习(三) 表达式的!(绑定表达式)与 #(非绑定表达式)

    在salesforce的classic中,我们使用{!expresion}在前台页面展示信息,在lightning中,上一篇我们也提及了,如果展示attribute的值,可以使用{!v.expresi ...

  5. 基于继承类的属性模版中无法绑定的原因和解决方法

    原文:基于继承类的属性模版中无法绑定的原因和解决方法 这个的晚上想学学WPF 做一个类似于ERP 左边菜单.构思一下思路 ...... 创建一个类基于Expander类. 1 public class ...

  6. scala 环境搭建 变量 值 数据类型 元组 表达式块 语句 函数 柯里化 集合 面向对象 隐式转换

    scala (scalable的简写) scala是一个比较冷门的语言,不太被人们所知道 为什么这么冷门的语言现在被我们使用 很多的大数据的项目的源码是是用scala语言编写的. 因为大数据技术不断被 ...

  7. 数据绑定(三)为Binding指定绑定源的几种方法

    原文:数据绑定(三)为Binding指定绑定源的几种方法 Binding的源是数据的来源,所以,只要一个对象包含数据并能通过属性把数据暴露出来,它就能当作Binding的源来使用,常用的办法有: 一. ...

  8. GridView绑定DataSet的DataTable,怎么让它在无记录时能显示表头呀(环境VS2005),在线等 谢谢!!

    主 题:   GridView绑定DataSet的DataTable,怎么让它在无记录时能显示表头呀(环境VS2005),在线等 谢谢!! 作 者:   bolowang (波罗王子)        ...

  9. React.js绑定this的5种方法

    this在javascript中已经相当灵活,把它放到React中给我们的选择就更加困惑了.下面一起来看看React this的5种绑定方法. 1.使用React.createClass 如果你使用的 ...

最新文章

  1. 免费学习AI公开课:打卡、冲击排行榜,还有福利领取
  2. TaxonKit:小巧、高效、实用的NCBI分类学数据命令行工具
  3. 现在电脑的主流配置_玩手游是因为电脑配置差?现在来告诉你这些网游需要啥配置...
  4. hdfs查看目录大小文件大小
  5. 【arc068F】Solitaire
  6. 风控做得好,离不开这一项核心能力
  7. XNOR.ai融资1200万美元
  8. 在Ubuntu16.04下安装cmake 和cmake-gui
  9. 看雪CTF.TSRC 2018 团队赛 第六题 『追凶者也』 解题思路
  10. PHP树形结构数据增加每层的级别
  11. html打字练习测试代码,JS实现简单打字测试
  12. Name was not previously introduced as per JSP.5.3的解决办法
  13. 2022年最新广播电视广告报价(共23份)
  14. 你需要一份更绝佳的文章排版与设计
  15. [EE261学习笔记] 13.离散傅里叶逆变换及离散傅里叶变换的一些性质
  16. golang并发http请求几种方式
  17. WinForm—控件TabIndex的作用和设置
  18. Frida hook零基础教程
  19. [转]深入Mysql字符集设置
  20. Android中Shared Preferences、Files、Network、SQLite数据库编程总结及示例

热门文章

  1. 如何用 GitHub Actions 写出高质量的 Python代码?
  2. 机器学习霸占高薪榜、区块链偃旗息鼓?2020 年软件工程师状况解读!
  3. 你能说出 Kafka 这些原理吗?
  4. GitHub:围剿Python计划!程序员:早知必有一战!你站谁?
  5. 为何你仍是一名社畜码农?
  6. 程序员如何用编程套路追到女朋友的?
  7. 5G 兴起、智能手机饱和、AI 普及......2019 年的科技趋势预测
  8. 我扒了 6730 个微信用户数据,得出了这些结论......
  9. ofo 辟谣滴滴收购;美团饿了么回应数据泄露;金立转战海外 | CSDN 极客头条
  10. TIOBE 3 月编程语言排行榜:Ruby 重回前十,Kotlin 突围