Link to home
Start Free TrialLog in
Avatar of ajaydhane
ajaydhane

asked on

MessageBox in ASP.Net

Hi friends
can U tell me how to show messagebox in asp .net on button click
I tried like
msgBox("Prompt",MsgBoxStyle.okonly,"title")
but it is giving error that
"It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application. "

I want to try using jscript i.e. Alert() but I don't know how to show call jscript from button click

So plz help me.
Regards
Ajay
Avatar of raama16
raama16

If you want to call javascript functions like "alert()" on button click, then use basic html button and not the server control button.Ex:
<INPUT type="button" value="Button" onclick="alert('Hi');">
If you use web form control button then you can not call the javascript functions.
msgBox("Prompt",MsgBoxStyle.okonly,"title"), this is not supported in web applications(asp.net).If you want to display some message on clicking web form button, add web form label and set its text property.
Ex: lblStatus.Text="Success!"
Alternatively, if you want to show that the user has clicked on sumbit button and to prevent them from clicking on it again,

You could disable the submit button and change the text to "Submitted"
In ASP.NET when you are executing Message.Show - it will be executed on the server and you won't see any message. You can use Respond object
ASKER CERTIFIED SOLUTION
Avatar of SeanGraflund
SeanGraflund

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ajaydhane

ASKER

To ramma16 :
To TDRXander:

I had already do that calling messagebox through HTML. but I want to do some validation & checking after clicking button & according to that I want to show messagebox(not in everyCase)
& in vs.net after writing 'msgbox' it shows the syntax so I think it may be supporting it.

To SeanGraflund :
I think ur code is in CSharp but I am using VB so I tried like this,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim sJavaScript As String
       sJavaScript = "<script language=javascript>\n"
       sJavaScript += "alert('This is your message text.');\n"
        sJavaScript += "</script>"
        Me.RegisterStartupScript("MessageBox", sJavaScript)
    End Sub

But I didn't understand How to call that MessageBox.
& I tried like above code but It is giving
RunTime Error, Invalid Character

Plz simplify ur comment
regards
Ajay
I am just correcting your code. Just try this,it works(you have to use "&" for string concatenation instead of "+" in vb.).

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim sJavaScript As String
      sJavaScript = "<script language=javascript>"
      sJavaScript &= "alert('This is your message text.');"
       sJavaScript &= "</script>"
       Me.RegisterStartupScript("MessageBox", sJavaScript)
   End Sub



If the above code gives the solution for your problem, then it is fine. Otherwise let us know what exactly you want to do.
1)Do you want to do client side validation on clicking a button?
2)And then,Do you want to display message box based on some criteria?
3)And then, Do you want to submit the page?

Regards,
Ramachandra
I tried ur code it is working(with '+' also it is working)
but problem is while reloading the page it is showing that messagebox everytime. & that I don't want.

I want to use both client side validation(fields are entered) & server side validation(i.e. dabase field is not present etc.)
So plz tell me both the solutions
I want to show messagebox if database field is not present for given data as well as to tell that fields are not entered.
As well as I am showing next page by server.transfer
& I want to come back to that previous page so plz tell me how to do that
Regards
AJAY

The way of doing things are drastically changed as far as ASP.NET is concerned compare to ASP.
Now if you want to make use of both client side validation as well as server side validation for particular fields ,then use "web form controls" for accepting data and appropriate "validation controls" to do validations. You can meet your requirements by using validation controls (best part is,without writing a single line of code!). Try using validation controls.(Don't stick on to old way of doing things!)

Regards,
Mysore Ramachandra
I very much agree with Ramachandra. There is no point stikcing to old style of ASP and javascript way of programming and calling ourselves .Net programmers.

Look at the following example:
http://aspnet.4guysfromrolla.com/articles/090402-1.2.aspx

It will help you to map a server control with a client site scripting


Regards