chatGPT is unable to answer: why VBScript's do until … loop can be written in multiple<% %> code blocks?

huangapple go评论68阅读模式
英文:

chatGPT is unable to answer: why VBScript's do until ... loop can be written in multiple<% %> code blocks?

问题

VBScript的do until ... loop是在多个<% %>代码块中编写的。

最近,我在一个.aspx文件中阅读了上面的这段代码块。这是一个需要维护的旧项目。

我对代码的风格感到惊讶。因为*do until ... loop*是在多个<%%>块中编写的,以插入更多<option>标签。这意味着<%%>块打破了完整的代码逻辑。

我已经搜索了解释,但在谷歌上还没有找到任何信息。我曾向ChatGPT提问,得到了代码的作用,但没有更深入的解释。

我想知道是否有人了解为什么代码可以被写成多个<%%>代码块,以及它是如何工作的,以及更深入的解释。
英文:

VBScript do until ... loop being written in multiple <% %> code blocks.

&lt;select id=&quot;TaskID&quot; name=&quot;TaskID&quot; onChange=&quot;javascript: changeReportType(); OnClientsByAdviserReportSelected();&quot;&gt;
    &lt;% prReports.MoveFirst()
    Do Until prReports.AtEnd()
        If CastDec(prReports(&quot;Requestable&quot;)) = 1D Then %&gt;
            &lt;option &lt;% If Convert.ToString(pnTaskID) = prReports(&quot;TaskID&quot;) Then %&gt; selected &lt;% End If %&gt; 
            value=&quot;&lt;%: prReports(&quot;TaskID&quot;).ToString &amp; &quot;-&quot; &amp; prReports(&quot;ReportName&quot;).ToString %&gt;&quot;&gt;
                &lt;%: prReports(&quot;DisplayName&quot;) %&gt;
            &lt;/option&gt;
        &lt;% End If
        prReports.MoveNext()
    Loop %&gt;
&lt;/select&gt;

Recently, I've read this code block above in a .aspx file. This is an old project that need to be maintaining.
I was astonished by the style of the code. Because the do until ... loop is written in multiple <%%> blocks for inserting more <option> labels. That means the <%%> blocks break the complete code logic. I've searching for the explanation, but I got nothing on google yet. I've asked chatGPT and got the answer what the code doing but no more in-depth explanation. I wonder there are some one understand why the code can be written into multiple <%%> code blocks.

how does it work and its in-depth explanation.

答案1

得分: 0

这是一个WebForms应用程序吗?

如果是这样的话,那看起来是vb.net代码。

实际上,就像Pascal、C#或其他语言一样,把<%看作是代码块的开始,%>表示代码块的结束。

所以,我可以编写一些服务器端代码,放在标记中,像这样:

<% 
    Dim i As Integer
    Dim j As Integer = 1
    For i = 1 To 10
        Response.Write(i & "<br/>")
        j = j + 5
    Next

    Response.Write("<br/>")
    Response.Write("<br/>")
    Response.Write("<br/>")
    Response.Write("Total for j = " & j)
%>

在上面的代码中有一些<br/>等标记。对于更复杂的标记,通常在vb.net中编写这样的代码会变得"困难"。

因此,一些标记可以保留为客户端标记(任何不在<% server code here %>的开放+关闭标记之内的内容都是客户端标记)。

所以,上面的代码可以这样编写:

<% Dim i As Integer %>
<% Dim j As Integer = 1 %>
<% For i = 1 To 10 %>
    <%= i %><br />
    <%  j = j + 5 %>
<% Next %>
<br />
<br />
<br />
Total for j = <%= j %>

在上述代码中,我选择有"更多"的标记(客户端端标记),因此我必须开始服务器端代码,关闭服务器代码标记,然后接下来是常规的客户端标记。

上面的两个示例都产生相同的结果。

请记住,无论是在C#还是vb.net中编写,您都可以这样做。

但是,我认为Web Forms标记已经相当拥挤了,因此在有一个干净的代码后台代码模块时,很少有理由在标记中混合使用服务器代码。

因此,在aspx页面的标记部分编写代码块的情况非常少见。然而,在标记中使用表达式是一种常见的做法。

请注意,如果这是一个C#应用程序,您也可以执行上述操作。

因此,在使用C#时,上面的代码可以这样编写:

<% int i;%>
<% int j = 1;%>
<% for (i = 1;i <= 10;i++)
    {
        Response.Write(i.ToString()); %><br />
<%      j = j + 5;
    }
 %>
<br />
<br />
<br />
Total for j = <%= j %>

因此,您不仅限于在vb.net中使用上述方法,当使用C#编写代码时,也有相同的方法和选项。

英文:

Is this a webforms application?

That being the case, then that looks to be vb.net code.

Really, like Pascal, C# or whatever? Think of the "<%" as a starting of a code block, and the "%>" as the end of a code block.

So, I could write some server-side code, place it in the markup like this:

        &lt;% 
            Dim i As Integer
            Dim j As Integer = 1
            For i = 1 To 10
                Response.Write(i &amp; &quot;&lt;br/&gt;&quot;)
                j = j + 5
            Next

            Response.Write(&quot;&lt;br/&gt;&quot;)
            Response.Write(&quot;&lt;br/&gt;&quot;)
            Response.Write(&quot;&lt;br/&gt;&quot;)
            Response.Write(&quot;Total for j = &quot; &amp; j)

        %&gt;

In above have some br's etc. With more complex markup, then it often becomes “difficult" to write such code in vb.net.

So, some of the markup can remain client-side markup. (Anything outside of an open + closing tag of <% server code here %> then client-side markup follows.

So, above could be written like this:

&lt;% Dim i As Integer %&gt;
&lt;% Dim j As Integer = 1 %&gt;
&lt;% For i = 1 To 10
        Response.Write(i) %&gt; &lt;br/&gt;
&lt;%  j = j + 5 %&gt;
&lt;%  Next %&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Total for j = &lt;% Response.Write(j) %&gt;

So, in above, I choose to have "more" markup (client side), and thus I have to start server-side code, close the server code tag, and what follows is now regular client-side markup.

Both of above examples produces the same results:

chatGPT is unable to answer: why VBScript's do until … loop can be written in multiple<% %> code blocks?

Keep in mind, you can do this when writing c#, or vb.net.

However, I think web forms markup is already rather crowded, and thus very little reasons exists to inter-mix server code in markup when you have a nice clean code behind code module.

Thus rare does one need to write code blocks in the markup section of an aspx page. However, using expressions in the markup is a common practice.

Note that you can do the above if this is a C# application.

So, above could be written as this when using C#:

&lt;% int i;%&gt;
&lt;% int j = 1;%&gt;
&lt;% for (i = 1;i &lt;= 10;i++)
    {
        Response.Write(i.ToString()); %&gt; &lt;br /&gt;
&lt;%      j = j + 5;
    }
 %&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Total for j = &lt;% Response.Write(j); %&gt;

So, you not limited to using above with vb.net, and the same approach and option is available when writing code in C#.

huangapple
  • 本文由 发表于 2023年7月28日 01:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定