简体   繁体   English

调用Java脚本返回值到asp.net中的代码页

[英]calling java script return value to code page in asp.net

i want to call my java script function return value to my code 我想调用我的Java脚本函数返回值到我的代码
i have a function like this..(this i wrote in my java script) 我有一个这样的功能..(这是我在我的Java脚本中写的)

function GetSex()
{

    var sex = EIDAWebComponent.GetSex();
    if(sex == 'M')
        return "Male";
    if(sex == 'F')
        return "Female";
    if(sex == 'X')
        return "Unknown";
}

my code: 我的代码:

<td>
    <span>Sex:&nbsp;</span>
</td>
<td>
    <span id="Sex_PDLabel" runat=server></span>
</td>

how i can call this return value in my code? 如何在我的代码中调用此返回值? i want to get corresponding value to any variable. 我想获取任何变量的对应值。 so try to write code something like this: 因此,尝试编写如下代码:

 ScriptManager.RegisterStartupScript(DirectCast(HttpContext.Current.Handler, Page), GetType(Page), "GetSEx", "GetSex()", True)
 Dim SexVAl = Sex_PDLabel.InnerText

but my sexVal Always getting empty string.i dont know why? 但我的sexVal总是变得空字符串。我不知道为什么? what is the problem with my code? 我的代码有什么问题?

any help is very appriciable.. 任何帮助都是非常适用的。

Use this. 用这个。

aspx: Create a hidden field to store the value. aspx:创建一个隐藏字段来存储值。

<input type="hidden" id="txtHidden" runat="server" />

javascript code: JavaScript代码:

function GetSex()
{
    var sex = EIDAWebComponent.GetSex();
     alert(sex);
    if(sex == 'M')
        document.getElementById("txtHidden").value=  "Male";
    else if(sex == 'F')
        document.getElementById("txtHidden").value =  "Female";
    else if(sex == 'X')
        document.getElementById("txtHidden").value =  "Unknown";
    else
        document.getElementById("txtHidden").value =  "none";
}

Call from ASP.NET code: 从ASP.NET代码调用:

ScriptManager.RegisterStartupScript((Page)(HttpContext.Current.Handler), 
                  typeof(Page), "GetSex", "GetSex();", true);

String value = txtHidden.Value;

One of the approaches is to store the value in a hidden field . 一种方法是将值存储在隐藏字段中

Add following to your ASP.NET markup: 将以下内容添加到您的ASP.NET标记中:

<asp:HiddenField id="hdnField" runat="server" />

and call: 并致电:

document.getElementById('<%= hdnField.ClientID %>').value = GetSex();

In codebehind the value will be accessible as: 在后面的代码中,可以通过以下方式访问该值:

var val = hdnField.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM