Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Showing alert not working in C#

    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?

    Code:
    void button_click(object sender, EventsArgs e)
    {
    
    if (text1.text == "")
    {
    Response.Write("<script language='JavaScript'>alert('Enter Text');void('');</script>");
    }
    else
    //something
    }

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Brandoe85. That is a great article. I will read it and hope to make it work in my page.

    Thanks again

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Hi,

    Add some the javascript in the head:
    Code:
    <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:
    Code:
    // 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;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •