Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Response.Redirect is causing some code to skip...? 3

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
US
Hi,

I'm trying to understand why Response.Redirect causes some of my code to be skipped redirecting to the page assigned. I have a button that when click is supposed to due some date validations and Warranty validations. Both of these work fine without adding Response.Redirect to void btnSubmit_Click. But when I do added to the code it causes some of the code in WarrantyValidation() to not be activated and it completely ignores WarrantyValidation(). Is there a reason for this?

Thanks!!!


private void btnSubmit_Click(object sender, System.EventArgs e)
{
DateValidation();
WarrantyValidation();
Response.Redirect("EnterNewData.aspx");
}
 
AT76: The answer is probably very simple; though I don't see it on first glance. What is the nature of your DateValidation() and WarrantyValidation() routines? Perhaps I can run a quick test page here (I suspect this will be a straightaway solution).

So you are saying that the following:
Code:
    private void btnSubmit_Click(object sender, System.EventArgs e)
        {
            DateValidation();
            WarrantyValidation();
            [COLOR=red][b]Exit Sub[/b][/color]
            Response.Redirect("EnterNewData.aspx");
        }
..works fine?
 
Thanks Isadore for helping. Here's the code. Basically DateValidation() and WarrantyValidation() get skipped and the page gets redirected. Do you see what's wrong with my code? Again thanks...



private void DateValidation()
{
System.DateTime submitDate = DateTime.Parse(lblDisplayDate.Text);
System.DateTime reviewDate = DateTime.Parse(txtNextReview.Text);
System.DateTime effectiveUntilDate = DateTime.Parse(txtEffecDate.Text);
if (reviewDate <= submitDate)
{
AlertMsg("'Next Review Date' must be greater than ICR Entry Date!");
}

if (effectiveUntilDate <= submitDate)
{
AlertMsg("'Effective Until Date' must be greater than ICR Entry Date!");
}

if (reviewDate >= submitDate.AddMonths(6))
{
AlertMsg("'Review Date' cannot be greater than 6 months from ICR Entry Date!");
}

if (!txtReturnDate.Text.Equals(""))
{
System.DateTime returnDate = DateTime.Parse(txtReturnDate.Text);
if (returnDate >= submitDate.AddMonths(15))
{
AlertMsg("'Return Date' cannot be greater than 15 months from ICR Entry Date!");
}
}

if (effectiveUntilDate >= submitDate.AddMonths(16))
{
AlertMsg("'Effective Until Date' cannot be greater than 15 months from ICR Entry Date!");
}
}

private void WarrantyValidation()
{
if (RadioButtonList6.SelectedValue.Equals("Yes"))
{
if (txtReturn.Text.Equals(""))
{
AlertMsg("Return % must be entered.");
}
if (txtReStocking.Text.Equals(""))
{
AlertMsg("Re-Stocking Fee % must be entered.");
}
if (txtReturnDate.Text.Equals(""))
{
AlertMsg("Return date must be entered.");
}
}
}
public void AlertMsg(string msg)
{
string sMsg;
StringBuilder sb = new StringBuilder();
msg.Replace("\\n", "\\\\n");
sMsg = msg.Replace("\\", "'");
sb.Append("<script language='javascript'>");
sb.Append("alert( \"" + sMsg + "\" );");
sb.Append("</script>");
Response.Write(sb.ToString());
}
 
How do you know they are being skipped? Do you expect a return error when you enter data? Have you traced the code to verify that the calls to the functions/subs are being skipped?
 
jbenson001: "How do you know they are being skipped? Do you expect a return error when you enter data?" YES

for example: When effectiveUntilDate = submitDate it ignores the message it's suppose to output.

Also for WarrantyValidation(), it ignores the code IF RadioButtonList6.SelectedValue = YES.
 
Ok Have you tried running without the redirect and see if it works?
 
Yes, without the redirect the code works. With it it skips.
 
AT76: Just a shot in the dark; but I have found problems seemingly related to your situation in which, if not Public, variables were (or could become problematic) as the routine travels from one Sub to the next (here 2 external routes).

If I add anything more at this point AT probably would just muddy up the water -- I would follow up on jbenson's suggestion and determined your trace results - post back to keep us informed. This is an interesting problem for sure.

Looking at the code nothing really stands out (you said it worked just fine - so it has to be something more subtle than that).
 
So what happens when you debug the project? Step through it an see what code executes and in what order...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
from wht i saw above ur 2 functions:
DateValidation();
WarrantyValidation();

return JS code (which is client side). when a Response.Redirect() command is send to the browser it simpley redirects thwe page without executing any JS.

instead or response.redrect try this try this:
Response.Write("<script>location.href='EnterNewData.aspx'</script>");


Known is handfull, Unknown is worldfull
 
Wow... That was exactly it vbKris! And thanks for the explanation.

I still do not understand though (sorry!) I'm new to ASP.NET. As you said DateValidation() and WarrantyValidation() are both client side code. I replace:
Response.Redirect("EnterNewData.aspx");
with:
Response.Write("location.href='EnterNewData.aspx'");
This worked.
Is this correct??
1)"Response.write" writes a string to the client response.
2)"location.href" allows you to change the location by simply setting it equal to another URL: 'EnterNewData.aspx'

Is this correct?

Thanks!
 
1)"Response.write" writes a string to the client response.

nope, it writes a header (a Location header). when the browser encounters it, it stops all processing and redirects to the page given in the header. therefore the JavaScript code is never executed (as the page has already been redirected).

2)"location.href" allows you to change the location by simply setting it equal to another URL: 'EnterNewData.aspx'

exactly, but since its done by javascript and since its the last command printed to the screen, the browser will execute all other JS commands before coming to this...

Known is handfull, Unknown is worldfull
 
Very cool. Thanks again for your help!!!
 
vbkris - excellent - my first impression were that the functions were C# (I program in VB) so overlooked that - but an excellent solution!
 
thanx dude...

Known is handfull, Unknown is worldfull
 
You shouldn't use Response.Write to write anything out to the Page. If you want to register any javascript, use RegisterClientScript or RegisterStartUpScript which can be found in the Page or ClientScript class depending on which version of the Framework you are using.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
>>You shouldn't use Response.Write to write anything out to the Page

i would call that a blankent statement...

Known is handfull, Unknown is worldfull
 
Maybe you would but it's with good reason. It's legacy code from classic ASP and if you want to recommend this method to other programmers then someone needs to point out the better methods so that they don't follow the same bad practices.

Here's just one of many blogs on the same subject:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
i called it banket statement in this particular thread why?

because there are absoutely no controls outputted, the poster just wants to get some alert boxes to the users. response.write is bad because it does not suite to the web control structure. but if the page is to have nothing but alert scripts then i would go for it.

infact by doing that i can exactly control what HTML to send to the browser...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top