[ת]dz̸.NETÖеÄÊý¾Ý°ó¶¨±í´ïʽ(Ò»)
½ñÌì̸ÏÂ.NETÖеÄÊý¾Ý°ó¶¨±í´ïʽ¡£
Êý¾Ý°ó¶¨±í´ïʽ±ØÐë°üº¬ÔÚ<%#ºÍ%>×Ö·ûÖ®¼ä¡£¸ñʽÈçÏÂ:
<tagprefix:tagname property='<%# data-binding expression %>' runat="server" />
»òÕßÈçÏ£º
<%# data-binding expression %>
ASP.NET Ö§³Ö·Ö²ãÊý¾Ý°ó¶¨Ä£ÐÍ£¬Êý¾Ý°ó¶¨±í´ïʽʹÓà Eval ºÍ Bind ·½·¨½«Êý¾Ý°ó¶¨µ½¿Ø¼þ£¬²¢½«¸ü¸ÄÌá½»»ØÊý¾Ý¿â¡£
Eval ·½·¨ÊǾ²Ì¬µ¥Ïò£¨Ö»¶Á£©·½·¨£¬ËùÒÔEval º¯ÊýÓÃÓÚµ¥Ïò£¨Ö»¶Á£©°ó¶¨£¬¸Ã·½·¨²ÉÓÃÊý¾Ý×ֶεÄÖµ×÷Ϊ²ÎÊý²¢½«Æä×÷Ϊ×Ö·û´®·µ»Ø¡£
Bind ·½·¨Ö§³Ö¶Á/д¹¦ÄÜ£¬ËùÒÔBind º¯ÊýÓÃÓÚË«Ïò£¨¿É¸üУ©°ó¶¨¡£¸Ã·½·¨¿ÉÒÔ¼ìË÷Êý¾Ý°ó¶¨¿Ø¼þµÄÖµ²¢½«Èκθü¸ÄÌá½»»ØÊý¾Ý¿â¡£
XPath ·½·¨Ö§³Ö¶ÔXMLÀàÐ͵ÄÊý¾ÝÔ´Ìṩ֧³Ö¡£
Êý¾Ý°ó¶¨±í´ïʽ¶¼¿ÉÒÔ³öÏÖÔÚÒ³ÃæµÄÄĸöλÖÃÄØ£¿
Ò»£¬¿ÉÒÔ½«Êý¾Ý°ó¶¨±í´ïʽ°üº¬ÔÚ·þÎñÆ÷¿Ø¼þ»òÕßÆÕͨµÄhtmlÔªËصĿªÊ¼±ê¼ÇÖÐÊôÐÔÃû/ÊôÐÔÖµ¶ÔµÄÖµ²à¡£ÀýÈ磺
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Êý¾Ý°ó¶¨±í´ïʽ%>' ></asp:TextBox><br />
×¢ÒâÌõ¿î£º´ËʱÊý¾ÝµÄ°ó¶¥±í´ïʽ¿ÉÒÔÊÇÒ»¸ö±äÁ¿£¬Ò²¿ÉÒÔÊÇÒ»¸ö´ø·µ»ØÖµµÄC#»òÕßVB.NET·½·¨£¬»¹¿ÉÒÔÊÇij¸ö¿Ø¼þµÄij¸öÊôÐÔµÄÖµ£¬Ò²¿ÉÒÔÊÇC#»òÕßVB.NET¶ÔÏóµÄij¸ö×ֶλòÕßÊôÐÔµÄÖµµÈµÈ¡£µ±È»Ò²¿ÉÒÔÖ±½Ó¾ÍÊÇÒ»¸ö×Ö·û´®£¬ÀýÈç"hello"¡£
Èç¹û´ËʱµÄÊý¾Ý°ó¶¨±í´ïʽÊÇEval("Êý¾Ý¿âÖÐij¸ö±íµÄij¸ö×Ö¶Î")µÈ,ÄÇ
ô±ØÐë°ÑTextBox1·ÅÔÚij¸öÑ»·ÏÔʾµÄ¿Ø¼þµÄÄ£°åÖвÅÕýÈ·£¬·ñÔò»áÌáʾ£ºEval()¡¢XPath() ºÍ Bind()
ÕâÀàÊý¾Ý°ó¶¨·½·¨Ö»ÄÜÔÚÊý¾Ý°ó¶¨¿Ø¼þµÄÉÏÏÂÎÄÖÐʹÓá£Æäʵ¾ÍÊÇÏëÈÃÄã°ÑTextBox1·ÅÔÚÏñRepeater,DataList,GridViewÕâÑù
µÄ¿Ø¼þµÄÄ£°åÖС£
¶þ£¬Êý¾Ý°ó¶¨°ó¶¨±í´ïʽ°üº¬ÔÚÔÚÒ³ÃæÖеÄÈκÎλÖá£ÀýÈ磺
<form id="form1" runat="server">
<div>
¡¡ <%#Eval("Êý¾Ý°ó¶¨±í´ïʽ£±")%>
¡¡ <%#Eval("Êý¾Ý°ó¶¨±í´ïʽ£²")%>
</div>
</form>
ͬÑù×ñÑ"Ò»"µÄ×¢ÒâÌõ¿î¡£
Èç¹û´ËʱµÄÊý¾Ý°ó¶¨±í´ïʽÊÇEval("Êý¾Ý¿âÖÐij¸ö±íµÄij¸ö×Ö¶Î")µÈ,ÄÇô±ØÐë°Ñ <%#Eval("Êý¾Ý°ó¶¨±í´ïʽ£±")%> <%#Eval("Êý¾Ý°ó¶¨±í´ïʽ£²")%> ·ÅÔÚÏñRepeater,DataList,GridViewÕâÑùµÄ¿Ø¼þµÄÄ£°åÖС£
Èý£¬¿ÉÒÔ½«Êý¾Ý°ó¶¨±í´ïʽ°üº¬ÔÚJavascript´úÂëÖÐ,´Ó¶øʵÏÖÔÚJavascriptÖе÷ÓÃC#»òÕßVB.NETµÄ·½·¨¡£ÀýÈ磺
Deafult2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<script language ="javascript" type="text/javascript">
function GetStr()
{
var a;
a = '';
a='<%#CSharpToJavascript()%>' //µ÷ÓÃc#µÄ·½·¨
alert(a);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="Javascriptµ÷ÓÃc#µÄ·½·¨!" onclick="GetStr()" /</div>
</form>
</body>
</html>
Default2.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();//·½·¨Óзµ»ØÖµµÄÒªÏÈ°ó¶¨,²ÅÄÜʵÏÖJavascriptµ÷ÓÃc#µÄ·½·¨!
}
public string CSharpToJavascript()
{
return "Javascriptµ÷ÓÃc#µÄ·½·¨!";
}
}
1£¬¿ÉÒÔÊÇÒ»¸ö±äÁ¿
ÀýÈ磺<asp:Label ID="Label1" runat="server" Text="<%#±äÁ¿Ãû%>"></asp:Label>
2£¬¿ÉÒÔÊÇ·þÎñÆ÷¿Ø¼þµÄÊôÐÔÖµ
ÀýÈ磺 <asp:Label ID="Label1" runat="server" Text="<%#TextBox2.Text %>"></asp:Label>
3£¬¿ÉÒÔÊÇÒ»¸öÊý×éµÈ¼¯ºÏ¶ÔÏó
ÀýÈç°ÑÒ»¸öÊý×é°ó¶¨µ½Áбí¿Ø¼þ£¬ÀýÈçListBoxµÈ£¬»òÕßRepeater,DataList,GridViewÕâÑùµÄ¿Ø¼þµÈ£¬´ËʱֻÐèÒª°ÑÊôÐÔDataSource='<%# Êý×éÃû%>' ¡£
4£¬¿ÉÒÔÊÇÒ»¸ö±í´ïʽ
ÀýÈ磺PersonÊÇÒ»¸ö¶ÔÏó£¬NameºÍCityÊÇËüµÄ2¸öÊôÐÔ£¬ÔòÊý¾Ý°ó¶¨±í´ïʽ¿ÉÒÔÕâÑùд£º
<%#(Person.Name + " " + Person.City)%>¡£
5£¬¿ÉÒÔÊÇÒ»¸ö·½·¨
ÀýÈ磺<%#GetUserName()%>¡£GetUserName()ÊÇÒ»¸öÒѾ¶¨ÒåµÄC#·½·¨£¬Ò»°ãÒªÇóÓзµ»ØÖµ¡£
6,¿ÉÒÔÊÇÓÃEval,DateBinder.EvalÈ¡µÃµÄÊý¾Ý±íµÄ×ֶΣ¬Õâ¸öÊÇ×î³£¼ûµÄÁË£¬²»ÔÙ¾ÙÀý¡£
×¢Ò⣺Èç¹ûÊý¾Ý°ó¶¨±í´ïʽ×÷ΪÊôÐÔµÄÖµ£¬Ö»ÒªÊý¾Ý°ó¶¨±í´ïʽÖÐûÓгöÏÖË«ÒýºÅ£¬ÄÇô<%#Êý¾Ý°ó¶¨±í´ïʽ%>µÄ×îÍâ²ãÓÃË«ÒýºÅ»òÕßµ¥ÒýºÅ¶¼¿ÉÒÔ¡£Èç¹ûÊý¾Ý°ó¶¨±í´ïʽÖгöÏÖË«ÒýºÅ£¬Ôò<%#Êý¾Ý°ó¶¨±í´ïʽ%>µÄ×îÍâ²ã×îºÃÒªÓõ¥ÒýºÅ¡£
ÓëÊý¾Ý¿âÓйضøÇҰ󶨵½DataView,DataTable,DataSet µÈÊý¾ÝÔ´µÄÊý¾Ý°ó¶¨±í´ïʽ¶¼ÓÐÄÇЩ£¿
1£¬<%#DataBinder.Eval(Container.DataItem,"×Ö¶ÎÃû")%>
<%#DataBinder.Eval(Container.DataItem,"×Ö¶ÎÃû","{0:c}") %>
»¹ÓÐ2ÖÖ²»³£Óõģº
<%# DataBinder.Eval(Container,"DataItem.×Ö¶ÎÃû")%>
<%# DataBinder.Eval(Container,"DataItem.×Ö¶ÎÃû"£¬{0:c})%>
Container.DataItemÏ൱ÓÚÊý¾Ý¿âÖÐij¸ö±í»òÕßÊÓͼÖеÄÒ»ÐмǼ£¬¶øÒ»ÐпÉÒÔÓкܶàÁС£
×îºóÒ»¸ö²ÎÊýºÍ String.FormatµÄÐÎʽһÑù¡£ÀýÈçc´ú±í»õ±Ò£¬p´ú±í°Ù·ÖºÅ£¬d´ú±í¶ÌÈÕÆÚ¸ñʽÏÔʾ,f´ú±í¸¡µãÊýÏÖʵ£¬f3´ú±íСÊýµãºóÈýΪ£¬Ò»´ÎÀàÍÆ¡£
ʹÓÃÈýÄ¿ÔËËã·û?:µÄÀý×Ó£º
<%# DataBinder.Eval(Container.DataItem, "×Ö¶ÎÃû").ToString().Trim().Length>16£¿DataBinder.Eval(Container.DataItem, "×Ö¶ÎÃû").ToString().Trim().Substring(0,16):DataBinder.Eval(Container.DataItem, "×Ö¶ÎÃû").ToString().Trim() %>
2£¬<%#Eval("×Ö¶ÎÃû")%>
<%#Eval("×Ö¶ÎÃû","{0:c}")%>
.NET 2.0гöÏÖµÄÒ»¸ö·½·¨¡£ºÍDataBinder.Eval()µÈ¼Û¡£
×îºóÒ»¸ö²ÎÊýºÍ
String.FormatµÄÐÎʽһÑù¡£ÀýÈçc´ú±í»õ±Ò£¬p´ú±í°Ù·ÖºÅ£¬d´ú±í¶ÌÈÕÆÚ¸ñʽÏÔʾ,f´ú±í¸¡µãÊýÏÖʵ£¬f3´ú±íСÊýµãºóÈý룬һ´ÎÀàÍÆ¡£<%#string.Format("{0:yyyy-MM-dd dddd}",Eval("stuBirth"))%>
<%# DataBinder.Eval(Container.DataItem,"stuBirth","{0:yyyy-MM-dd}")%>
ʹÓÃÈýÄ¿ÔËËã·ûµÄÀý×Ó£º
<%#(Eval("ÐÔ±ð")).ToString() =="True"?"ÄÐ":"Å®"%>
ÐÔ±ð×Ö¶ÎÀàÐÍΪ£ºÊÇ/·ñ(Access)£¬bit(sql server)
ʹÓ÷½·¨µ÷ÓõÄÀý×Ó£º
<%# GetUserPhoto(Eval("PhotoPath")) %>
GetUserPhoto()µÄ¶¨Ò壺
3£¬ <%#((DataRowView)Container.DataItem)["×Ö¶ÎÃû"] %>string GetUserPhoto(object photoPath)
{
if (photoPath == DBNull.Value)<%#((DataRowView)Container.DataItem)["×Ö¶ÎÃû"] %>
°ó¶¨µ½DataView,DataTable,DataSet
{
return "<img src='Images/none.gif'>";
}
else
{
return "<img src='Upload/" +photoPath.ToString() + "'>";
}
}
<%# string.Format("{0:c}", ((DataRowView)Container.DataItem)["×Ö¶ÎÃû"])%>
Container.DataItemÏ൱ÓÚÊý¾Ý¿âÖÐij¸ö±í»òÕßÊÓͼÖеÄÒ»ÐмǼ£¬¶øÒ»ÐпÉÒÔÓкܶàÁС£
ÓÃString.Format·½·¨É趨×ֶεÄÏÔʾÑùʽ¡£ÀýÈçc´ú±í»õ±Ò£¬p´ú±í°Ù·ÖºÅ£¬d´ú±í¶ÌÈÕÆÚ¸ñʽÏÔʾ,f´ú±í¸¡µãÊýÏÖʵ£¬f3´ú±íСÊýµãºóÈýΪ£¬Ò»´ÎÀàÍÆ¡£
ÀàÐÍת»»Àý×Ó£º<%# (int)((DataRowView)Container.DataItem)["×Ö¶ÎÃû1"]*(int)((DataRowView)Container.DataItem)["×Ö¶ÎÃû2"]%> Òâ˼ÊÇ2¸ö×Ö¶ÎÏà³Ë¡£
ÉÏ
ÃæÈýÖְ󶨷½·¨µÄЧÂÊ£ºEval·½·¨Ö´ÐÐʱºò»áµ÷ÓÃDataBinder.Eval·½·¨£¬DataBinder.Eval·½·¨ÔÚÔËÐÐʱʹÓ÷´ÉäÖ´ÐкóÆÚ°ó
¶¨¼ÆËã,»áµ¼ÖÂÐÔÄÜÃ÷ÏÔϽµ¡£ËùÒԻᵼÖÂÐÔÄÜÃ÷ÏÔϽµ¡£ËùÒÔÈýÕßÖÐ<%#((DataRowView)Container.DataItem)["
×Ö¶ÎÃû"] %>µÄÐÔÄÜ×îºÃ¡£
ÍƼöÐÅÏ¢
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- Asp.net MVCÔ´Âë·ÖÎö--Model Validation(Se....
- .Net À¬»ø»ØÊÕ»úÖÆÔÀíºÍËã·¨£¨Ò»£©
- .netʵÌåнâ
- ·ÖÎöASP.NET¶ÁÈ¡XMLÎļþ4ÖÖ·½·¨
- »ñÈ¡ÍøÕ¾ËõÂÔͼ´úÂë
- Asp.net ¸ù¾ÝIPµØÖ·»ñÈ¡¿çÍø¶ÎmacµØÖ·º¯Êý¡¾ËѲء¿
- ¹ýÂËÃô¸Ð¹Ø¼ü×Ö¡£
- Éú³ÉÖ¸¶¨Î»ÊýµÄËæ»ú×Ö·û´®
- ¿Í»§¶Ë»ñÈ¡MACµØÖ·£¬µ½·þÎñÆ÷ÑéÖ¤
- ²½²½ÎªÓª .NETÈý²ã¼Ü¹¹½âÎö ¶þ¡¢Êý¾Ý¿âÉè¼Æ
ÆÀÂÛ