Passing parameters in asp.net function from javascript

.net , asp.net , csharp , java , javascript , xhtml Philadelphia, United States
  • 10 years ago

    Hi..
        I am having a function getcities()in codebehind. I am writing javascript in aspx page and calling this function from javascript like this...

      <% getcities(); %>

      This wrks fine. But now i want to pass parameters in this function. So if i give
     
      <% getcities(cityname, statename); %>

     den it gives error - cityname & statename does not exist in current context. Here i have 2 var cityname & statename. can anyone tell me wat is the problem in this.

    Thnku in advance...

  • 10 years ago

    Hi,

    Passing in javascript variables like that won't work i am afraid, what you are trying to pass in there are some asp.net global variables, i.e. they have to have been created inside a global script block for asp.net to see and recognise them.

    if you need to pass variables from javascript to an asp.net function, you can try using hidden fields, people use those to good effect, simply change the values to what you want to pass in to the function and then use the function to read them out.

    Alternatively, you can use postback or callbacks depending on what functionality you want to use, i would recommend postbacks in most cases as callbacks can cause problems with updatepanels and redirects so much be used with much more caution.

    If you need an example of a postback doing this sort of call, then let me know and i will knock up a quick and dirty example.

    Regards

    Si

  • 10 years ago

    Hi.

    Dear Slicksim how I can use asp.Net function from javascript.

    asp fuction like this      string AAA(string a){}

     

    thanks

    Armen

     

  • 10 years ago

    Hi,

    How do you want the function call to happen?

    I.e. do you want the page to postback, or do you want it to callback?

    What is the functions purpose?

    Regards

    Simon C

     

  • 10 years ago
    in javascript I write document.write(st); Default.aspx.cs written
  • 10 years ago

    Default.aspx

    in javascript i write

     <% var st= jj("iii"); %>
        
         document.write(st);

    Default.aspx.cs written

    public string jj(string a)

    {

    //calling webservice

    return "some string"

    }

    /////////////////////////

    javascript knows function jj but    returns error like this "st undefined or st is null"

     thanks

     

  • 10 years ago

    Hi,

    When you use <% {code} %> you are actually getting the parser to run some script there in asp.net, not javascript.  so think of those code blocks as you actually writing lines of code in your code behind, javascript can not use them.

    you can in your example use: <% response.write(jj("iii")) %> but something tell me that isnt what you want.

    Try adding a hiddenfield to your form called hidTest then use the following javascript

    <% hidTest.value = jj("iii") %>

    var st = document.getElementById('<%= hidTest.clientId %>').value;

    document.write(st);

    If that doesnt work, i shall show you the code pattern for client callbacks

    Regards

    Simon C

  • 10 years ago
    Thank You Simon. I try that You say. for(var i=0;i var st= document.getElementById('').value; document.write(st); } Error 1 The name 'i' does not exist in the current context //// I want in client side,in generated HTML text , get(find) all tags ,send its to webservice(in server) as string, get result(string) and replace A tag to result. (before page shows) can I do it? Help me please.
  • 10 years ago

    Hi Legolas,

    You have lost me a little I am afraid.

    The line: i var st = document.getElementById('').value

    What is that for?

    When you say you want to get all the tags, do you mean all the values from textboxes etc on the page?  Why do you need to do that in Javascript? Can't you use a postback and call the webservice from the code behind?

    Regards

    Simon C

  • 10 years ago

    <script type="text/jscript" >

     for(var i=0;i<10;i++)
       {
      
       <% HiddenField1.Value = jj(i.toString());%>
       var st= document.getElementById('<%= HiddenField1.ClientID %>').value;

         document.write(st);
        
       }

     </script>

  • 10 years ago
    I want in client side,in generated HTML text , get(find) all tags ,send its to webservice(in server) as string, get result(string) and replace A tag to result. (before page shows) can I do it? Help me please.
  • 10 years ago

    only A tags.

    in default.aspx.cs i have not access to A tags because it's are not Controls.

    in javascript i can find A tags. but here i can not use    .net function to get webservice result

     

  • 10 years ago

    Hi,

    You can create asp:hyperlink objects that are controls and will be seen by the code behind.

    i have managed to do something like what you want, however, i dont know if it is what you are after, take a look (btw, i develop in vb.net, so you will need to convert to the c# equivalents)

    This is the code behind:

    Partial Class _Default

    Inherits System.Web.UI.Page

    Implements ICallbackEventHandler

    Dim strHref As String = ""

    Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult

    Return strHref & " function called"

    End Function

    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent

    strHref = eventArgument

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "thecallback", "<script>function start(thehref){" & ClientScript.GetCallbackEventReference(Me, "thehref", "result", "") & "}</script>")

    End Sub

    End Class

     

    this is the markup in the page

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!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">

    <title>Untitled Page</title>

    <script type="text/javascript">

    function startitup()

    {

    var tags = document.getElementsByTagName("a");

    var atags = "";

    for(i = 0; i < tags.length; i++)

    {

    var tag = tags[i];

    atags += tag.getAttribute("id") + "|";

    atags += tag.getAttribute("href") + "|";

    }

    start(atags)

    }

    function result(theresult)

    {

    alert(theresult);

    }

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <div id="div1">

    <a href="test.aspx" id="test">test</a><br />

    <a href="test1.aspx" id="test1">test 1</a><br />

    <a href="test2.aspx" id="test2">test 2</a><br />

    <a href="test3.aspx" id="test3">test 3</a><br />

    <input type="button" onclick="startitup();" value="begin" />

    </div>

    </form>

    </body>

    </html>

    See if that does what you want.

    Regards

    Simon C

  • 10 years ago

    Hi Simon jan!

    Thank You,Thank You Thank You!!!

    that all I need.

    thank you

    Armen

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous