【问题标题】:C# script in html not reading the filehtml中的C#脚本不读取文件
【发布时间】:2013-02-07 10:23:45
【问题描述】:

我正在尝试获取一个文件,该文件的服务器名称由 htmnl 读取(使用 ac# 脚本进行读取),但我无法让脚本将名称传递给 html,这是代码:

<%@ Page Language="C#" MasterPageFile="~/MasterPage3.master" Title="Services" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <center>
        <script runat="server">
            string[] server = new string[] { };
            void Button1_Click(Object sender, EventArgs e)
            {

                string[] server = System.IO.File.ReadAllLines(@"C:\server.txt");  //here i get the name of the server
         return server[1];
            }
        </script>
        <br />
        <table width="600px" style="border-color: Silver; border-style: solid; border: 1">
            <tr>
                <td style="background-color: LightBlue; text-align: center">
                    <a href="Default.aspx?SERVER=1">1</a>
                </td>
            </tr>
            <tr>
                <td style="background-color: LightBlue; text-align: center">
                    <a href="Default.aspx?SERVER=2">2</a>
                </td>
            </tr>
            <tr>
                <td style="background-color: LightBlue; text-align: center">
                    <a href='Default.aspx?SERVER=' <%#server[1]%>>JD5PKF1</a>
                </td>
                //but i cant get it to be read here
            </tr>
            <input type="button" value="All in one" onclick="location.href='Option2.aspx';">
        </table>
    </center>
</asp:Content>

有什么想法吗?

【问题讨论】:

  • 我在这里看不到任何试图传递服务器名称的东西。你遇到了什么异常?您尝试过哪些方法将名称输入到 html 端?
  • &lt;center&gt; 标签?那已被弃用。使用 CSS。
  • 是的,我在发布此内容时忘记添加返回,基本上它返回了服务器 [1]; (我知道里面有东西

标签: c# asp.net html visual-studio


【解决方案1】:

除了别人报的错误,标签

<a href='Default.aspx?SERVER='<%#server[1]%>>

格式错误,应该是这样的

<a href='<%# "Default.aspx?SERVER=" + server[1] %>'>

并且它不检查 server[] 是否实际上在 index=1 处有一个元素。

【讨论】:

  • 这个更改确实有帮助,但它仍然没有被读取,我更改了代码,所以我将服务器名称硬编码到脚本中(基本上我说 server='server1',然后返回 server;但仍然它没有阅读它
  • 发现错误(至少在硬编码时)我在调用该方法时调用了变量
【解决方案2】:

您的网站无权访问您的硬盘。如果你想打开一个文件,你必须把它放在可以访问的目录中(例如把它和你的网站放在同一目录下)

【讨论】:

    【解决方案3】:

    您已经声明了两次服务器。

    您没有使用的第一个。 第二个你用文本填充,然后它就超出了范围。

    很难说你的意图是什么。

    【讨论】:

    • 如果我不定义它,它不会将脚本作为 c#
    猜你喜欢
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-05-30
    • 1970-01-01
    • 2014-10-26
    • 2014-02-24
    • 2013-04-20
    相关资源
    最近更新 更多