...

View Full Version : Showing alert not working in C#



dotnet11
12-23-2006, 04:28 AM
I created a form and now I have to write validation. I want to show some kind of alert or message box. I wrote the following code. When the user clicks on the submit image, I get the alert but at the same time the web page is blanking out. How to make the page stay without blanking?



void button_click(object sender, EventsArgs e)
{

if (text1.text == "")
{
Response.Write("<script language='JavaScript'>alert('Enter Text');void('');</script>");
}
else
//something
}

Brandoe85
12-26-2006, 08:45 PM
Hi,

Take a look at this message box example:
http://aspnet.4guysfromrolla.com/articles/021104-1.aspx

If you'd like others, do a quick Google for asp.net message box.

Good luck.

dotnet11
12-27-2006, 02:34 AM
Thanks Brandoe85. That is a great article. I will read it and hope to make it work in my page.

Thanks again

dotnet11
12-27-2006, 05:07 PM
Brandoe85, I read the article and my situation is a bit different. In my case, I have a text box and a go button. The user have to type a word in the text box and when he clicks the go button it searches for articles. So when I tried the code it always displaying the alert, whether you enter a word or not. I thing this is because of IsStartupScriptRegistered. So even if a user enters some text in the text box and submits, it displays the alert and when I click the OK then it searches for the articles/journals. But I dont want that. It shouldnt display any alert once there is something in the text box.

Brandoe85
12-27-2006, 09:59 PM
Hi,

Add some the javascript in the head:


<script type="text/javascript">
function checkText()
{
var elm = document.getElementById('text1');
if(elm.value == '')
{
alert('please enter something');
return false;
}
return true;
}
</script>
Add the onsubmit handler in the form tag:


// my example form
<form id="Form1" method="post" runat="server" onsubmit="checkText();">
Then do your server validation as well in your button click. Good luck;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum