为什么会这样呢? [英] Why is this happening
问题描述
为什么当我将数据保存到此JavaScript的标签时!
why when i save data to a label from this JavaScript!
args = 199;
function myfunction(command, args) {
switch (command) {
case "CMD_Data":
document.getElementById("lblMyScore").textContent = args;
default:
}
}
数据在标签中显示为199!
the data shows in the label as 199 here!
lblMyScore.Text;
但是当我点击按钮时,它不会看到这里的数据代码背后!
but it will not see that data here when i click the button from the code behind!
protected void btmSave_Click(object sender, EventArgs e)
{
var codeBehindValue = lblMyScore.Text;
}
它看到的是来自页面的数据Text =0?
< asp:Label ID =lblMyScorerunat =serverText =0Font-Bold =TrueFont-Size =XX-LargeForeColor =#FF3300 >
好吧我试过它有点适用于我试图做但你带走按钮!并使用,
it see,s the data from the page Text="0"?
<asp:Label ID="lblMyScore" runat="server" Text="0" Font-Bold="True" Font-Size="XX-Large" ForeColor="#FF3300">
OK i tried that it kinda works for what im trying to do but u take away the button! and use,
protected void HiddenField1_ValueChanged(object sender, EventArgs e)
{
var codeBehindValue = HiddenField1.Value;
}
页面不刷新所以价值仍然没有任何想法!
试图在没有按钮的情况下自动保存数据!
the page dont refresh so the values are still not there any thoughts!
trying to auto save the data with no button!
推荐答案
当按钮时单击,页面回发。因此,标签
进入初始状态。如果您通过 JavaScript 存储任何内容,它就会丢失。
而是在HiddenField上设置值
并在 Code Behind 中获取HiddenField
值。
When Button is Clicked, Page is Posted Back. So,Label
gets to its initial state. If you store anything on it by JavaScript, it is lost.
Instead set value on aHiddenField
and get theHiddenField
value in Code Behind.
Tadit Dash是对的。看看这个例子:
我可以在Asp.Net代码中获取Javascripts值 [ ^ ]
Tadit Dash is right. Look at this example:
Can I Get Javascripts Values In Asp.Net Code[^]
这篇关于为什么会这样呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!