Connecting Tech Pros Worldwide Forums | Help | Site Map

Changing stylesheet rules

Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84
#1: Jul 1 '09
I have a calender control on my page. It is a popup control, meaning when I click on calender image then it opens up. But the problem is when I am changing the month, it becomes invisible due to stylesheet rule. which is following.

Expand|Select|Wrap|Line Numbers
  1. #datePicker
  2. {
  3.        display:none;
  4.        position:absolute;
  5.        border:solid 2px black;
  6.        background-color:white;
  7. }
  8.  
and the calender is following

Expand|Select|Wrap|Line Numbers
  1. <img src="Calendar.gif" onclick="displayCalendar()" />
  2.  
  3. <div id="datePicker">
  4.     <asp:Calendar
  5.         id="calEventDate"
  6.         OnSelectionChanged="calEventDate_SelectionChanged" 
  7.         Runat="server" />
  8.     </div>
  9.  
  10. function displayCalendar()
  11. {
  12.     var datePicker = document.getElementById('datePicker');
  13.     datePicker.style.display = 'block';
  14. }
  15.  
Can I change style sheet rule dynamically in the page load postback event, so when the month changes I can make display:"block" instead of "none".

kunal pawar's Avatar
Needs Regular Fix
 
Join Date: Oct 2007
Location: Pune, India
Posts: 297
#2: Jul 2 '09

re: Changing stylesheet rules


You can do one thing, create another CSS class for display image and assign that css class using javascript to datePicker
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#3: Jul 2 '09

re: Changing stylesheet rules


Change the <div> that contains the Calendar into a Panel.

When the month is changed set the style of the panel to display:block.
You'll have to change your JavaScript so that it uses the Panel's ClientID. Sometimes when the Panel is rendered in the browser (as a <div>) it's given a different ID than the one you gave it in your .NET code. The reason is because the ID has to be unique....so if you're using MasterPages or User Controls this is changed in order to meet this requirement.

Please note that I'm using VB.NET in the following example...let me know if you're using C#...

Expand|Select|Wrap|Line Numbers
  1. <img src="Calendar.gif" onclick="displayCalendar()" />
  2.  
  3. <asp:Panel Id="datePicker" runat="server">
  4.     <asp:Calendar
  5.         id="calEventDate"
  6.         OnSelectionChanged="calEventDate_SelectionChanged" 
  7.         Runat="server" />
  8. </asp:Panel>
  9.  
  10. function displayCalendar()
  11. {
  12.     var datePicker = document.getElementById('<%= datePicker.ClientID %>');
  13.     datePicker.style.display = 'block';
  14. }
  15.  
In your .net code you'd have:

Expand|Select|Wrap|Line Numbers
  1. Private displayCalEventDate As Boolean = False 'indicates whether or not to display the panel containng the calendar on postback
  2.  
  3. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4.  
  5.      If IsPostBack = True Then
  6.          'Retrieving whether or not to display the panel containing the calanedar
  7.          'from ViewState... it's saved there in the PreRender event.
  8.         displayCalEventDate = Ctype(ViewState("displayCalEventDate"), Boolean)
  9.     End If
  10.  
  11. End Sub
  12.  
  13. Protected Sub calEventDate_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles calEventDate.SelectionChanged
  14.     displayCalEventDate = True
  15. End Sub
  16.  
  17. Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  18.    'The PreRender event happens just before the page is sent to the browser
  19.    'Here Check to see if the calendar's style should be changed to display
  20.    'And save the value for next time (I'm using ViewState here but you don't have to)
  21.  
  22.     ViewState("displayCalEventDate") = displayCalEventDate 
  23.     If displayCalEventDate Then
  24.       datePicker.style.Remove("display")
  25.       datePicker.style.Add("display", "block")
  26.    End If
  27. End Sub
Cheers!
-Frinny
Member
 
Join Date: Jul 2007
Location: Toronto, Canada
Posts: 84
#4: Jul 2 '09

re: Changing stylesheet rules


This is what I have and it is working.

