Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a jquery plugin that give me a popup with a list of links. When I click the link it changes the value of a text box via jquery:
JavaScript
$(document).ready(function () {
            $("a.clickable").click(function (event) {
                event.preventDefault();
                $("input#new_casecontactid").val($(this).attr('data-fullname'));
            });
        });

This part works fine, but when I save the form, the server side still thinks the old value is still in the textbox.
C#
protected void btnSv_Click(object sender, EventArgs e)
{
    var ContactName = fullname.Text
}

How do I get the new value of my textbox from the client side to server side?





This is what I used to get it working:

Java
$(document).ready(function () {
     $("a.clickable").click(function (event) {
         event.preventDefault();
         txt = $(this).attr('data-fullname');

         $.ajax({
             type: "POST",
             data: txt,
             success: function () {
                 $("input#new_casecontactid").val(txt);
             }
         });
     });
 });
Posted
Updated 21-Dec-11 9:35am
v3

1 solution

Of course the server side still have an old value. You never sent any HTTP request to the server side during all you jQuery manipulation. The missing link is Ajax HTTP request to be sent via jQuery. Something like:

JavaScript
$.post(
   'myPage.aspx',
   {
      type: "textbox-text-updated",
      textBoxText: newText
   },
   function(data) { /* callback on server response with data, if you need to do so */ } 
);


Your server-side code should extract new value from HTTP request by the key and remember new value for the TextBox.Text.

[EDIT] Please remember that it will send the 'post' HTTP request asynchronously; it means that on the return from $.post the post is generally not yet processed, so if you need to handle the result of the post, it can be reliably done only in the callback shown above, which is called only when the server's response is already received.

—SA
 
Share this answer
 
v2
Comments
Monjurul Habib 21-Dec-11 1:34am    
agree 5!
Sergey Alexandrovich Kryukov 21-Dec-11 11:56am    
Thank you, Monjurul.
--SA
Nunnenkamp 21-Dec-11 12:05pm    
Thank you! That got me going in the right direction.
Sergey Alexandrovich Kryukov 21-Dec-11 12:22pm    
You are welcome. Sorry, this code is only to give you the idea. I would need to know more about your plug-in and your server-side which could make a more elegant solution. Maybe you plug-in already designed with Ajax in mind, just examine it to see if there is a simple solution.

Good luck, call again.
--SA

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