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

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

LVL 1
sspeedy00Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
novynovConnect With a Mentor Commented:
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
 
sspeedy00Author Commented:
I wil award the points, somehow I missed the answer to this one.  Thanks novynov, I will try your solution today, later on.
0
 
sspeedy00Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.