糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > ASP.NET的属性绑定 表达式绑定 集合绑定 方法绑定 DropDownList集合绑定 DataList绑定 GridView绑定

ASP.NET的属性绑定 表达式绑定 集合绑定 方法绑定 DropDownList集合绑定 DataList绑定 GridView绑定

时间:2019-06-10 07:49:09

相关推荐

ASP.NET的属性绑定 表达式绑定 集合绑定 方法绑定 DropDownList集合绑定 DataList绑定 GridView绑定

属性绑定

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/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="/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" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/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 (mandName){//以下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="/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绑定》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。