Expand|Select|Wrap|Line Numbers
  1. <img alt="" src="<%=Page.resolveUrl("~/images/Calendar.gif")%>" onclick="displayCalendar('startDate')" />
  2. <div id="startDate">
  3.     <asp:Calendar
  4.     id="calEventDate"
  5.     OnSelectionChanged="calEventDate_SelectionChanged" 
  6.     Runat="server" BackColor="#6DB523" BorderColor="#FFCC66" BorderWidth="1px" 
  7.     DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
  8.     ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" >
  9.     <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
  10.     <SelectorStyle BackColor="#FFCC66" />
  11.     <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
  12.     <OtherMonthDayStyle ForeColor="white" />
  13.     <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
  14.     <DayHeaderStyle BackColor="#41498A" Font-Bold="True" Height="1px" ForeColor="White" />
  15.     <TitleStyle BackColor="#FC5B04" Font-Bold="True" Font-Size="9pt" 
  16.         ForeColor="#FFFFCC" />
  17.     </asp:Calendar>
  18. </div>
  19.  
  20. <img alt="" src="<%=Page.resolveUrl("~/images/Calendar.gif")%>" onclick="displayCalendar('endDate')" />
  21. <div id="endDate">
  22.     <asp:Calendar
  23.     id="calEndDate"
  24.     OnSelectionChanged="calEventDate_SelectionChanged" 
  25.     Runat="server" BackColor="#6DB523" BorderColor="#FFCC66" BorderWidth="1px" 
  26.     DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
  27.     ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" >
  28.     <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
  29.     <SelectorStyle BackColor="#FFCC66" />
  30.     <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
  31.     <OtherMonthDayStyle ForeColor="white" />
  32.     <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
  33.     <DayHeaderStyle BackColor="#41498A" Font-Bold="True" Height="1px" ForeColor="White" />
  34.     <TitleStyle BackColor="#FC5B04" Font-Bold="True" Font-Size="9pt" 
  35.         ForeColor="#FFFFCC" />
  36.     </asp:Calendar>
  37. </div>
  38.  
  39. <!-- Following conditional statement is used to check which calender month was changed -->
  40. <%
  41.     If Session("calName") = "calEndDate" Then
  42. %>        
  43.     <script type="text/javascript">
  44.         var datePicker = document.getElementById("endDate");
  45.         datePicker.style.display = "block";
  46.     </script>
  47. <%  
  48. ElseIf Session("calName") = "calStartDate" Then
  49. %>        
  50.     <script type="text/javascript">
  51.         var datePicker = document.getElementById("startDate");
  52.         datePicker.style.display = "block";
  53.     </script>
  54. <% 
  55.    End If
  56. %> 
  57.  
  58. <!-- This is what I have in vb file -->
  59.  
  60. Protected Sub calEndDate_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles calEndDate.VisibleMonthChanged
  61.     Session("calName") = "calEndDate"
  62. End Sub
  63.  
  64. Protected Sub calEventDate_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles calEventDate.VisibleMonthChanged
  65.     Session("calName") = "calStartDate"
  66. End Sub
  67.  
And this is in the javascript

Expand|Select|Wrap|Line Numbers
  1. function displayCalendar(calID) {
  2.     var datePicker = document.getElementById(calID);
  3.     var notClickedCalenderName = (calID == "startDate" ? notClickedCalenderName = "endDate" : notClickedCalenderName = "startDate");
  4.     var notClickedCalender = document.getElementById(notClickedCalenderName);
  5.  
  6.     if (datePicker.style.display.indexOf("block") > -1) {
  7.         datePicker.style.display = 'none';
  8.     }
  9.     else {
  10.         datePicker.style.display = 'block';
  11.         notClickedCalender.style.display = 'none';
  12.     } 
  13. }
  14.  
Please let me know if there is difference in our approach.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#5: Jul 2 '09

re: Changing stylesheet rules


Well that works too :)
My solution uses less JavaScript than yours does but both will get the job done :)
Reply