Message Box in C# Web Application

B

ben

I have a web application whic connects to the database and populates a
datagrid based on 2 fields the user enters on the web form.
I have a try catch block to make sure that in case the user enters
values(dates) not found in database, then i redirect the user to the
origianl page where they enter the values.

I would like a message box saying they entered invalid dates(not a
check for date format, but that they entred dates which dont return
any values in the db, n hence the db error)

so far, i have this
catch
{

Response.Write("<script>window.alert('No Entries Found.')</script>");
}

I would like the user to be redirected to the date entry field after
clicking this messagebox.How do i do that? If i try to add
Response.Redirect("Main.aspx") after the response.write, it ignores
the message box and simply redirects, whic is not what i want.

Any inputs wud be appreciated

THanks
 
K

Kevin Spencer

Response.Redirect occurs on the server, while the Page is processing.
Therefore, if you put in any code that writes anything to the Page, it will
not happen, as the current Page is unloaded before it ever reaches the
browser. If you want the page to redirect on the client side (that is, after
the alert box pops up on the client), your simplest solution would be to use
JavaScript to do it (document.location = url;). If you need to do more
processing on the server prior to redirecting, you could use JavaScript to
post the form and do the Redirect on the server side.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
B

Ben Arthur

Kevin,
Thanks for ur immediate reply.
Can u plz post soem sample code for doing the document.location code, as
i m not familiar with this.
Thanks
Ben
 
B

Ben Arthur

ok...figured out the syntax for
window.document.location('blahblah.aspx')...
howevre it doe snot do anything n does not redirect either....

any ideas?
 
K

Kevin Spencer

ok...figured out the syntax for
window.document.location('blahblah.aspx')...
howevre it doe snot do anything n does not redirect either....

any ideas?

Not really. It's just client-side JavaScript, and it will work if it is used
correctly. I have no idea how you've used it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
B

Ben Arthur

ok...figured out the syntax for
window.document.location('blahblah.aspx')...
howevre it doe snot do anything n does not redirect either....

any ideas?
 
B

Ben Arthur

this is what i have so far

catch
{
Response.Write("<script>window.alert('No Entries Found.Please
')</script>");
Response.Write("<script>window.document.location('Menu.aspx')</script>")
;
}

so after i display the popup, i redirect it, is this the correct way? or
shud i use the script in the aspx page....if so how do i call it from
here(cs page)

Thanks for ur help
 
G

Guest

Ben

First of all, the javascript command is

window.document.location.href = "blah.aspx"

or, just

window.location.href = "blah.aspx"

You can also use

window.navigate("blah.aspx")

But some browsers (older IE, current IE/Mac and current Safari) don't like window.navigat

So, be careful

- - - - - - - - - - - - - -

Now, back to your original question about putting up a message box followed by the positioning on the date

Let's say you're getting the dates from GetDates.aspx and showing the grid on ShowActivity.aspx. In GetDates.aspx, the form's ID is OptionsForm, and the textbox you want to move to automatically is called FromDateTextBox

On ShowActivity.aspx, as soon as you know there's no activity, go back to GetDates.aspx with a parameter that flags for nothing found (be sure to do this before writing anything to the page; you'll get an error otherwise)

Response.Redirect("GetDates.aspx?NoActivity=Y", true)

Then, on GetDates.aspx, put the following

1. Alter your BODY tag so it looks like this
<body onload="OptionalNoActivityMessage();"

2. In the HEAD section, put something like this (this is ASP-ish rather than ASP.NET'ish, but I don't know a better way right now to get stuff into the HEAD section)
<head><script language="JavaScript"
function OptionalNoActivityMessage()
<
if (Request.QueryString["NoActivity"] != null && Request.QueryString["NoActivity"].Equals("Y"))
Response.Write("window.alert('Nothing found...');")
Response.Write("window.document.OptionsForm.FromDateTextBox.focus();")

%
} // Closing brace for client-side function
</script></head

Here's the chain of events

1. User enters dates in GetDates.aspx, then submit
2. ShowActivity.aspx looks for activity, sees none, and redirects back to GetDates.aspx with a parameter that indicates nothing was found
3. In GetDates.aspx, because the "Not Found" parameter was set, the SERVER code inserts two CLIENT javascript commands that cause the browser to
3a. Show a messagebo
3b. Set the focus to the first date control after the user clears the messagebox

I hope this helps

Regards
Ed
 
K

Kevin Spencer

Ed figured out the problem with your syntax. Use his.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top