簡體   English   中英

如何在JavaScript中獲取服務器端變量

[英]How to get a server-side variable into JavaScript

我想從數據庫(使用asp.net(C#)中的datareader)將值傳遞給此JavaScript。

該腳本來自http://www.dynamicdrive.com/dynamicindex14/leftrightslide.htm

我需要在下面的行中設置通過值:

leftrightslide[cnt] = '<a href="MYWEBPAGE"><img src="MYIMAGE FRM DATABASE.gif" border=1></a>';
cnt++;

因此,數據讀取器的值將傳遞到JavaScript數組中。

請參閱以下內容:
http://www.velocityreviews.com/forums/t76971-populating-javascript-array-from-vb-net.html
使用datareader在服務器端創建字符串並進行注冊。

要么

從服務器端使用RegisterArrayDeclaration(arrayName,arrayValue)。 參見以下內容:
http://msdn.microsoft.com/en-us/library/aa479302.aspx

您可以使用網絡服務

您必須使用腳本管理器http://msdn.microsoft.com/en-us/library/system.web.ui.page.clientscript.aspx來執行此操作。 檢查鏈接會有所幫助。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
        StringBuilder cstext1 = new StringBuilder();
        cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
        cstext1.Append("script>");

        cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RegisterStartupScript</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM