如何將變量從asp.net傳遞到JavaScript?將變量從ASP.net傳遞到JavaScript
回答
您可以使用ASP.Net HiddenField。您只需在服務器上設置它的值,並在需要時通過javascript檢索它。
Serverside集團
hdf_Test.Value = "yourValue";
HTML
<asp:HiddenField runat="server" ID="hdf_Test" />
的Javascript
document.getElementById('hdf_Test').value
在服務器端這段代碼不起作用我試過字符串x; x.value = 「d」;它給我錯誤? – user1363918
這是因爲您正在嘗試使用不是HiddenField控件的變量。我用更多的信息更新了我的答案。 –
我知道我參加派對的時間已經很晚了,但是這幫了我很大的忙。 – user2366842
我有運氣使用JSON方法調用來調用我的C#方法。有在這個職位爲例 Using jQuery's getJSON method with an ASP.NET Web Form
我不知道答案,但,這可能是一個有用的鏈接,你:)(一)
有多種方式:
1 - 與<%= myVariable %>
2寫出來在你的JavaScript - 設置cookie的服務器端,然後檢索cookie的客戶端
3 - 設置一個隱藏的表單輸入您的值
4 - 將值重定向到頁面作爲查詢字符串參數,然後使用JavaScript解析參數
5 - 構建所有JavaScript服務器端,保存到變量,然後寫出變量client-側。
6 - 獲取與一個AJAX請求
有趣我不知道選項1. :) + 1 –
第一個選項導致VBScript運行時錯誤800a000d類型不匹配...至少當'myVariable'是一個經典的asp頁面中的記錄集變量時,比如'<%= rs(「myVariable」)%>' – Karapapas
價值創造背後
protected string MyProperty { get { return "your value"; } }
代碼中的屬性,然後在javascript
var myValue = "<%= MyProperty %>";
有趣的我對此不知道。 :) + 1 –
非常感謝你.... – user1363918
在HTML:
<script type="text/javascript">
alert(<%=Greetings()%>);
</script>
在後面的代碼:
protected string Greetings()
{
return Microsoft.Security.Application.AntiXss.JavaScriptEncode("Hello World!");
}
使用JavaScript標籤
< script> var var1 = @var1;
var var2 = @var2;
< /script>使用隱藏域
< input type="hidden" value="@var1" id="h_var1"/>
< input type="hidden" value="@var2" id="h_var2" />
在JS
$(function()
{
var var1 = $("#h_var1").val();
var var2 = $("#h_var2").val();
}
3.Retrieve使用JSON
var var1;
var var2;
$.get(url,function(result)
{
var1 = result.var1; var2 = result.var2;
}
@var語法通過AJAX數據取決於您的視圖引擎。這也許<%= VAR1%>
您可以在後面的代碼使用:
公共字符串JSON;
你需要給它一個值
在JavaScript中,你可以輸入:
<script>
var myVar = <%=json%>;
</script>
如果你想要得到的字符串變量相當於在你的代碼的一面,這是代碼:
實施例:
string jsString= JsEncoder.JavaScriptEncode("This is an example of C# string to be converted to javascript string",true));
類別代碼:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace stackoverlofw.JavascriptEncoder
{
public class JsEncoder
{
/// <summary>
/// Empty string for Java Script context
/// </summary>
private const string JavaScriptEmptyString = "''";
/// <summary>
/// Initializes character Html encoding array
/// </summary>
private static readonly char[][] SafeListCodes = InitializeSafeList();
/// <summary>
/// Encodes input strings for use in JavaScript.
/// </summary>
/// <param name="input">String to be encoded.</param>
/// <param name="emitQuotes">value indicating whether or not to emit quotes. true = emit quote. false = no quote.</param>
/// <returns>
/// Encoded string for use in JavaScript and does not return the output with en quotes.
/// </returns>
/// <remarks>
/// This function encodes all but known safe characters. Characters are encoded using \xSINGLE_BYTE_HEX and \uDOUBLE_BYTE_HEX notation.
/// <newpara/>
/// Safe characters include:
/// <list type="table">
/// <item><term>a-z</term><description>Lower case alphabet</description></item>
/// <item><term>A-Z</term><description>Upper case alphabet</description></item>
/// <item><term>0-9</term><description>Numbers</description></item>
/// <item><term>,</term><description>Comma</description></item>
/// <item><term>.</term><description>Period</description></item>
/// <item><term>-</term><description>Dash</description></item>
/// <item><term>_</term><description>Underscore</description></item>
/// <item><term> </term><description>Space</description></item>
/// <item><term> </term><description>Other International character ranges</description></item>
/// </list>
/// <newpara/>
/// Example inputs and encoded outputs:
/// <list type="table">
/// <item><term>alert('XSS Attack!');</term><description>'alert\x28\x27XSS Attack\x21\x27\x29\x3b'</description></item>
/// <item><term>[email protected]</term><description>'user\x40contoso.com'</description></item>
/// <item><term>Anti-Cross Site Scripting Library</term><description>'Anti-Cross Site Scripting Library'</description></item>
/// </list>
/// </remarks>
public static string JavaScriptEncode(string input, bool emitQuotes)
{
// Input validation: empty or null string condition
if (string.IsNullOrEmpty(input))
{
return emitQuotes ? JavaScriptEmptyString : string.Empty;
}
// Use a new char array.
int outputLength = 0;
int inputLength = input.Length;
char[] returnMe = new char[inputLength * 8]; // worst case length scenario
// First step is to start the encoding with an apostrophe if flag is true.
if (emitQuotes)
{
returnMe[outputLength++] = '\'';
}
for (int i = 0; i < inputLength; i++)
{
int currentCharacterAsInteger = input[i];
char currentCharacter = input[i];
if (SafeListCodes[currentCharacterAsInteger] != null || currentCharacterAsInteger == 92 || (currentCharacterAsInteger >= 123 && currentCharacterAsInteger <= 127))
{
// character needs to be encoded
if (currentCharacterAsInteger >= 127)
{
returnMe[outputLength++] = '\\';
returnMe[outputLength++] = 'u';
string hex = ((int)currentCharacter).ToString("x", CultureInfo.InvariantCulture).PadLeft(4, '0');
returnMe[outputLength++] = hex[0];
returnMe[outputLength++] = hex[1];
returnMe[outputLength++] = hex[2];
returnMe[outputLength++] = hex[3];
}
else
{
returnMe[outputLength++] = '\\';
returnMe[outputLength++] = 'x';
string hex = ((int)currentCharacter).ToString("x", CultureInfo.InvariantCulture).PadLeft(2, '0');
returnMe[outputLength++] = hex[0];
returnMe[outputLength++] = hex[1];
}
}
else
{
// character does not need encoding
returnMe[outputLength++] = input[i];
}
}
// Last step is to end the encoding with an apostrophe if flag is true.
if (emitQuotes)
{
returnMe[outputLength++] = '\'';
}
return new string(returnMe, 0, outputLength);
}
/// <summary>
/// Initializes the safe list.
/// </summary>
/// <returns>A two dimensional character array containing characters and their encoded values.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "This is necessary complexity.")]
private static char[][] InitializeSafeList()
{
char[][] allCharacters = new char[65536][];
for (int i = 0; i < allCharacters.Length; i++)
{
if (
(i >= 97 && i <= 122) || // a-z
(i >= 65 && i <= 90) || // A-Z
(i >= 48 && i <= 57) || // 0-9
i == 32 || // space
i == 46 || // .
i == 44 || // ,
i == 45 || // -
i == 95 || // _
(i >= 256 && i <= 591) || // Latin,Extended-A,Latin Extended-B
(i >= 880 && i <= 2047) || // Greek and Coptic,Cyrillic,Cyrillic Supplement,Armenian,Hebrew,Arabic,Syriac,Arabic,Supplement,Thaana,NKo
(i >= 2304 && i <= 6319) || // Devanagari,Bengali,Gurmukhi,Gujarati,Oriya,Tamil,Telugu,Kannada,Malayalam,Sinhala,Thai,Lao,Tibetan,Myanmar,eorgian,Hangul Jamo,Ethiopic,Ethiopic Supplement,Cherokee,Unified Canadian Aboriginal Syllabics,Ogham,Runic,Tagalog,Hanunoo,Buhid,Tagbanwa,Khmer,Mongolian
(i >= 6400 && i <= 6687) || // Limbu, Tai Le, New Tai Lue, Khmer, Symbols, Buginese
(i >= 6912 && i <= 7039) || // Balinese
(i >= 7680 && i <= 8191) || // Latin Extended Additional, Greek Extended
(i >= 11264 && i <= 11743) || // Glagolitic, Latin Extended-C, Coptic, Georgian Supplement, Tifinagh, Ethiopic Extended
(i >= 12352 && i <= 12591) || // Hiragana, Katakana, Bopomofo
(i >= 12688 && i <= 12735) || // Kanbun, Bopomofo Extended
(i >= 12784 && i <= 12799) || // Katakana, Phonetic Extensions
(i >= 19968 && i <= 40899) || // Mixed japanese/chinese/korean
(i >= 40960 && i <= 42191) || // Yi Syllables, Yi Radicals
(i >= 42784 && i <= 43055) || // Latin Extended-D, Syloti, Nagri
(i >= 43072 && i <= 43135) || // Phags-pa
(i >= 44032 && i <= 55215) /* Hangul Syllables */)
{
allCharacters[i] = null;
}
else
{
string integerStringValue = i.ToString(CultureInfo.InvariantCulture);
int integerStringLength = integerStringValue.Length;
char[] thisChar = new char[integerStringLength];
for (int j = 0; j < integerStringLength; j++)
{
thisChar[j] = integerStringValue[j];
}
allCharacters[i] = thisChar;
}
}
return allCharacters;
}
}
}
- 1. 將變量從Silverlight傳遞到ASP.Net
- 2. 將變量從javascript傳遞到php
- 3. 將變量值從Php傳遞到Javascript
- 4. AJAX將變量從javascript傳遞到PHP
- 5. 將變量從Ruby傳遞到JavaScript
- 6. 將變量值從javascript傳遞到ASP
- 7. 將變量值從javascript傳遞到php
- 8. 將變量從瓶子傳遞到javascript
- 9. 將變量從javascript傳遞到iMacros
- 10. 將值從PHP傳遞到Javascript變量
- 11. 將變量從Ajax傳遞到Javascript
- 12. 將變量從Javascript/JQuery傳遞到PHP
- 13. 將變量從javascript傳遞到php
- 14. 將變量從Android Activity傳遞到JavaScript
- 15. Asp.net從身體傳遞變量到iframe
- 16. 將變量從php傳遞給javascript
- 17. 將變量從java傳遞給javascript
- 18. 將變量從android傳遞給javascript
- 19. 將變量從javascript傳遞給href
- 20. 將變量傳遞給JavaScript
- 21. 將數組從javascript傳遞到ASP.NET
- 22. 將PHP變量傳遞到JavaScript
- 23. 將JavaScript變量傳遞到片段中
- 24. 將javascript變量傳遞到php
- 25. 將PHP變量傳遞到JavaScript
- 26. 將Golang變量傳遞到Javascript
- 27. 從javascript函數傳遞變量到php
- 28. 從Javascript傳遞動態變量到PHP
- 29. 變量不能從PHP傳遞到javascript
- 30. 從JavaScript傳遞變量到Objective-C - JSON?
你想做什麼?一個例子可能有幫助嗎? – Xharze
無論您使用哪種方法,請記得使用'Microsoft.Security.Application.AntiXss.JavaScriptEncode'來編碼您的字符串以使其更安全。 –