无法在Javascript中访问Codebehind值 [英] Codebehind value not accessed in Javascript
本文介绍了无法在Javascript中访问Codebehind值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在javascript中具有以下功能:
window.onunload = unloadPage;
Have a function in javascript as :
window.onunload = unloadPage;
function unloadPage()
{
var abc= <%=Price %>
alert(abc)
}
Price是代码背后的变量,其值在Datagrid OnPageIndexChanged事件中设置.
VB.NET代码是:
公开价格为字符串
}
Price is the variable in codebehind and its value is set in Datagrid OnPageIndexChanged event.
VB.NET Code is:
Public Price As String
Protected Sub dgttumfileview_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgttumfileview.PageIndexChanged
Price = "True"
End Sub
但是我在警报中获得未定义"的价格.如何获得在代码隐藏中设置的"True"值.
But I''m getting value of Price as ''undefind'' in alert. How can I get value as "True" which is set in codebehind.
推荐答案
有时,在后面的代码中设置的值不会出现在javascript中.因此,我建议使用隐藏字段而不是变量.
在源中添加一个隐藏字段,并在后面的代码中设置值并从javascript函数中读取.
Hi,
some times the values set in code behind will not appear in javascript. so, i suggest to use hidden field instead of variable.
add one hidden field to the source and set the value in code behind and read from javascript function.
function unloadPage()
{
var abc= document.getElelmentById('hdfID').value;
//hdfID is the id of hidden field.
alert(abc)
}
希望对您有所帮助.
hope it helps.
这篇关于无法在Javascript中访问Codebehind值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文