Link to home
Start Free TrialLog in
Avatar of dhk3140
dhk3140

asked on

Using a popup calendar control

I have a calendar control in a popup window that worked fine in ASP.NET 1.x; but now that I've upgraded to ASP.NET 2.0/VS 2005 it's quit working.  It's supposed to fill in a text box on the original form with the date selected and close, but it just does nothing.

The code in the form that opens the popwindow is:

<a href="javascript:calendar_window=window.open('calendar.aspx?formname=Form1.txtOpenDate','calendar_window','width=154,height=188');calendar_window.focus()">Calendar</a>

This seems to work ok.

The calendar.aspx code is:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="calendar.aspx.vb" Inherits="GamesWanted_AWWCP_calendar" %>
<script language="JavaScript">
<!--

function SymError()
{
  return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;

function SymWinOpen(url, name, attributes)
{
  return (new Object());
}

window.open = SymWinOpen;

//-->
</script>

<script runat="server">
    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs)
        If e.Day.Date = DateTime.Now().ToString("d") Then
            e.Cell.BackColor = System.Drawing.Color.LightGray
        End If
    End Sub
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
    <title>Choose a Date</title>
</head>
<body leftmargin="0" topmargin="0">
    <form id="Form1" runat="server">
        <asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_dayrender" showtitle="true" DayNameFormat="FirstTwoLetters" SelectionMode="Day" BackColor="#ffffff" FirstDayOfWeek="Monday" BorderColor="#000000" ForeColor="#00000" Height="60" Width="125" >
            <TitleStyle backcolor="#000080" forecolor="#ffffff" />
            <NextPrevStyle backcolor="#000080" forecolor="#ffffff" />
            <OtherMonthDayStyle forecolor="#c0c0c0" />
        </asp:Calendar>
        <asp:Literal id="Literal1" runat="server"></asp:Literal>
    </form>
</body>
</html>

And the codefile is:


Partial Class GamesWanted_AWWCP_calendar
    Inherits System.Web.UI.Page


    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged

        Dim strjscript As String = "<script language=""javascript"">"
        strjscript = strjscript & "window.opener." & HttpContext.Current.Request.QueryString("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"
        strjscript = strjscript & "</script" & ">" 'Don't Ask, Tool Bug
        ClientScript.RegisterClientScriptBlock(Me.GetType, "returnDate", strjscript)
        Literal1.Text = strjscript
    End Sub

End Class

Thanks in advance,

Daniel
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Hi dhk3140,

Just so we're clear, it sounds like the calendar pop-up is opening, but when you select a day on the calendar, nothing appears to happen, including no TextBox being updated and window not closing, right?

If so, this tells me there is probably an issue with this line:
strjscript = strjscript & "window.opener." & HttpContext.Current.Request.QueryString("formname") & ".value = '" & Calendar1.SelectedDate & "';window.close();"

So let's just validate that the TextBox control is truly at Form1.txtOpenDate, by doing a View Source of your original page.  Find the txtOpenDate TextBox and make sure that the ID property is set to "txtOpenDate" and that the <form> has an ID of "Form1".  If this is all true, we'll probably attempt changing the line I referenced above... but let's see what you come back with.

Good luck!
-- Jason
Avatar of dhk3140
dhk3140

ASKER

Jason:

Here's the textbox code:

<asp:TextBox ID="txtOpenDate" Runat="server" MaxLength="10"></asp:TextBox>

Form code:

    <form id="form1" runat="server">


I don't know if it makes a difference or not, but the form is declared in a master page, and the text box is in a content page.  I'm sorta thinking this might be related to the problem, but haven't had a chance to check.  I'd really like it to be master/content pages tho.

Thanks,

Daniel
dhk3140,
You can do what you want, but what's going to happen when you place your code within the Master Pages scheme with content panes it's going to change the ID of every control that is inside of those panes.  This is fine, but you need to be aware of that when you write JavaScript code that tries to reference a specific control.

So what I was hoping you could do was actually load your page in a browser and go to (in IE) the View menu and select Source.  So we can see what the new name of the txtOpenDate control is.

It's going to probably be something like:
ctl00_contentHolder_txtOpenDate

So you'll need to replace your original page's call to the window.open method wtih the appropriate ID.
Avatar of dhk3140

ASKER

scolja,

So what you were looking for was:

<input name="ctl00$ContentPlaceHolder1$txtOpenDate" type="text" maxlength="10" id="ctl00_ContentPlaceHolder1_txtOpenDate" />

And Form1.ctl00_ContentPlaceHolder1_txtOpenDate is what I need to reference instead of Form1.txtOpenDate in the script?

Regards,

Daniel
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dhk3140

ASKER

scolja,

Actually, the form name changed to aspnetForm.  Once I added that, it worked!

Your a genius!  Thanks!

Daniel