[英]Unable to print integer value using Response.Write()
我有以下代碼打印一些列表。 該列表正在打印,但是整數變量i的值不在打印。
<%
int i = 1;
try
{
foreach (LElement r in LElements)
{
Response.Write("<br/><div style=\"font-size:small\">");
Response.Write("Element "+i+"="+r.name);
Response.Write("</div>");
i++;
}
Response.Write("<br/>");
}
catch (Exception)
{
Response.Write("Error");
}
%>
它只是打印
元素= ABC
元素= XYZ
等等...
產生的HTML有點像這樣:
<br/><div style="font-size:small">Element = ABC</div>
<br/><div style="font-size:small">Element = XYZ</div>
<br/><div style="font-size:small">Element = PQR</div><br/>
請告訴我這段代碼有什么問題?
我已經嘗試過重現您的問題,但是不能,所以我猜測您的示例代碼與您的真實代碼有很大不同,或者正在發生更深的事情? 這就是我所做的並且可以正常運行,因此您可以以此為起點,告訴我們它是否比代碼更有效,並且可以突出顯示您所犯的任何錯誤?
<%
string[] data = { "ABC", "DEF", "GHI", "XYZ" };
int i = 1;
try
{
foreach(string item in data)
{
Response.Write("<br/><div style=\"font-size:small\">");
Response.Write(String.Format("Element {0} = {1}", i, item));
Response.Write("</div>");
i++;
}
Response.Write("<br/>");
}
catch (Exception)
{
Response.Write("Error");
}
%>
PS我已經將輸出包裝到String.Format
,這將確保將其正確轉換為字符串。
Response.Write(String.Format("Element {0} = {1}", i, r.name));
嘗試這個:
Response.Write(String.Format("Element {0} = {1}", i, r.name));
編輯: Zarathos領先於我:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.