Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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