Solved

How to call VB Script in VB code?

Posted on 2004-04-18
18
185 Views
Last Modified: 2012-05-04
Hi,

I need to execute my VB code when this happens:

        If userName <> dReader2("custID") Then
            Response.Redirect("ForumPosts.aspx?topicID=" & topicID)
            'Execute VB code right here
        End If

This is my VB code:

      <!-Script to alert user that they can only edit their own post.>            
      <script language="vbscript">
      Function NoWay()
        MsgBox("You can only edit your own posts!")
      End Function
      </script>

How do it call it?
0
Comment
Question by:FYPJ
  • 8
  • 7
18 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 10854876
Try this in your code behind.

yourWebControl.Attributes.Add("onclick", "NoWay()")
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10855022
You can use
If userName <> dReader2("custID") Then
      ResponseClientScriptBlock ("way", "MsgBox('You can only edit your own posts!')" );
      Response.Redirect("ForumPosts.aspx?topicID=" & topicID)
End If

but you have to change the places of the response.redirect and the vb execution

B..M
0
 

Author Comment

by:FYPJ
ID: 10856584
Hi,

I can't use Timbo87's solution as i dun have an onclick command to run the script.

As for mmarinov, what is ResponseClientScriptBlock?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10857183
0
 

Author Comment

by:FYPJ
ID: 10859394
Sorry i did not fully understand it. Begginner  here.

This was wat i tried:

        'Form the script that is to be registered at client side.
        Dim scriptString As String
        scriptString = "<script language=VB> function NoWay()"
        scriptString += "MsgBox(""You can only edit your own posts!"")"
        scriptString += "End Function"
        scriptString += "</script>"


        If userName <> dReader2("custID") Then
            Response.Redirect("ForumPosts.aspx?topicID=" & topicID)
            RegisterClientScriptBlock("clientScript", scriptString)

        End If

But still no alert box appeared.

What is the correct way?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10859444
try this

 Dim scriptString As String
        scriptString = "<script language=javascript> function NoWay(){"
        scriptString += "if(confirm(""You can only edit your own posts!""))"
        scriptString += "{document.location.href=" & "ForumPosts.aspx?topicID=" & topicID & "}"
        scriptString += "}"
        scriptString += "</script>"


        If userName <> dReader2("custID") Then
            RegisterClientScriptBlock("clientScript", scriptString)
        End If

B..M
0
 

Author Comment

by:FYPJ
ID: 10859565
Hmm... tried ur code but i directs me straight to the page without the alert box.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10859653
does it give you client errors ?
i don't think that this will give you something different but try this

        scriptString = "<script language=javascript> function NoWay(){"
        scriptString += "if(window.confirm('You can only edit your own posts!'))"
        scriptString += "{document.location.href=" & "ForumPosts.aspx?topicID=" & topicID & ";}"
        scriptString += "}"
        scriptString += "</script>"

Unfortunatelly i can not test it :-( right now
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:FYPJ
ID: 10859745
Nope it does not give any error, just directs me right to the page without any alerts.

Still doesn't work.

Thanks for ur effort to help. Can u get back to me soon when u find the solution?

Thanks!
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10859827
i've send it to a friend of mine and this work i've tested

        Dim scriptString As String
        scriptString = "<script language=javascript>"
        scriptString += "if(confirm(""You can only edit your own posts!""))"
        scriptString += "{document.location.href=" & """ForumPosts.aspx?topicID=" & topicID & "";}"
        scriptString += "</script>"

        If IsPostBack Then
            RegisterClientScriptBlock("clientScript", scriptString)
        End If

B..M
0
 

Author Comment

by:FYPJ
ID: 10860045
Nope still doesn't work for me.

Damn this sucks...

Is there anyway i can generate a VB MsgBox from my VB codes without calling a script?

Sorry for the trouble.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10860122
i hate the qutes in vb :-(
this line is incorrect :-( scriptString += "{document.location.href=" & """ForumPosts.aspx?topicID=" & topicID & "";}"
it should be
scriptString += "{document.location.href=" & """ForumPosts.aspx?topicID=" & topicID & """;}"

about vb msgbox - i can not tell if it or it is not a way of doing this
i stopped use vb script for clients needs because it is workig only in ie


B..M
0
 

Author Comment

by:FYPJ
ID: 10860165
YES!!! Thank u very much it works!

But the confirm box gives the user a choice of yes or no to click, is there a way to edit it to show only 1 button?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10861175
it is not possible to edit yes/no button but you can use alert instead of confirm
but in this way it won't return true/false value but will acts like a normal mesage
is this what you want ?

B..M
0
 

Author Comment

by:FYPJ
ID: 10865795
Yes thats what i need.

So how do i structure my script so that when the user clicks ok on the alert box i will direcit them to this page?

document.location.href=" & """ForumPosts.aspx?topicID=" & topicID & """;
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 75 total points
ID: 10866368
       Dim scriptString As String
        scriptString = "<script language=javascript>"
        scriptString += "alert(""You can only edit your own posts!"");"
        scriptString += "document.location.href=" & """ForumPosts.aspx?topicID=" & topicID & "";"
        scriptString += "</script>"

B..M
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replacing HTML tags in Textarea/Textbox 5 47
DataGridview Currency Formating? 22 53
C# Single Form 8 28
VB.Net - Windows UserName index and length error 4 22
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now