Passing values from Javascript to ASPX

Last post 08-08-2008 12:47 AM by amit_3281. 6 replies.

Sort Posts:

  • Passing values from Javascript to ASPX

    01-28-2008, 2:55 AM
    • Member
      420 point Member
    • BugInfested
    • Member since 12-04-2005, 4:31 AM
    • Posts 107

    We have a program that needs to pass a variable value from Javascript to ASPX.  Can somebody tell me how to do this?

  • Re: Passing values from Javascript to ASPX

    01-28-2008, 3:07 AM
    • Member
      66 point Member
    • mcguzic
    • Member since 04-09-2007, 1:59 PM
    • Posts 90

    You can put on aspx page HiddenField and pass value to it , and after get it from c# or vb ...

     

     

    http://webdefence.info for those who develops and for those who hacks
  • Re: Passing values from Javascript to ASPX

    01-28-2008, 3:12 AM
    • Member
      274 point Member
    • elkdanger
    • Member since 01-22-2007, 12:42 PM
    • Newcastle, UK
    • Posts 78

    One way to do this would be to use ASP.Net Ajax and a web service:

    1. Create a webservice with a service method which takes some parameter that you need to pass from Javascript

    2. Use the asp:ScriptManager on the page and bind your webservice as a service reference, like shown:

    <asp:ScriptManager runat="server">
    <Services>
    <asp:ServiceReference Path="MyService.asmx"/>
    </Services>
    </asp:ScriptManager>

    3. Call your webservice method from javascript!

    <script type="text/javascript">
    MyService.SomeMethod(param, onSuccess);
    function onSuccess(result)
    {
    alert("done!");
    }
    </script>

    Bear in mind you do not need to explicitly create the parameter for the onSuccess handler; .Net will do this for you.

    Hope that helps! 

    Steve's Coding Blog: http://stevescodingblog.co.uk
  • Re: Passing values from Javascript to ASPX

    01-28-2008, 3:14 AM
    • Member
      85 point Member
    • naresh_337
    • Member since 10-18-2007, 11:51 AM
    • Posts 65

    HI :)

    Have a look in to this !

    this may help you!

    http://www.thescripts.com/forum/thread628738.html

  • Re: Passing values from Javascript to ASPX

    01-28-2008, 3:17 AM
    • Participant
      1,576 point Participant
    • Spider Master
    • Member since 10-16-2007, 5:32 PM
    • New Zealand
    • Posts 449

    Hello, Can you be more specific with your question so that we can better understand what you are in fact trying to accomplish?

    • How are you calling this variable? JavaScript
    • What type of variable?
    • What do you wish to do with it after its submitted?
    • Is it related to function/control or data to be stored?

    Perhaps a small example of what you have so far could help people better understand.

    Regards Big Smile

    Trading Center is a New Classifieds Starter Kit on Code Plex.

    "If Your Question Has Been Answered, Please Mark It As the Answer"

  • Re: Passing values from Javascript to ASPX

    01-29-2008, 3:43 AM
    Answer

    Hi,

    Based on your description, you want to retrieve the variable from JavaScript in aspx page.

    I'm agreed with mcguzic. You can set the variable from JavaScript into Hidden control and then you can get it in aspx page.

    HTML: 

    <script type="text/javascript">
    function abc()
    {
      var str="value";
      document.getElementById("Hidden1").value=str;
    }
    
    </script>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Hidden1" type="hidden" runat="server" />
            <asp:Button ID="Button1" runat="server" OnClientClick="abc()"  Text="Button" 
                onclick="Button1_Click" />
        </div>
        </form>
    </body>

     Code Behind:

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(Hidden1.Value);
        }

     But you should pay attention on the order of setting the variable to Hidden control in JavaScript and retrieving the value of Hidden control in aspx page. In the same event handle, it needs execute the Client first. For example,  OnClientClick="abc()" will be executed before onclick="Button1_Click". Otherwise, you will get the empty.

    Hope it helps.

     


    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Passing values from Javascript to ASPX

    08-08-2008, 12:47 AM
    • Member
      6 point Member
    • amit_3281
    • Member since 08-01-2008, 2:26 AM
    • Posts 8

    If I need to send more than 100Kb Data then what shall I do?

     

    Thanks in Advance

Page 1 of 1 (7 items)