文本框值在服务器端不可用。 [英] Textbox value not available on server side.
问题描述
我通过javascript为ASP.NET页面设置了Textbox的值,但在回发中,该值显示为空字符串。我没有禁用ViewState。我试图在服务器上获取的文本框的值是txtTotalSplit。我正在使用以下javascript代码来设置它。
有什么建议吗?
I am setting a value of Textbox via javascript for ASP.NET page but in postback the value is showing empty string. I haven''t disabled ViewState .The textbox''s value I am trying to get on the server is of txtTotalSplit. I am using following javascript code to set it.
Any suggestion ?
function totalSplit() {
var cash = document.getElementById('<%=txtCash.ClientID %>').value;
var creditcard = document.getElementById('<%=txtCreditCard.ClientID %>').value;
var eft = document.getElementById('<%=txtEFT.ClientID %>').value;
var amax = document.getElementById('<%=txtAMEX.ClientID %>').value;
var cheque = document.getElementById('<%=txtCheque.ClientID %>').value;
var total=0;
if (!(cash == "")) total += parseFloat(cash.toString());
if (!(creditcard == "")) total += parseFloat(creditcard.toString());
if (!(eft == "")) total += parseFloat(eft.toString());
if (!(amax == "")) total += parseFloat(amax.toString());
if (!(cheque == "")) total += parseFloat(cheque.toString());
document.getElementById('<%=txtTotalSplit.ClientID %>').value = parseFloat(total.toString()).toString();
}
以下是否有任何问题
Is Anything wrong with following
document.getElementById('<%=txtTotalSplit.ClientID %>').value = parseFloat(total.toString()).toString();
推荐答案
您可以按如下方式调试:
1.在下一行中设置一个固定值,看看你是否在PostBack中得到了值:
You can debug this as follows:
1. Set a fixed value in the following line and see whether you get the value in PostBack:
document.getElementById(''<%=txtTotalSplit.ClientID %>'').value = ''100'';
2.如果您获得了价值,这意味着您在以下代码中遇到问题,并且您需要修复它。
2. If you get the value, that means, you have problem in the following code, and, you need to fix it.
parseFloat(total.toString()).toString();
3.如果您没有得到该值,请确保以下代码返回正确找到文本框:
3. If you don''t get the value, make sure the following code returns finds a textbox correctly:
document.getElementById(''<%=txtTotalSplit.ClientID %>'')
此外,请确保以下方法是正确执行没有任何错误:
And also, make sure the following method is executed correctly without any error :
totalSplit()
希望你的问题能得到解决。
Hopefully, your problem will be solved.
参考我的代码和下面的代码工作正常myend
javascript代码
refer my code and below code working fine myend
javascript code
function setTextDynamically()
{
var total = 100;
document.getElementById('<%=txtTest.ClientID %>').value = parseFloat(total.toString()).toString();
}
html代码
html code
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:Button id="btnTest" runat="server" Text="Set Text Box" OnClientClick="setTextDynamically();" />
<asp:Button ID="btnGet" runat="server" Text="Get Value" />
代码背后
code behind
protected void Page_Load(object sender, EventArgs e)
{
btnGet.Click += new EventHandler(btnGet_Click);
}
void btnGet_Click(object sender, EventArgs e)
{
Response.Write(txtTest.Text);
}
首先,在您提供的代码中,Enabled =false
不包括在内。
其次,如果Enabled =false
包含在Server控件中,则以下呈现HTML
First, in the code you provided, theEnabled="false"
is not included.
Second, if theEnabled="false"
is included the Server control, the following HTML is renderered
<input type="text" disabled="disabled" id="txtTest" name="txtTest">
而且,在这种情况下,用户也无法修改文本框的值,更不用说JavaScript了。
所以,这是问题的主要原因。
And, in this case, user also cannot modify the value of the text box, let alone the JavaScript.
So, this was the main reason of the problem.
这篇关于文本框值在服务器端不可用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!