如何分享C#和JavaScript之间的变量? [英] How to share a variable between C# and JavaScript?
本文介绍了如何分享C#和JavaScript之间的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想分享的C#和JavaScript之间的 currentTab
变量。这里是我的code:
I want to share the currentTab
variable between C# and JavaScript. Here is my code:
C#:
public int currentTab = 1;
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "showTab(" + currentTab + ");", true);
}
JavaScript的:
var currentTab = "<%=currentTab%>";
function showTab(index)
{
currentTab = index;
// Show tab at (index)
}
我用这种办法来在回发再次显示当前选项卡。然而, currentTab
在C#中剩余的 1
在回传
。我怎样才能解决这个问题呢?
I used this approach to show the current tab again on PostBack. However, currentTab
on C# is remaining 1
after PostBack
. How can I solve this issue?
推荐答案
您可以从您的javascript函数写的指数,到hiddenfield,然后阅读上回发。
You can write the index from your javascript function, to a hiddenfield, and then read that on postback.
在您的code,你检查hiddenfield,如果你的页面是回发。
像这样:
In your code, you check the hiddenfield, if your page is postback. Like so:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
currentTab = Int32.Parse(HiddenTabValue.Value);
}
}
这篇关于如何分享C#和JavaScript之间的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文