簡體   English   中英

C#-將C#變量傳遞給JavaScript

[英]C# - Passing C# Variable to JavaScript

我在C#中有以下代碼行:

Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx?num=<%=trans_number%>', '_newtab')", true);

現在,trans_number是我的C#代碼背后的變量。 我的問題是,當我處理查詢字符串(num變量)時,結果始終是:

<%trans_number%>

而不是該變量的內容。 我該如何解決?

我認為,您可以輕松解決此問題……也許:

Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Transaction_Number.aspx?num=" + trans_number + "', '_newtab')", true);

您有一個字符串,該字符串與變量連接,然后又嵌入另一個字符串中。 使用中間變量和String.Format方法時,此類問題往往會消失:

string url = String.Format(@"http://localhost:4000/Transaction_Number.aspx?num={0}", trans_number);
string js = String.Format("window.open('{0}', '_newtab')", url);

Page.ClientScript.RegisterStartupScript(Page.GetType(), null, js, true);

暫無
暫無

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

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