Click here to Skip to main content
11,929,481 members (54,613 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET jQuery
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">
     <script type="text/javascript" src="../javascripts/jquery-1.8.js"></script>
     <script type="text/javascript">
        window.onload = function () {
        $('#linkbutton').live("click", function (e) {
            alert('Reference Code: ' + $('#randomText').val());
        function random() {
            return Math.floor(Math.random()*10000000);
    <form id="form1" runat="server">
    <asp:LinkButton ID="linkbutton" runat="server">Click me!</asp:LinkButton>
    <asp:TextBox ID="randomText" runat="server"></asp:TextBox>

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 21-Feb-13 4:54am
El Dev877

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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()
            alert('Reference Code: ' + $('#randomText').val());
return true;


and on server side get value of textbox.
El daniel 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100