Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?


C#
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
C#
document.getElementById('<%=txtTotalSplit.ClientID %>').value = parseFloat(total.toString()).toString();
Posted

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. 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. 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.
 
Share this answer
 
Comments
virang_21 1-Sep-10 2:49am    
txtTotalSplit is showing proper value on client side. which means there is no issue with javascript setting its value. But it has "" as its value on postback. I explicitly set its EnableViewState="true" ...

I checked with setting txtTotalSplit value to constant as you mentioned like '1000' but still same result...

<asp:textbox id="txtTotalSplit" runat="server" enabled="False" enableviewstate="true">
refer my code and below code working fine myend

javascript code

C#
function setTextDynamically() 
{
var total = 100;
document.getElementById('<%=txtTest.ClientID %>').value = parseFloat(total.toString()).toString();
        }


html code
XML
<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
C#
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);
    }
 
Share this answer
 
First, in the code you provided, the Enabled="false" is not included.

Second, if the Enabled="false" is included the Server control, the following HTML is renderered

<input type="text" disabled="disabled" id="txtTest" name="txtTest">


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.
 
Share this answer
 
check page load method whether you have set the textbox's value blank or not
 
Share this answer
 
Comments
virang_21 1-Sep-10 2:36am    
I am not setting its value anywhere in code behind ...
virang 1-Sep-10 2:49am    
i have subitted my code in below answer please check
Found the answer.. Because it was disabled it wasn't showing the value on server side. I changed Textbox's Enabled="false" property to true and now it is working ... Though I am not sure about why it behave that way .. Anything to do with ViewState ? Any rational ? ....
 
Share this answer
 
Comments
Hiren solanki 1-Sep-10 3:00am    
if virang has helped you then mark as a answer.
shineskollam 24-Jan-13 5:32am    
how you get? still i have the same problem. i change my texbox's Enabled=true
I had similar issue, when I added a asp:TextBox to a complex SharePoint 2010 page.
The javascript to set value off the asp:TextBox worked fine,
however on server-side, the TextBox.Text property was always empty.

Cause: when I viewed the source of the page in browser, I could see that although there was a Form on the page, the TextBox was sitting outside of the Form element.

Solution: I moved the TextBox to a ContentPane where I knew it would appear inside the Form.

the values then appeared OK on server side.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900