Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-10
4
Medium Priority
?
1,471 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 1400 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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