Solved

PopUp Calendar Not Working Properly (Calendar Control from .NET 2.0)

Posted on 2008-10-10
4
1,460 Views
Last Modified: 2013-12-17
Hi again,
I'm following this tutorial: http://www.dotnetjohn.com/Articles.aspx?articleid=67

Basically, I'm working with a Calendar control.  I have a textbox and right beside it, a little calendar image.  User clicks on calendar, clicks on a date, the date should automatically (code does the work), populate the textbox with the selected date.  

Welp, mine's not doing that :(  The article contains VB, so maybe I'm missing something.  I'd like to k now what the hidden textbox control does, and how exactly are we passing the date into the textbox.  I will post my related code, but it'll probably make sense to look at the tut first.



So, why isn't my textbox tbBeginDate getting populated? TIA.

// contains some comments, please ignore!

//this is within PopUp.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

   this.control.Value = Request.QueryString["textbox"].ToString();

   Response.Write("control.Value: " + control.Value);

  

            }

   

   //don't think this part's working...

           protected void Calendar1_SelectionChanged(object sender, EventArgs e)

           {

              Calendar cal = (Calendar)sender;

  

               //Response.Write(cal.ID);

               //this.tbBeginDate.Text = cal.SelectedDate.ToString().Trim();

               //this.cldBeginDate.dst

               Response.Write("control.Value: " + control.Value);

               Response.Write("Selected Date: " + cal.SelectedDate.ToString());

               string scriptString = "<script>window.opener.document.forms(0)." + control.Value + ".value = '";

               scriptString += cal.SelectedDate.ToString();

               scriptString += "';self.close()";

               scriptString += "&lt;/" + "script>";

               Response.Write("        scriptString: " + scriptString);

               //ClientScript.RegisterClientScriptBlock(this.GetType(), "anything", scriptString);

               Page.RegisterClientScriptBlock("anything", scriptString);

           }

   

  
 

//markup designer stuff etc.
 

    // my popup.aspx stuff

            <asp:Calendar ID="cldBeginDate" runat="server" BackColor="White" BorderColor="Navy"

    BorderStyle="Double" OnDayRender="cldBeginDate_DayRender" OnSelectionChanged="Calendar1_SelectionChanged">

                <SelectedDayStyle BackColor="Navy" />

                <SelectorStyle BackColor="Navy" />

               <NextPrevStyle BorderColor="White" BorderStyle="None" ForeColor="Navy" />

           </asp:Calendar>

    

    <input type="hidden" id="control" runat="server" />

   

   //lastly, in my main.aspx page:

               <a href="javascript:;" onclick="window.open('popup.aspx?textbox=tbBeginDate','cldBeginDate','width=250,height=225,left=270,top=180')">

               <img src="images/SmallCalendar.gif" border="0" style="left: 175px; position: absolute; top: 255px"></a>

Open in new window

0
Comment
Question by:sspeedy00
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
novynov earned 350 total points
ID: 22693950
So, I was able to get your code to work. I needed to do a couple of things:

- I added an <input type="text" id="tbBeginDate" /> to your calling page (main.aspx). This is the control that should be populated by the selected calendar date. I didn't see it in your code snippet. Did I miss something?
- I changed part of the client script generation function in Calendar1_SelectionChanged to match that of the tutorial. See code snippet #1 below.

To answer your question:

- The hidden control isn't a hidden text box control. It is a hidden control (a different type of input tag). In this tutorial, they are using it to store the querystring value which represents the id of the control that you want the date written to (i.e. the textbox). Notice in the main.aspx onclick handler that "tbBeginDate" is passed on the qstring as the value of textbox. In popup.aspx, this value is stored as the value of the hidden control, and then used later when emitting the javascript in Calendar1_SelectionChanged. That code builds up a reference to the textbox by appending the value of the hidden control (i.e. "tbStartDate")  to window.opener.document.forms(0). The whole line of code ends up generating javascript that says something like:

window.opener.document.forms(0).tbStartDate.value = "10/11/2008 6:00 AM";

"window.opener" is a reference back to main.aspx. The javascript is looking for the textbox control on that page and setting its value to the date.

That said, the hidden control really isn't necessary. You could easily just use a private string variable for the same thing in popup.aspx. See snippet #2 below. I did it this way as well, and it worked fine. I haven't read through the tutorial to see if there was a specific reason the author did it that way.

I hope that helps. Please let me know.


//Code snippet #1
 

//Your code

//scriptString += "&lt;/" + "script>";
 

//Changed to match tutorial

scriptString += "</" + "script>";
 

Before I did this, I was receiving javascript errors. Stepping through the SelectionChanged event handler and watching the scriptString value get built up revealed that the final tag of your script was getting generated as "&lt;/script>" instead of "</script>". I'm not sure why you had changed it to use the html encoded version of "<".
 

//Code snippet #2
 

       private string controlId;
 

        protected void Page_Load(object sender, EventArgs e)

        {

            //Save the value of the textbox id for later

            controlId = Request.QueryString["textbox"].ToString();

            

            //Write it out on the popup page

            Response.Write("control.Value: " + controlId);

            

        }

   

   

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)   

        {

      

            //Grab the calendar

            Calendar cal = (Calendar)sender;
 

                   

            //Write out the important values

            Response.Write("control.Value: " + controlId);       

            Response.Write("Selected Date: " + cal.SelectedDate.ToString());       

            

            //Build up the javascript call

            string scriptString = "<script>window.opener.document.forms(0)." + controlId + ".value = '";       

            scriptString += cal.SelectedDate.ToString();

            scriptString += "';self.close()";       

            

            //scriptString += "&lt;/" + "script>";       

            scriptString += "</" + "script>";       

            

            //Write out the script string so I can see it

            Response.Write("        scriptString: " + scriptString);       

            

            //Register the script

            Page.RegisterClientScriptBlock("anything", scriptString);

   

        }
 
 

       

Open in new window

0
 
LVL 1

Author Comment

by:sspeedy00
ID: 22895006
I wil award the points, somehow I missed the answer to this one.  Thanks novynov, I will try your solution today, later on.
0
 
LVL 1

Author Closing Comment

by:sspeedy00
ID: 31505086
I have not been able to test the solution, but you seem to have tested it.  I will give it a shot later on today.  Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now