3

I've used this command since I start my new project on ImageButton_Click Event, and it's working perfect in every page that I'm using this: I first, check if there's a field empty if yes then I send this alert.

ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('No Empty fields are allowed.');", true);  

What I'm doing here: I have 2 textboxes that I type two dates, these dates are used to fill my reportviewer, but before it goes to the reportviewer, I check if it's empty or not.

In this page it's working only in the first time I press the button. I used breakpoint, and it's passing OK throught my conditional IF, but it's not showing the message... Why?
Full Code on a Button_Click Event:

 if (Txt_Dt_Final.Text == "" || Txt_Dt_Inicial.Text == "")
     {
        ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('No Empty fields are allowed.');", true);
        Txt_Dt_Inicial.Focus();
        return;
     }

UPDATE
Actually, after I press it by the first time, nothing works anymore... Even if I type a valid date, it is not working... I tested it now and, the error only appears, if the error hapens the first time you press the button.
e.g.:
If I type the dates OK, then my reportviewer will return me the result of my report... Then I click on the button with empty textboxes, it will fire the error and show me the message. If I press again it will work fine !

Now, if I press the button with empty fields for the first time, the button does not work anymore ! It just back working if I refresh the page... Why ? o.O
All my others pages are working perfect.

Ghaleon
  • 1,186
  • 5
  • 28
  • 55
  • Is the image button inside an update panel? – Andrew Walters Apr 18 '13 at 20:44
  • @AndrewWalters Yes, it is. – Ghaleon Apr 18 '13 at 20:57
  • The only thing I see that I would do different is use a unique ID for the script key ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "alert('No Empty fields are allowed.');", true); Maybe the script's getting cached – Andrew Walters Apr 18 '13 at 21:13
  • If all else fails you can always use the .net RequiredFieldValidators on your page – Andrew Walters Apr 18 '13 at 21:14
  • It seems like you have to register the script every postback. I don't understand why because the script seems to be put in twice. would love to know the correct/elegant answer. – Leo Nov 08 '13 at 14:04
  • possible duplicate of [ScriptManager.RegisterStartupScript code not working - why?](http://stackoverflow.com/questions/4994040/scriptmanager-registerstartupscript-code-not-working-why) – CodeArtist Jan 05 '15 at 05:26

1 Answers1

6

Your code should work unless you have your ASP controls inside an update panel. If you do then use this instead.

ScriptManager.RegisterStartupScript(updatePanel,updatePanel.GetType()
                                                  , "alert", javaScript, true);
Leo
  • 1,495
  • 23
  • 41
Prashant
  • 118
  • 6
  • 1
    But I've always used this inside a `UpdatePanel` and always worked... ;s I just don't understand why the error only on this case.. – Ghaleon Apr 19 '13 at 11:46