从代码隐藏中的 javascript 函数中检索值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31136426/
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
retrieve value from javascript function in codebehind
提问by Mo7ammed
How can I retrieve value from javascript function in codebehind, on page load .. javascript function like :
如何从代码隐藏中的 javascript 函数中检索值,在页面加载时.. javascript 函数,如:
<script type="text/javascript">
function isIFrame() {
var isInIFrame = (top.location != self.location);
if (isInIFrame) {
return "inside";
}
else {
return "outside";
}
}
</script>
and code behind like :
和后面的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
string resutOfExecuteJavaScript = "";
// resutOfExecuteJavaScript = isIFrame(); // from javascript
if (resutOfExecuteJavaScript == "inside")
{
// do something
}
else
{
// do something
}
}
thank you.
谢谢。
回答by Midhun Murali
You cannot directly call a client side javascript method from server side code . For that first you need to assign the function result to value of some hidden variable and then access it in server side
您不能从服务器端代码直接调用客户端 javascript 方法。为此,您首先需要将函数结果分配给某个隐藏变量的值,然后在服务器端访问它
Suppose you have an hidden field like this
假设你有一个像这样的隐藏字段
<input type="hidden" runat="server" id="hdnVal"/>
then you can set the value as below
然后你可以设置如下值
document.getElementById("hdnVal").value=isIFrame();
then at serve side
然后在发球区
string resutOfExecuteJavaScript = hdnVal.Value;
回答by Rojalin Sahoo
using _doPostBack, you can solve this one
使用_doPostBack,你可以解决这个问题
<script type="text/javascript">
function isIFrame() {
var isInIFrame =(top.location != self.location);
var result;
if (isInIFrame) {
result="inside";
}
else
{
result ="outside";
}
__doPostBack('callPostBack', result);
</script>
</head>
In code behind section
在代码后面部分
protected void Page_Load(object sender, EventArgs e)
{
this.ClientScript.GetPostBackEventReference(this, "arg");
if (IsPostBack)
{
string eventTarget = this.Request["__EVENTTARGET"];
string eventArgument = this.Request["__EVENTARGUMENT"];
if (eventTarget != String.Empty && eventTarget == "callPostBack")
{
if (eventArgument == "inside"){
//do something
}
else if(eventArgument == "outside")
{
//do something
}
}
else
{
// set the button click
btnclick.Attributes.Add("onClick", "isIFrame();");
}
}
Below link will help you out to get more idea.
下面的链接将帮助您获得更多想法。
回答by ahmadreza ranjbar
in javascript file or your script add :
在 javascript 文件或您的脚本中添加:
function SetHiddenVariable()
{
document.getElementById(inpHide).value= "value";
}
in .aspx add this tag:
在 .aspx 中添加这个标签:
<input id="inpHide" type="hidden" runat="server" />
in aspx.cs (c# file) add :
在 aspx.cs(c# 文件)中添加:
anyVariable = inpHide.Value;