ASP.NET - getting a texbox value from JQuery,html to a c# varaible

Asked By Daniel on 21-Feb-13 09:58 AM
Hi! 
I want to get a textbox value from Html and Jquery and then store it in a variable in c#.
I am using this code but I dont know what next:
<head runat="server">
    <title></title>
     <script type="text/javascript" src="../javascripts/jquery-1.8.js"></script>
     <script type="text/javascript">
        window.onload = function () {
            $('#randomText').hide();
 
         }
        $('#linkbutton').live("click", function (e) {
            event.preventDefault();
            $('#randomText').val(random());
            alert('Reference Code: ' + $('#randomText').val());
        });
        function random() {
            return Math.floor(Math.random()*10000000);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:LinkButton ID="linkbutton" runat="server">Click me!</asp:LinkButton>
 
    <asp:TextBox ID="randomText" runat="server"></asp:TextBox>
     </div>
    </form>
</body>
</html>
as u can see this code is generating a random number and set it into a textbox.then what I want is to get that textbox value and store it in a c# variable.Help fixing this...
Danasegarane Arunachalam replied to Daniel on 22-Feb-13 03:37 AM
This worked for me.

I comment event.preventdefault and return a false, which restricts the postback

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
  <title></title>
  <script type="text/javascript">
  window.onload = function () {
    $('#randomText').hide();
  
  }
  $('#linkbutton').live("click", function (e) {
    //event.preventDefault();
    $('#randomText').val(random());
    alert('Reference Code: ' + $('#randomText').val());
    return false;
  });
  function random() {
    return Math.floor(Math.random() * 10000000);
  }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:LinkButton ID="linkbutton" runat="server">Click me!</asp:LinkButton>
   
  <asp:TextBox ID="randomText" runat="server"></asp:TextBox>
  </div>
  </form>
</body>
</html>

Daniel replied to Danasegarane Arunachalam on 27-Feb-13 02:01 AM
Thanks!