在 C# CodeBehind 中为 javascript 变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3655766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assign value to javascript variable In C# CodeBehind
提问by Shahin
How can i assign value to javasctipt variable from code-behind (C#)?
如何从代码隐藏 (C#) 中为 javasctipt 变量赋值?
<script type="text/javascript">
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/, ''); };
function ConstantByCode(_Obj, _Div) {
var pl = new SOAPClientParameters();
_Obj.value = _Obj.value.trim();
pl.add("Code", _Obj.value);
pl.add("Group", _Obj.Grp);
alert(_Obj.Grp);
var _Value = SOAPClient.invoke("ConstantWS.asmx", "GetConstantByCode", pl, false, CallBackEvent);
if (_Value == null || _Obj.value == "" || _Obj.value == null || IsNumeric(_Obj.value) == false) {
_Obj.value = "";
_Div.innerHTML = "";
}
else {
_Div.innerHTML = _Value;
}
}
function CallBackEvent(r) {
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
BehindCode
背后的代码
txtCode.Attributes.Add("Grp", Me.ConstValue)
txtCode.Attributes.Add("onchange", "ConstantByCode(this," & DivTitle.ClientID & ");")
txtCode.Attributes.Add("onkeyup", "ConstantByCode(this," & DivTitle.ClientID & ");")
_obj.Grp has now value. alert said : undefined
_obj.Grp 现在有价值。警报说:未定义
采纳答案by VinayC
I see that you want to retrieve value of Grp that is a custom attribute. You need to use getAttribute function - so instead of _Obj.Grp, you need to use _Obj.getAttribute("Grp").
我看到您想要检索作为自定义属性的 Grp 值。您需要使用 getAttribute 函数 - 因此_Obj.Grp,您需要使用_Obj.getAttribute("Grp").
Also, I see that you are not enclosing client id in quotes from ode-behind. So instead of
另外,我发现您没有将客户端 ID 括在来自 ode-behind 的引号中。所以代替
txtCode.Attributes.Add("onchange", "ConstantByCode(this," & DivTitle.ClientID & ");")
you need to say
你需要说
txtCode.Attributes.Add("onchange", "ConstantByCode(this,'" & DivTitle.ClientID & "');")
Note the single quote(') around the client id.
请注意客户端 ID 周围的单引号 (')。
Further, ConstantByCode js function appears to be taking div element. Hence, you need to add line to it for converting from client id to actual DOM. i.e.
此外,ConstantByCode js 函数似乎采用了 div 元素。因此,您需要向其中添加行以将客户端 ID 转换为实际 DOM。IE
function ConstantByCode(_Obj, _Div) {
_Div = document.getElementById(_Div);
.... // rest of the code
回答by Elixir
Firstly you will need to have access to the value on the client. We can do this by storing the value in a hiddenfield or by adding an attribute to the control. It seems you wish to do this by using an attribute so lets do this first.
首先,您需要访问客户端上的值。我们可以通过将值存储在隐藏字段中或通过向控件添加属性来做到这一点。似乎您希望通过使用属性来执行此操作,因此让我们先执行此操作。
add the following to your page_load method so we have access to the C# value on the client.
将以下内容添加到您的 page_load 方法中,以便我们可以访问客户端上的 C# 值。
protected void Page_Load(object sender, EventArgs e) { string requiredJSValue = "put your value here"; txtCode.Attributes.Add("CSCodeAttribute", requiredJSValue); }
protected void Page_Load(object sender, EventArgs e) { string requiredJSValue = "把你的值放在这里"; txtCode.Attributes.Add("CSCodeAttribute", requiredJSValue); }
We then need to access this value through Javascript. Firstly we will need to get the client ID of the control as C# will set this value. Note. I am using Jquery to retrieve the control ID. This is not required, however I prefer it. Jquery is a framework for javascript and can be downloaded from www.jquery.com
然后我们需要通过 Javascript 访问这个值。首先,我们需要获取控件的客户端 ID,因为 C# 将设置此值。笔记。我正在使用 Jquery 来检索控件 ID。这不是必需的,但我更喜欢它。jquery是一个javascript框架,可以从www.jquery.com下载
function GetCSAttributeValue() { var csControlID = $('#<%= txtUOMCost.ClientID %>'); //Gets the control name. var requiredJSValue = csControlID .attr("CSCodeAttribute"); //Value stored in variable. }
function GetCSAttributeValue() { var csControlID = $('#<%= txtUOMCost.ClientID %>'); //获取控件名称。var requiredJSValue = csControlID .attr("CSCodeAttribute"); //值存储在变量中。}
回答by Sidharth Panwar
I'm not 100% sure but I think you'll need a workaround to get this working. Because logically at the backend the javascript variable doesn't even exist. You can probably create a hidden field and make it a bridge between the javascript variable and code behind. Check this: http://forums.devx.com/showthread.php?t=164356
我不是 100% 确定,但我认为您需要一种解决方法才能使其正常工作。因为在后端逻辑上,javascript 变量甚至不存在。您可能可以创建一个隐藏字段,并使其成为 javascript 变量和背后代码之间的桥梁。检查这个:http: //forums.devx.com/showthread.php?t=164356
Try this: 1. Add a hidden field in you .aspx page:
试试这个: 1. 在你的 .aspx 页面中添加一个隐藏字段:
<asp:HiddenField ID="hidden" runat="server" />
2. Change the value of this field in your code-behind:
2. 在代码隐藏中更改此字段的值:
protected void Page_Load(object sender, EventArgs e)
{
hidden.Value = "hello";
}
3. Write the following script to access the value and put it in any variable:
3. 编写以下脚本来访问该值并将其放入任何变量中:
<script type="text/javascript">
if (document.getElementById("MainContent_hidden") != undefined) {
var hiddenVal = document.getElementById("MainContent_hidden").value;
}
else {
var hiddenVal = null;
}
</script>
WARNING: The third part is tricky. We are not using the same ID that we provided in the 1st step when we are calling the getElementById function. This is because asp.net changes this and the temporary workaround is to run the page once and view its source. Check the id of the hidden field and put it in step 3 inside the getElementById function. You can look for better alternatives but for now use this if you want. If you're struck at step 3, let me know.
警告:第三部分很棘手。当我们调用 getElementById 函数时,我们使用的 ID 与我们在第一步中提供的 ID 不同。这是因为 asp.net 改变了这一点,临时解决方法是运行一次页面并查看其源代码。检查隐藏字段的 id 并将其放在 getElementById 函数中的步骤 3 中。您可以寻找更好的替代品,但现在如果您愿意,请使用它。如果您在第 3 步中被击中,请告诉我。
回答by Carlos Mu?oz
I don'see how your question is related to the code...
But to set a value of a javascript value from serverside... well you can't, because server side code runs precisely in the server and way before the HTML goes to the client and javascript gets executed.
我不明白你的问题与代码有什么关系......
但是要从服务器端设置一个 javascript 值的值......好吧你不能,因为服务器端代码在 HTML 运行之前精确地在服务器中运行到客户端并执行 javascript。
But what you can do is make your server side code generate a piece of javascript that holds your value.
但是您可以做的是让您的服务器端代码生成一段 javascript 来保存您的价值。
<script type="text/javascript">
var x = <%= ServerSideMethod() %>;
</script>

