0

I've deployed my ASP.NET application today. It's a web application (I am using forms, etc.). I was happy with all my functionalities. For example, I had part of the of the code that says

if c.results = " " then MsgBox("Error! No record was returned)

Then I clear all my text boxes.

That MsgBox was working when I was running my application locally, but now that I've deployed it I get a server run error! I read some similar posts that said MsgBox is not supported with web applications, but all their answers were with JavaScript. I am not familiar with it, but I don't understand how to put my JavaScript in my Visual Basic class instead of where I am calling the MsgBox- is there a way fixing the above issue with VB.NET code?

Here is the code:

Dim results = customer.getCustomerDetails(txtCustomerNumber.Text, txtDOB.Text)

If (results.customerNumber = "") Then
    Response.Write("<script>alert('Customer record not found')</script>")

    txtCustomerNumber.Text = ""
    txtDOB.Text = ""

Else
    ( Do the other stuff)

End if
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Warda
  • 67
  • 1
  • 3
  • 15
  • 2
    `MsgBox` is a WINforms function, not a WEBforms one. Use javascript `alert` or a modal popup. You can call client side scripts with `ScriptManager` – VDWWD Oct 04 '17 at 20:10
  • Thanks for your comment! Can you please explain more? How can you in a vb code specify that if results "" then call the JavaScript alert box. I hardly worked with JavaScript and I can't get my head around , also I can't find a document that walk through an example. Thank you – Warda Oct 04 '17 at 20:14
  • Basically I am trying to say my functionality is written in vb.net and JavaScript is client side how can I mix both as in vb.net do the above if statement check then replace MsgBox with JavaScript alert?! – Warda Oct 04 '17 at 20:26
  • See https://stackoverflow.com/questions/4848678/how-to-call-javascript-function-from-code-behind – VDWWD Oct 04 '17 at 20:27
  • You need to understand the difference between client side and server side code. – mason Oct 04 '17 at 20:37
  • msgbox is server side so it can't show up to the client which is only seeing the client side of the asp.net webform, hence use the Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "") if you want to access javascript alert from code behind or simply alert in javascript – JKOU Oct 05 '17 at 06:23
  • 1
    @JKOU your answer is the correct one, if you write it as an answer I'll accept it. Thank you so much! :) – Warda Nov 07 '17 at 15:48
  • ok I will do that :) – JKOU Nov 08 '17 at 07:41

2 Answers2

0

MsgBox is server side so it can't show up to the client which is only seeing the client side of the ASP.NET webform, hence use the following if you want to access a JavaScript alert from code behind or simply alert in JavaScript:

Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "<script 
language=JavaScript>alert('your wanted message');</script>") 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JKOU
  • 255
  • 2
  • 14
-1

You can generate client code from your server side VB as such:

If c.results = " " Then
    Response.Write("<script>alert('Error! No record was returned')</script>")
End If
SE1986
  • 2,534
  • 1
  • 10
  • 29
  • The respone.write may cause some problems sometimes, instead use the Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "") – JKOU Oct 05 '17 at 06:21
  • What sort of problems might be experienced? – SE1986 Oct 05 '17 at 06:39
  • well for example, one time i was using response.write and it ruined my interface while showing up the popup message, instead try to use the code i set above. – JKOU Oct 05 '17 at 06:41
  • Fair do's. Never had any issues myself but will bear that in mind – SE1986 Oct 05 '17 at 07:28
  • ok that's great, it depends on your situation, i hope you do not have to face it, just in case something happens, use the code i provided instead – JKOU Oct 05 '17 at 07:33
  • Response.Write did affect on my interface. The tab disappeared however JKOU code made the date calendar looks messy! Not sure what is the ideal way – Warda Oct 05 '17 at 08:40
  • you need to post some more code (perhaps the full method you have this code in) to understand better what may be causing that. We can't see any code for your calendar – SE1986 Oct 05 '17 at 08:46
  • I've added my code. However the calender is Jquery code . in the aspx, I don't think thats an issue coz it was okay before adding Respone code – Warda Oct 05 '17 at 09:03
  • I've read the problem with the Calender changing and Font becoming bigger is due to Response.Write messing up with HTML code. So its defintley an issue with that. http://forums.codeguru.com/showthread.php?468711-RESOLVED-bigger-font-after-postback – Warda Oct 05 '17 at 09:12
  • @Warda you can use ajax control toolkit for the ajax calendar extender, which can be used as a calendar and bind it to a control, or you can show us the jquery code so i can test around – JKOU Oct 06 '17 at 06:53
  • Please do not recommend using `Response.Write` to run JavaScript in Web Forms. That's not the proper mechanism to do it. – mason Oct 06 '17 at 16:37