将值分配给javascript变量在C#代码隐藏 [英] assign value to javascript variable In C# CodeBehind

查看:138
本文介绍了将值分配给javascript变量在C#代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分配值从后台代码(C#)javasctipt变量

How can i assign value to javasctipt variable from code-behind (C#)?

<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

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已现值。
警告说:未定义

_obj.Grp has now value. alert said : undefined

推荐答案

我看到你要检索GRP的值是一个自定义属性。您需要使用的getAttribute功能 - 所以不是 _Obj.Grp ,您需要使用 _Obj.getAttribute(GRP)

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").

另外,我看你是不是从ODE隐藏引号引起客户端ID。因此,而不是

Also, I see that you are not enclosing client id in quotes from ode-behind. So instead of

txtCode.Attributes.Add("onchange", "ConstantByCode(this," & DivTitle.ClientID & ");")

您需要说

txtCode.Attributes.Add("onchange", "ConstantByCode(this,'" & DivTitle.ClientID & "');")

请注意单引号(')周围的客户端ID。

Note the single quote(') around the client id.

此外,ConstantByCode js函数似乎正在div元素。因此,你需要添加一行到它从客户端ID转换成实际的DOM。 。即

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.

function ConstantByCode(_Obj, _Div) {
   _Div = document.getElementById(_Div);
   .... // rest of the code

这篇关于将值分配给javascript变量在C#代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