Calendar and postback


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Calendar and postback

  1. #1
    Join Date
    Mar 2006
    Posts
    11

    Calendar and postback

    Hi,

    I have a page that contains a repeater and a calendar. I the header section of the repeater i have a label. When i change the date on the calendar i want the label in the header section of the repeater to change to display the date thats been choosen.

    The code ive made almost works. When i first time choose a date nothing happens, when i choose another date afterwards the previous date i chose is displayed. Its like postback is delayed one step.

    Im kinda new to ASP.NET, what could cause this ?

    regards
    Mathias Koizumi

  2. #2
    Join Date
    Dec 2004
    Posts
    717
    Can you show us ur code?

    I have tested in VS Web Express Edition with the following code.
    Code:
     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            Label1.Text = Calendar1.SelectedDate.ToString();
        }
    It's working fine.. It doesn't need to be done any special things.
    So, I think that we can help you more effectively if we got the required code that you used.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  3. #3
    Join Date
    Mar 2006
    Posts
    11
    oki, here goes :

    <script runat="server">


    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
    Session["datovalgt"] = Calendar1.SelectedDate.ToShortDateString();


    }


    protected void Repeater2_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.Header) {
    Label myLabel1 = (Label)e.Item.FindControl("LabelDay1");
    myLabel1.Text = (String)Session["datovalgt"];
    }


    }
    </script>

    <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999"
    CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
    ForeColor="Black" Height="180px" Width="200px" OnSelectionChanged="Calendar1_SelectionChanged">
    <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
    <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
    <SelectorStyle BackColor="#CCCCCC" />
    <WeekendDayStyle BackColor="#FFFFCC" />
    <OtherMonthDayStyle ForeColor="#808080" />
    <NextPrevStyle VerticalAlign="Bottom" />
    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
    <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
    </asp:Calendar>
    <br />
    <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource5" OnItemCreated="Repeater2_ItemCreated">
    <HeaderTemplate>
    <table border='1' width='700' bgcolor="white"><tr>
    <td><strong><asp:Label ID="LabelDay1" runat="server" Text=''></asp:Label></strong></td>
    <td><strong><asp:Label ID="LabelDay2" runat="server" Text=""></asp:Label></strong></td>
    <td><strong><asp:Label ID="LabelDay3" runat="server" Text=""></asp:Label></strong></td>
    <td><strong><asp:Label ID="LabelDay4" runat="server" Text=""></asp:Label></strong></td>
    <td><strong><asp:Label ID="LabelDay5" runat="server" Text=""></asp:Label></strong></td>
    <td><strong><asp:Label ID="LabelDay6" runat="server" Text=""></asp:Label></strong></td>
    <td><strong><asp:Label ID="LabelDay7" runat="server" Text=""></asp:Label></strong></td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>


    </asp:Repeater>

  4. #4
    Join Date
    Dec 2004
    Posts
    717
    Hi sundruid,

    It happens cuz' of the sequence of the events. "Calendar1_SelectionChanged()" Event is fired after Repeater2_ItemCreated() fired. That's why the changs on the label is always delayed one step.

    But I have one solution that I like to share for your problem.

    1) The glabal variable of the page is needed to declared.

    Code:
    Label lbl = new Label();
    2) Session is not necessary to be used.

    Code:
    protected void Repeater2_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
       if(e.Item.ItemType==ListItemType.Header) 
       {
    	Label myLabel1 = (Label)e.Item.FindControl("LabelDay1");
    	lbl = myLabel1;
       }
    }
    3) Set the text to the label variable.
    Code:
    private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {						
       lbl.Text = Calendar1.SelectedDate.ToString();			
    }
    Hope you may find it useful..
    Last edited by Sync; 07-04-2006 at 04:25 PM.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  5. #5
    Join Date
    Mar 2006
    Posts
    11
    Hi Michael,

    thx for your answer

    If the calendar event is fired first, wouldnt the session variable be there when repeater event is fired. I think the sequence is the reverse of what you mentioned according to my logic.

    By the way how do i make the variable global, where in the code shold i place it ?

  6. #6
    Join Date
    Dec 2004
    Posts
    717
    Quote Originally Posted by sundruid
    Hi Michael,
    If the calendar event is fired first, wouldnt the session variable be there when repeater event is fired. I think the sequence is the reverse of what you mentioned according to my logic.
    Oh. Yeah.. Sorry.. I'll update my previous thread..

    By the way how do i make the variable global, where in the code shold i place it ?
    Just declare outside of the any function within the class.

    Here is the whole coding.
    Code:
    Label lbl = new Label();
    
    private void Page_Load(object sender, System.EventArgs e)
    {
    	// Put user code to initialize the page here
    	if(!Page.IsPostBack)
    	{
    		oleDbDataAdapter1.Fill(dataSet21);
    		Repeater2.DataSource = dataSet21.Tables[0];
    		Repeater2.DataBind();
    	}			
    }
    
    private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {						
    	lbl.Text = Calendar1.SelectedDate.ToString();			
    }
    protected void Repeater2_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
    	if(e.Item.ItemType==ListItemType.Header) 
    	{
    		Label myLabel1 = (Label)e.Item.FindControl("LabelDay1");
    		lbl = myLabel1;
    	}
    }
    Free feel to let me know if you wanna get the sample program that I created for your problem.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  7. #7
    Join Date
    Mar 2006
    Posts
    11
    thx a alot, ill try that.

    Regards
    Mathias Koizumi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center