|
-
Nov 2nd, 2004, 04:38 PM
#1
Thread Starter
Fanatic Member
How to fire off Java script function from VB code in VB .Net
Hi everyone. I have an input box function written in java. I have a couple of questions.
First, how would I start that function from the middle of a precedure from my visual basic sub routine? I don't want it to always run when this button is clicked. What I'm doing is checking a database for some specific data, if it's not found, the popup box function needs to open up so they can enter a value in.
also, can I pass some value, say a session value from the vb code to the java function?
Lastly, how could I pass the value they entered in back to the vb process that is running.
Following is the java I tried but I don't think it will use the Session variable I'm trying.
Code:
<script language="javascript">
Function (TermID)
var TermID = prompt("Enter Terminal ID for " & Session("st"));
</script>
I know there are buttons out there that I could click and have them enter a value, but like I said I need this to run sometimes, not everytime the submit button is clicked.
Thanks!
-
Nov 3rd, 2004, 11:13 AM
#2
Within your VB function you can do something like this:
Code:
....if data is not found
Response.Write("<script language='javascript'>YourJSFunctionName(" & YourParameterValue & ")</script>")
-
Nov 3rd, 2004, 01:01 PM
#3
Hyperactive Member
The most accepted way to do this is as follows:-
if <your data code> then
dim sb as new system.text.stringbuilder
sb.append("<script language=""javascript"">" & vbcrlf)
sb.append("Function (TermID)" & vbcrlf)
sb.append("var TermID = prompt(""Enter Terminal ID for " & Session("st") & """);" & vbcrlf)
sb.append("</script>")
page.registerstartupscript("myscript", sb.tostring)
sb = nothing
end if
What this does is correctly places the javascript where it is sure to run once the page has loaded (and all objects are loaded and rendered on the page). It also allows you to access the code and remove it by using the name you give it, myscript, in the case above. The vbcrlf is just to make the code readable in the pages source.
-
Nov 4th, 2004, 11:19 AM
#4
Thread Starter
Fanatic Member
Thanks for the post you guys. However there is still a problem.
I need this input box to pop up right when it encounters it in the code. This is not happening.
The input box does not show up until after the procedure has completed which is too late.
Also, is there anything like a Yes No button that I can have pop up at the begining of the code and have it process the answer without waiting until the procedure has already run?
Thanks again.
-
Nov 4th, 2004, 10:21 PM
#5
Banned
Use JS and override the pre render event of the base class. Or use JS in the page load event.
-
Nov 5th, 2004, 07:51 AM
#6
Thread Starter
Fanatic Member
I don't know what you mean there. Could you possibly give a code example please?
Thanks
-
Nov 8th, 2004, 07:57 AM
#7
Thread Starter
Fanatic Member
-
Nov 11th, 2004, 08:29 AM
#8
Hyperactive Member
Hi Indy,
I know what you are asking, what happens is usually you response.write the javascript, and it happens after everything else, you need it before everything else gets rendered on the page? Am I correct?
To do this... do something like this...
Dim jScriptString as String
jScriptString = "<SCR" + "IPT>"
jScriptString += "alert('Hello');"
jScriptString += "</SCR" + "IPT>"
lblScript.Text = jScriptString
Now put lblScript (an asp:label) at the top of your page, the script will then execute before anything else is rendered...
-
Apr 20th, 2005, 11:59 AM
#9
Lively Member
Re: How to fire off Java script function from VB code in VB .Net
I'm having a similar problem and I don't see an answer in the post above.
Here's my scenario.....
I have placed the following javascipt in the html side of my aspx page
(hard coded)
<script language="javascript">
function blahblah()
{
alert('Test')
}
</script>
Now let's look at my code behind....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Session("callscript" = true) Then
' Here I need to be able to call the script that is already hard coded into my page
End If
End Sub
I don't think it's possible but how would you call an existing script in asp.net code ?
-
Apr 20th, 2005, 12:57 PM
#10
Hyperactive Member
Re: How to fire off Java script function from VB code in VB .Net
What you'll need to do is write the script into a label control. Because you can't call a script that hasn't been rendered to the HTML page yet..
Make sure the script is in the head. And your label tag in the body...
Then do something like
<script language = "JavaScript">
function sayHello()
{
alert('Hello');
}
</script>
<asp:label id = "lblScript" runat = "server" />
Then in your server-side code...
Sub CallMyScript()
lblScript.Text = "sayHello();"
End Sub
He who has conquered himself, is far greater than he who has conquered a thousand men... - The Buddha
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|