在 C# CodeBehind 中为 javascript 变量赋值

2024-04-06

如何从代码隐藏 (C#) 为 javascript 变量赋值?

<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;
}

背后的代码

 txtCode.Attributes.Add("Grp", Me.ConstValue)
    txtCode.Attributes.Add("onchange", "ConstantByCode(this," & DivTitle.ClientID & ");")
    txtCode.Attributes.Add("onkeyup", "ConstantByCode(this," & DivTitle.ClientID & ");")

_obj.Grp 现在具有价值。 警报说:未定义


我看到您想要检索自定义属性 Grp 的值。您需要使用 getAttribute 函数 - 所以而不是_Obj.Grp,你需要使用_Obj.getAttribute("Grp").

另外,我发现您没有将客户端 ID 括在 ode-behind 的引号中。所以而不是

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

你需要说

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

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

此外,ConstantByCode js 函数似乎正在采用 div 元素。因此,您需要向其中添加行以将客户端 id 转换为实际 DOM。 IE。

function ConstantByCode(_Obj, _Div) {
   _Div = document.getElementById(_Div);
   .... // rest of the code
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C# CodeBehind 中为 javascript 变量赋值 的相关文章

随机推荐