locked
Values set by javascript are not reflected in serverside(.Net) RRS feed

  • Question

  •  

    Hi all

    I am a new user & learner of .NET and this is my first question.
    I am entering the values into a listbox using java script logic.
    But when post back occurs because of another button control(not save button) , all the values entered by javascript into listbox disappears. But I have to save those values into database using save button.

    java script is working fine.But because of postback those values are disappearing. Please suggest me a solution

    Tuesday, November 27, 2007 1:15 PM

Answers

  • Hi,

     

    Yes. All the values entered into the listbox by javascript will disappear when there is a post back ocurred in the web page. As I understand, you can try to consider using some Hidden controls to save the the values entered into the listbox by javascript before any post back occurrs. Then you can get the values from the behind code when clicking "save" button to save data to the database. Here are some sample codes for your reference:

    <input type="hidden" id="hd1" runat="server" />

     

    function SaveValuesToHidden()

    {

       document.getElementById("hd1").value = "Value1";

    }

     

    <asp:Button ID="btnTest" runat="server" Text="Test" OnClientClick="SaveValuesToHidden()" />

     

    Then get the values from the behind code as follows:

    string value1 = hd1.Value;

     

    Hope this helps,

    Regards,

    Thursday, November 29, 2007 4:32 AM
    Moderator

All replies

  • Hi,

     

    Yes. All the values entered into the listbox by javascript will disappear when there is a post back ocurred in the web page. As I understand, you can try to consider using some Hidden controls to save the the values entered into the listbox by javascript before any post back occurrs. Then you can get the values from the behind code when clicking "save" button to save data to the database. Here are some sample codes for your reference:

    <input type="hidden" id="hd1" runat="server" />

     

    function SaveValuesToHidden()

    {

       document.getElementById("hd1").value = "Value1";

    }

     

    <asp:Button ID="btnTest" runat="server" Text="Test" OnClientClick="SaveValuesToHidden()" />

     

    Then get the values from the behind code as follows:

    string value1 = hd1.Value;

     

    Hope this helps,

    Regards,

    Thursday, November 29, 2007 4:32 AM
    Moderator
  • Hello, I've the same problem but my control is a asp:textbox not hidden. A calendar set the date into the textbox via javascript. When I put manualy values, it work. But when the value is setted via javascript , the .text value deoesn't change on the server side.  The weirdest thing is this all worked fine with visual studio 2003 and doesn't work wtih 2005.

    Help Stick out tongue
    Tuesday, February 12, 2008 4:52 PM
  • Don't set the ReadOnly proterty to [true] of asp:textbox.
    Wednesday, July 2, 2008 2:46 AM
  • Hi !!

    I am also facing the same problem. Code is not  picking the value of  textbox updated by javascript while it picks up the value entered manually. I also tried to set readonly = false. But still not working Could you please help?

    Thanks,
    Poonam
    Friday, October 17, 2008 7:51 PM

  •  I have the similar question, Has anyone solved the problem? I'm still waiting for the answer.
    Sunday, December 19, 2010 1:51 AM
  • Place the field which is giving you trouble inside a form with "runat=server" attribute and try. Then it will reflect the values set dynamically on the client side in your code-behind.

    I'm not sure of this behavior but the above seemed to work for me.

    • Proposed as answer by devinlgls Monday, April 23, 2012 7:23 AM
    Monday, April 23, 2012 7:23 AM