Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using MsgBox in validation 1

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
0
0
US
This code is from VB.NET, could I use this style of validation in ASP.NET ? Would the MsgBox function work?

Private Function ValidateData() As Boolean

Dim dataInValid As Boolean = True

If Not IsDate(txtHireDate.Text) Then
MsgBox("Hire Date must be a date in the format MM/DD/YY.", _
MsgBoxStyle.Exclamation, Me.Text)
dataInValid = False
End If

If txtSocialServicesID.Text.Length <> 11 Then
MsgBox(&quot;Social Services ID must be 11 characters in length&quot;, _
MsgBoxStyle.Exclamation, Me.Text)
dataInValid = False
End If

Return (dataInValid)

End Function

dpd
 
Nope - not at all. MsgBox is not part of server side code, even in classic asp.

The best solution is to investigate the user of the validation controls, which come as standard part of ASP.net.

If you really need a message box, you could do one through client script.

Hope this helps.

Mark [openup]
 
You'll have to generate a client side script from your server side valication sub.

If Not IsDate(txtHireDate.Text) Then

response.write(&quot;<script language='javascript'>alert&quot; _
& &quot;('Hire date is not valid.')<&quot; & &quot;/&quot; &&quot;script>&quot;)

end if
 
Ideally you'd use a combination of what Mark and Tom have suggested.

The validation controls are a definate must for presentation-layer validation. However, if you generate an error way down where your code accesses the database, those validation controls won't help you.

Thats where Tom's idea comes in, so that you can display the error message to the user.

The way I generate my error messages is to set my try catches up so that any error message will buble up to the top-most calling sub/function (usually a control event, like a button click or something like that). Then, in that control's catch, I'll put something like Tom suggested (I have a label on every page called lblError that I set the text to be pretty much what Tom has suggested. Response.write would work as well though).

you can also add things like creating a custom error object that can log errors and display custom error messages.

The other thing you may want to take a look at is Microsofts Error Handling block, which you can find on msdn. Its a block of code that will take care of all the error-handling framework within your app. Something to at least look at anyway.

hth

D'Arcy
 
Validation controls, particularly the validation summary control are really good controls that can help make the page validation robust. The javascript popup I posted is good when you can't, for whatever reason, use asp validation controls, but if you can use them they are the way to go for a solid app.

Good luck with it.

Tom T
 
What if you definitely need to use a MsgBox solution? For example to delete a record you need to ask the question for a Yes/No answer?

if Msgbox(&quot;Delete Record?&quot;, vbYesNo ) = vbYes
'delete record
else
'Exit Sub
Sub

How can you accomplish this in ASP.Net? The &quot;alert&quot; javascript function that Tom suggested anyway only accepts &quot;OK&quot;. What if you have to make a choice between Yes & No and run code back on the server?.

Please help or provide sample!
 
Never mind .. I think I found a thread from Mark that will help answer my question. I will try it out first! I was still researching when I posted the question?

thread855-467496
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top