www.webdeveloper.com
Results 1 to 6 of 6

Thread: Call a JavaScript function from VB code

  1. #1
    Join Date
    Oct 2008
    Posts
    92

    Call a JavaScript function from VB code

    How can I call a Javascript function from my VB.Net code?

    I have built a ASP.Net website in VS2005. It is one of those set-ups where you have 2 seperate files for the webpages: i.e. Default.aspx and Default.apsx.vb.

    I need to be able to call a JavaScript function from within my VB code, also I need to pass a string variable into the JavaScript function. The JavaScript code is currently sitting at the top of the Default.aspx page whereas the VB code is in the Default.apsx.vb page.


    Additional info (if it helps):

    What the VB code does is take a user-inputed address and find it in a database and returns certain info about the address. I am trying to improve it to display a Google Map of the address using the Google Maps JavaScript API.

    I already have the javascript function ready to go that takes a string address and makes and displays a google map of it, I just need to be able to call it after the user has entered their address.

  2. #2
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    Hi,

    you can build your javascript in your code behind then register it as a startup script.

    some example code in c#

    Code:
    private void Button1_Click(object sender, System.EventArgs e)
     {
      string javaScript =
       "<script language=JavaScript>\n" +
       "alert('Button1_Click client-side');\n" +
       "</script>";
      
      RegisterStartupScript("Button1_ClickScript", javaScript); 
    }
    regards


    Ribs

  3. #3
    Join Date
    Oct 2008
    Posts
    92
    Actually I have been thinking about this some more and it seems that the most sensible way to do it is to do the reverse. I.e. to pass the address to the webpage and have the javascript function pick up the address and run everytime the webpage is refreshed.

    So in other words, each time the user enters an address, that address would be stored in a hidden textbox or a cookie or something, then when the page is refreshed the javascript function grabs the address from the hidden textbox/cookie and runs it.

    But what would the JavaScript syntax be to retrieve the address from the hidden textbox or cookie? Something like "getElementbyID("Name of text box")"???

  4. #4
    Join Date
    Apr 2010
    Location
    Cocoa Beach, FL
    Posts
    41

    Simple Javascript solution to get value of element

    You can get the value of a form element completely within the Javascript scope and, then you can pass it to a URL as a URL variable which can then be processed with the ASP.NET code. That is one way to do it, probably the simplest, although there are other ways to do it also. Also a hidden form element will be processed by ASP.NET in the same way that a form element of type text is processed.

    You can access the value of a form element using getElementById, even if the form element is type HIDDEN, this is actually done quite often in web application development.

    Here is a simple example of using getElementById with a form element. It is very important that your form element has an ID attribute, or else the javascript will fail.

    <html>
    <head>

    <script languague="javascript">
    function ProcessForm() {
    var Address = document.getElementById("address").value;
    if (Address != "") {
    alert("Address:" + Address);
    return true;
    } else {
    return false;
    }
    }
    </script>

    </head>
    <body>

    <form method="post" onsubmit="return ProcessForm();">
    Address: <input type="text" name="address" id="address" value="" />
    <input type="submit" value="Submit">
    </form>

    </body>
    </html>

    Michael G. Workman
    michael.g.workman@gmail.com
    Last edited by Michael_G; 07-11-2011 at 02:13 PM.

  5. #5
    Join Date
    Sep 2012
    Posts
    1
    Hi,
    I am attempting to do something of the same sort. My job uses a cloud based ticketing system that does not refresh an incoming ticket list on its own. I must press a specific button on the specific applet. I believe that this specific refresh button actually does two jobs; 1) any changes I make to a ticket status is updated on the server 2)grabs list of designated tickets (assigned to me and new). For example: If i change a ticket status to "Spam", click out of the field and use the designated refresh button it will remove the ticket from my view. However, if I change the ticket status to "Spam", click out of the field and do a hard refresh it ticket in question stays as originally marked and in my view.

    So my project is to create my own browser that loads the page and refresh the ticket applet every minute or two minutes and look for a new ticket. Creating the browser, timing and detect new ticket is done. However, I cannot seem to pass the javascript code for the applet from VB.

    Another issue is that since the webpage interface is not ours, I cannot modify the page to accept my own specific info. However, I have found the function on their js webpage. i am just in need of being able to pass the script and its parameters from VB.

    Here is the html code for the button:
    <a id=?"servercontentneg706_refresh" class=?"noImageUnderline portletIconRefresh" href=?"javascript:?refreshPortlet('servercontentneg706','LISTSEARCH_-706', false, '-17',null,null, null)?;?var win = window.isOLC ? window :? parent.cleanupTooltips ? parent :? window;? if (typeof win.cleanupTooltips != 'undefined')?win.cleanupTooltips('LISTSEARCH__706')?;?" onclick=?" setPortletToUpdating('servercontentneg706')?;? " title=?"Refresh">?</a>?

    Here is the code that is sent when the refresh button is clicked:
    javascript:refreshPortlet('servercontentneg706','LISTSEARCH_-706', false, '-17',null,null, null);var win = window.isOLC ? window : parent.cleanupTooltips ? parent : window; if (typeof win.cleanupTooltips != 'undefined')win.cleanupTooltips('LISTSEARCH__706');

    Here is the routine that is called (I believe):
    function refreshPortlet(f,l,e,g,m,d,h,c,k,a){setPortletToUpdating(f);var b="/app/center/setup/portletrefresher.nl?elemid="+f+"&sticky="+l;if(e!=null){b+="&noreload="+(e?"T":"F");}if(g!=null){b+= "&sc="+g;}if(m!=null){b+="&portletid="+m;}if(d!=null){b+="&entityid="+d+"&haschild="+(h?"T":"F");}if (c!=null){b+="&searchid="+c;}if(a!=null){b+="&lineid="+a;}b+="&nsts="+(new Date().getTime());if(k!=null&&k.length>0){if(k.charAt(0)=="?"){k=k.substring(1);}b+="&addparams="+es cape(k);}sendRequestToFrame(b,"server_commands");}

    I'm looking for the simplest ideas to do a refresh on the applet from my vb browser, whether it be simulating the button push (seems easiest), passing just the call to the js or even running the full routine from within VB.

    Thanks in Advance
    vb newbie

  6. #6
    Join Date
    Oct 2012
    Posts
    47
    I have a job that is responsible for filling SSRS report. The user presses the button and go out and check to see if there is data. If there is no data, I make no message data. If there is a data call SSRS report that I would like to open in a new window. I thought using the JavaScript function would be the best way to achieve this. How it's done or what you would recommend?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles