Results 1 to 13 of 13

Thread: [RESOLVED] how i can return value string from function javascript to code-behind asp.net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Resolved [RESOLVED] how i can return value string from function javascript to code-behind asp.net

    Hi All,
    how i can return value string from function javascript to code-behind asp.net
    directly as this syntax example but this give me errors

    in html code

    <script>
    function Getstr()
    {
    return "MyStr";
    }
    </script>

    in code-behind
    Page.ClientScript.RegisterStartupScript(Me.Getstr(), "alert", "Getstr();", True)

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: how i can return value string from function javascript to code-behind asp.net

    One way is to make the javascript method set the value of a hidden field control/element then when the page is posted back get the value from the hiddenField.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Re: how i can return value string from function javascript to code-behind asp.net

    thank you very much for reply me
    but that is means no other solution expect hidden field control/element
    no direct solution

  4. #4
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: how i can return value string from function javascript to code-behind asp.net

    well javascript runs on the client (browser). If you want javascript to communicated with the server you can use ajax but perhaps it would be better to give a broader overview of what you need the page to do.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Re: how i can return value string from function javascript to code-behind asp.net

    thank you for reply
    my page aspx include button facebook connection
    to give me information like name,age,......about user
    by facebook JavaScript API
    i want save it in database by make function js return directly value to code-behind

    I wish I could find this

  6. #6
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: how i can return value string from function javascript to code-behind asp.net

    Quote Originally Posted by prokhaled View Post
    Hi All,
    how i can return value string from function javascript to code-behind asp.net
    directly as this syntax example but this give me errors

    in html code

    Code:
    <script>
    function Getstr()
    {
        document.getElementById('hfMyStr').value = MyStr;
    document.getElementById('btnSubmitString').click();
    }
    </script>
    on the aspx page
    Code:
    <form id="form1" runat="server">
    <asp:HiddenField ID="hfMyStr" runat="server" />
        <asp:Button ID="btnSubmitString" runat="server" Height="0" Width="0" OnClick="btnSubmitString_Click" />
    </form>
    in code-behind
    Code:
    protected void btnSubmitString_Click(object sender, EventArgs e)
    {
    ScriptManager.RegisterStartupScript(this, this.GetType(), "myAlert", "alert('" + hfMyStr.Value + "');", true);
    What is wrong with brin's suggestion?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Re: how i can return value string from function javascript to code-behind asp.net

    thanks for all

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: how i can return value string from function javascript to code-behind asp.net

    Hello,

    For the benefit of other people who might be reading your thread, you might want to think about telling us what your final solution was.

    Gary

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Re: how i can return value string from function javascript to code-behind asp.net

    Code:
    For the benefit of other people who might be reading your thread, you might want to think about telling us what your final solution was.

    How i can do this i can not remember

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Re: [RESOLVED] how i can return value string from function javascript to code-behind

    thank u i remember that

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] how i can return value string from function javascript to code-behind

    I'm confused

  12. #12
    Addicted Member
    Join Date
    Mar 2007
    Location
    San Pedro de Macoris, Dominican Republic
    Posts
    211

    Re: [RESOLVED] how i can return value string from function javascript to code-behind

    Quote Originally Posted by gep13 View Post
    I'm confused
    Heh. Me too, I opened the thread because I saw it marked as resolved and all I can see is a somewhat funny reply.

    Can anyone suggest how do you do what the poster asked?
    (other than the solution brin already provided, is there other methods to do it withut a hidden field?)

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] how i can return value string from function javascript to code-behind

    Another solution would be to put the variables onto the QueryString, and inspect the Query String variables on the server.

    This isn't a particularly secure way of doing things though. It is better to POST the information to the server, as suggested by Brin, with the use of the HiddenField.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width