[英]How to write inline C# code within html im asp.net
我正在asp.net中開發一個網站,所以我想在html頁面中編寫c#代碼,但我不知道該怎么做。我知道我應該使用此“ <%%>”來使用C#代碼,但我需要更多幫助。請更正以下代碼
HTML中的Asp.net,但無法正常工作
<% System.Data.SqlClient.SqlConnection sqlc = new System.Data.SqlClient.SqlConnection(@"Data Source=DESKTOP-B7KQDP5\MSSQLSERVER2014;Initial Catalog=Rishmak;Integrated Security=True");
System.Data.SqlClient.SqlCommand sqlcmd = new System.Data.SqlClient.SqlCommand("select * from indexinfo", sqlc);
sqlc.Open();
System.Data.SqlClient.SqlDataReader dr = sqlcmd.ExecuteReader();
if (dr.Read())
{%>
<div class="dr["classs"].ToString();">
<img src="../images/ dr["img"].ToString();" alt="Alternate Text" />dr["onvan"].ToString();
<div style=" height:25px"></div>
<div class="column-center-text bounceIn animated">
dr["matn"].ToString();
</div>
<div><a href="#" class="btn">بیشتر بدانید</a></div>
</div>
}%>
我不是專家,但是對我來說卻像ASP.NET ...請注意那里使用了asp:Image標記。 基本上,您正在尋找的是嵌入了ASP.NET控件的HTML。
您正在看的是ASP.NET代碼,但我建議您看一下使基礎知識和基礎知識下降的教程。 ASP.NET 4.5 Web窗體入門
我相信您使用舊的asp邏輯解決了這個問題。 在asp.Net中,您不必將代碼放入HTML標記內,而應創建asp .net對象並從代碼隱藏中進行處理。
在您的情況下,Repeater對象可以解決問題。 請檢查以下鏈接:
這樣,您可以將要重復的代碼放入轉發器中,並讓asp.net邏輯根據需要使用不同的參數重復該代碼。
<%
System.Data.SqlClient.SqlConnection sqlc = new System.Data.SqlClient.SqlConnection(@"Data Source=DESKTOP-B7KQDP5\MSSQLSERVER2014;Initial Catalog=Rishmak;Integrated Security=True");
System.Data.SqlClient.SqlCommand sqlcmd = new System.Data.SqlClient.SqlCommand("select * from indexinfo", sqlc);
sqlc.Open();
System.Data.SqlClient.SqlDataReader dr = sqlcmd.ExecuteReader();
if (dr.Read())
{ %>
<div class="<% Response.Write(dr["classs"]); %>">
<img src="../images/<% Response.Write(dr["img"]); %>" alt="Alternate Text" /><% Response.Write(dr["onvan"]); %>
<div style=" height:25px"></div>
<div class="column-center-text bounceIn animated">
<% Response.Write(dr["matn"]); %>
</div>
<div><a href="#" class="btn">بیشتر بدانید</a></div>
</div>
<%
}%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.