كثيرا ما الاحظ لخطبه وخيوط عجيبه غريبه في اكواد ال html
اقواس وخيوط سباغيتي كل وحده لها شكل
بالرغم من اني فقط استعمل ال كثيرا <%# %>
داخل القريد فيو
الا ان الفضول دفعني لمعرفة باقي الاكواد ماذا تعني وكيف استعملها
واخيرا وجدت معلومات جيده
<% %> An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class.http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying single pieces of information.http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax. http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
<%$ %> ASP.NET Expression. http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx
<%@ %> Directive Syntax. http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx
<%-- --%> Server-Side Comments. http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx
<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4).http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx
باختصار ال <% %> يستعمل لاكواد السيرفر
مثال
<%@ Page Language="C#" %><html><body>
<form id="form1" runat="server">
<% for(int i = 0; i < 6; i++) %>
<% { Response.Write("<br>" + i.ToString()); }%>
</form>
</body>
</html>وايضا هذا مثال رائع
<% if (IsPostBack) { %>
<a href="SomeURL.aspx">Text for Link</a>
<% } %>هذا الكود جدا رائع برايي ويعطي تحكم اكبر لاكواد ال
html
ويعني انه الرابط
SomeURL.aspx
لايعمل الا اذا اعطى قيمه
true
<%$ %> يستخدم لتعابير asp.net
مثل الوصول الى بيانات webconfig
وال resources
مثال
<asp:Label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>" />
للوصول الى ال
Resources
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT * FROM [Employees]" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"></asp:SqlDataSource>
للوصول لبيانات الاتصال
NorthwindConnectionString1
<%# %>
استخدامه شائع وهو للبيانات المتصله في قاعدة البيانات او الغير متصله مثال
<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e) {
// Rather than explictly pulling out the variable from the StateList control
// and then manipulating a Label control, just call Page.DataBind.
// This will evaluate any <%# %> expressions within the page.
Page.DataBind();
}
</script>
</head>
<body>
<h3><font face="Verdana">Binding to a property of another server control</font></h3>
<form runat="server">
<asp:DropDownList id="StateList" runat="server">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
<asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<p>
Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>
</form>
</body>
</html>