Solved

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

Posted on 2008-10-10
4
1,462 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

11 Experts available now in Live!

Get 1:1 Help Now