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

1 solution

you are preventing postback of button click

use this

<asp:LinkButton ID="linkbutton" runat="server" onclick="linkbutton_Click" OnClientClick="return generateRandom();" >Click me!</asp:LinkButton>


and javascript function

function generateRandom()
{
C#
$('#randomText').val(random());
            alert('Reference Code: ' + $('#randomText').val());

return true;

}

and on server side get value of textbox.
 
Share this answer
 
Comments
El Dev 21-Feb-13 10:50am    
Thnks its works but last thing is there any way to prevent a duplicate random number.
Taha Akhtar 21-Feb-13 10:57am    
its random,there is always a possibility,use guid generator or check whether random number exists or not

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