freaky
freaky

Reputation: 294

running a javascript with the help of VB inside of ASP.Net

I want to know is there is way for running a javascript code with the help of vb

page:test1.aspx
<a href="#" onclick="Popup=window.open('testopen.aspx?jack=hello','Popup','toolbar=yes,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=420,height=400,left=430,top=23'); return false;">Test Window</a>


page:test2.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim check As String = "<a href='#' onclick='Popup=window.open('make_changes_form.aspx','Popup','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=420,height=400,left=430,top=23'); return false;>Test Window from vb</a>"
    Context.Response.Write(check)
End Sub


the code in page test1.aspx works
now i want to if there is a way for it to work in page:test2.aspx.vbbecause the current method is not working for me. Thank you
Edited
The reason im trying to do this is because i cannot use a function in javascript by passing as integer
for instance

Javascript
 function hello (str1)
  {
     alert(str1)
      alert ("hello world")    
   }

aspx code
    <input type="text" id="2">
    <a href="#" onClick="hello(2.value)">CheckFunction</a>


This is why i cannot use function because the function wont activate because id cannot be a number it has to have an alpha numeric value

Upvotes: 0

Views: 1033

Answers (2)

Pellizon
Pellizon

Reputation: 1375

Using Firebug, i noticed that the tag wasn't comming with the javascript, so i created a function apart and called it from event onclick, it worked fine to me.

try this:

    Dim check As String = "<a href='#' onclick='OpenPopUp()'>Test Window from vb</a>"
    Context.Response.Write(check)
    jscript += "<script language='JavaScript'>"
    jscript += "function OpenPopUp() {"
    jscript += "window.open('popup_to_be_oppened.aspx', '', 'resizable=no , menubar=no, scrollbars=yes, width=290, height=480');"
    jscript += "} </script>"
    Context.Response.Write(jscript)

Also, you will have to use hello(document.getElementById('2').value instead of hello(2.value)

Upvotes: 1

Echilon
Echilon

Reputation: 10264

Rather than using Response.Write, you should really use a standard ASP.NET Hyperlink control and ScriptManager.RegisterStartupScript. Then you could use jQuery to fire the alert:

On your page:

<asp:HyperLink runat="server" ID="lnkPopup" ClientIDMode="Static" NavigateUrl="#">Show Popup</asp:HyperLink>

In your codebehind:

Dim js = <js>$(document).ready(function () {
    $('#lnkPopup').bind('click', function () {
        alert('Some text');
    });
});</js>.Value
ScriptManager.RegisterStartupScript(Me, GetType(Page), "jsPopup", js, true);

The actual script is a multiline literal which makes it easier to read in VB.NET.

Upvotes: 1

Related Questions