0

I have a textbox and a dropdownlist. The textbox is defaulted to 1 but if the user enters a different value into the textbox they must select a value from the dropdownlist. If they do not select a value from the dropdownlist then the exception message appears, and the changes they made to the textbox will not be saved. The problem is the value in the textbox is still there when I reopen the page. If I manually refresh the page, then it defaults back to 1. So I am trying to refresh the page after the exception message appears, but if I put code to refresh the page after the exception then the message doesn't appear anymore. How can I refresh the page after the user closes the exception message?

JobPieceSerialNo SerNo = new JobPieceSerialNo(job.ID);
if (SerNo.Reason == null)
{
         throw new Exception("Must select reason");
}

catch (Exception ex)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Message",
                                          "<script>$(document).ready(function () {  $(\"<div>" + "Please note : " + ex.Message + "</div>\").dialog({modal: true,title: \"NOTE\",buttons: [ { text: \"Ok\", click: function() { $( this ).dialog( \"close\" ); } } ]}); ShowHidePointToPoint('OVERNIGHT');});</script>");
    Response.Redirect("Job.aspx?JobID=" + Request.QueryString["JobID"], false);
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
user123456789
  • 1,914
  • 7
  • 44
  • 100

2 Answers2

0

You almost certainly don't want to be throwing an Exception here, it looks like you're using an exception to handle normal UI validation and if so that's quite unusual.

There are some pretty standard ways of validating user input and presenting validation results back to the user, this article describes simple c# validation which seems to fit your problem better than throwing and catching an Exception.

jonnarosey
  • 520
  • 1
  • 8
  • 19
  • The only reason I was using an exception is because I was having trouble displaying a message. An exception seemed to be the only thing that would work. – user123456789 Aug 17 '15 at 13:21
0

You can use windows.location after closing dialog

your catch would be like this:

    catch (Exception ex)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Message",
                                          "<script>$(document).ready(function () {  $(\"<div>" + "Please note : " + ex.Message + "</div>\").dialog({modal: true,title: \"NOTE\",buttons: [ { text: \"Ok\", click: function() { $( this ).dialog( \"close\" ); window.location.href = 'Default.aspx'; } } ]}); ShowHidePointToPoint('OVERNIGHT');});</script>");

}

or you can also change the window location to the current page in close function of your dialog.

JQuery UI dialog have a close event:

    $( ".selector" ).dialog({
  close: function( event, ui ) {window.location.href ="Job.aspx";}
});
Aria
  • 3,724
  • 1
  • 20
  • 51
  • I need the window.location to go to follow this: Response.Redirect("Job.aspx?JobID=" + Request.QueryString["JobID"], false); I need to check which job ID is being used and refresh that page. Just refreshing Job.aspx gets me out of the current job page I'm in – user123456789 Aug 17 '15 at 13:28
  • Also I noticed it takes a second to redirect the page after I close the exception message. If there anyway to speed this up? – user123456789 Aug 17 '15 at 13:29
  • @user123456789 It is possible to get query string in JS and append it in your redirect page take a look at [THIS LINK](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) `window.location.href = "Job.aspx?JobID=" + getParameterByName('JobID')+ "";` – Aria Aug 17 '15 at 13:54