简体   繁体   English

无法使用Response.Write()打印整数值

[英]Unable to print integer value using Response.Write()

I have following code that prints some list. 我有以下代码打印一些列表。 The list is printing but the value of integer variable i is not printing. 该列表正在打印,但是整数变量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");
}
%>

Its just printing 它只是打印

Element = ABC 元素= ABC

Element = XYZ 元素= XYZ

and so on... 等等...

Resulting HTML is somewhat like this: 产生的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/>

Please tell me what is wrong with this code? 请告诉我这段代码有什么问题?

I've tried reproducing your issue and can't so I am guessing either your sample code is quite different from your real code or something deeper is going on? 我已经尝试过重现您的问题,但是不能,所以我猜测您的示例代码与您的真实代码有很大不同,或者正在发生更深的事情? This is what I've done and works OK so you could take this as a starting point and tell us if it works any more than your code and might highlight any mistakes you have made? 这就是我所做的并且可以正常运行,因此您可以以此为起点,告诉我们它是否比代码更有效,并且可以突出显示您所犯的任何错误?

<%    
    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 I've wrapped the output into a String.Format which will ensure it gets converted to a string correctly. PS我已经将输出包装到String.Format ,这将确保将其正确转换为字符串。

Response.Write(String.Format("Element {0} = {1}", i, r.name));

Try this: 尝试这个:

Response.Write(String.Format("Element {0} = {1}", i, r.name));

Edit: Zarathos was ahead of me :) 编辑: Zarathos领先于我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM