Kerzoz
Kerzoz

Reputation: 331

Response.Write not working in ASP.NET

I have a calendar named poDateCalendar, I wanted to make it so whenever the user selects the calendar to be any earlier than today's date, there will be a response as follows:

protected void poDateCalendar_SelectionChanged(object sender, EventArgs e)
    {
        if (poDateCalendar.SelectedDate < System.DateTime.Today)
        {
            Response.Write("<script>alert('Warning: you are selecting a PO date earlier than today's date')</script>");
        }
        poDateCalendar.Visible = false;
        poDateBtn.Text = poDateCalendar.SelectedDate.ToShortDateString();
    }

It worked the first time I tried it, but somehow now it doesn't work anymore, is there anything I did wrong?

I also tried doing breakpoints, it passed through the if statement and did run the response.write. However, there's just nothing that is displayed after.

Upvotes: 1

Views: 3159

Answers (3)

Nikhil Vartak
Nikhil Vartak

Reputation: 5117

I don't quite get the point - Allow users to do what they're not supposed to do, and then shout when they do so. + Message box / alerts are the old school ways of doing things. That's a bad user experience, IMHO.

I have a better suggestion here for you. You could instead disable to previous dates so that they can't select in first place. You can use DayRender event for that.

<asp:Calendar ID="poDateCalendar" runat="server" OnDayRender="poDateCalendar_DayRender" />

In code-behind:

protected void poDateCalendar_DayRender(object sender, DayRenderEventArgs e)
{
     if (e.Day.Date < DateTime.Today)
     {
         e.Day.IsSelectable = false;
         // Additionally grey out dates if you want
     }
}

Upvotes: 0

Naveen Gogineni
Naveen Gogineni

Reputation: 306

Just replace your code with the following line

Response.Write("<script> alert(\"Warning: you are selecting a PO date earlier than today's date\");</script>");

In your case your code is failing because you have a single quote at "today's" which is making your code to fail. So I am using escape sequence at start and end of alert message.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415690

Check the raw HTML sent to the browser. Likely that script was rendered before the opening html tag. There is no page or DOM yet, and so even if the browser ran the javascript at all it wouldn't know what to do with it.

This happens because of where you are in the ASP.Net Page Life Cycle at the time that code runs. A page class in ASP.Net works by first building up all of the controls and properties to have the correct data. Then, once that is all finished, there is a rendering phase where those controls are transformed into HTML for your browser. Before this rendering phase, anything you send to Response.Write() is going to a response stream that hasn't even sent headers to the browser yet.

To get around this, instead of writing to the Response stream direclty, use the Page.RegisterStartupScript() method.

Upvotes: 0

Related Questions