?
Solved

How to set asp.net calender to set todays date if no active choice is made

Posted on 2013-01-13
9
Medium Priority
?
253 Views
Last Modified: 2013-04-26
Hello!

On my .aspx site the user can pick a date in a calender to state when the record was created. If no choice is made I would like, the aspx-page to insert todays date into the Access-database.

This happens if I create the record directly in the database, but when I create the record via the aspx-page the record gets the date 2001-01-01.

Can this be done in the calender control or do I have to write a sub to achieve this.

For more details se attached pdf-file.

This is the present code for the page:
how-to-get-todays-date-into-data.pdf
0
Comment
Question by:marcgu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 38771925
Hey Try this

<asp:Calendar ID="Calendar1" runat="server"  SelectedDate="<%# DateTime.Now %>" />
0
 

Author Comment

by:marcgu
ID: 38772634
Hi!

Unfortunately, that didn´t work for me. I changed the code in both the edititemtemplate and the insertitemtemplate, but result was that no date at all was inserted into the MS Access Database.
0
 
LVL 21

Expert Comment

by:masterpass
ID: 38772667
If you want todays date to be there in the form, it a good practise that you let user know that if he does not select a date then it would be dafaulted to today's date. You can acheive this in the page load function:

Calendar1.SelectedDate = System.DateTime.Today

Open in new window


This way the date will be preselected when the page loads.

Ref : http://msdn.microsoft.com/en-us/library/8k0f6h1h%28v=vs.80%29.aspx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:marcgu
ID: 38772747
Maybe both your solutions are correct but at this point, I don´t really know exactly where to place the code in my code.

I do have two calender controls in the formview.

1.If possible, tell me exactly where to place the code and what to delete from my code to make it work.
2. Should I change the page load?
3. I will accept an answer saying: Please study the basics first. Maybe thats what I have to do to fully understand. However it would be easier to understand the basics if I had a good example to relate to.





     
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
          DataSourceID="AccessDataSource1" AllowPaging="True">
          <EditItemTemplate>
              ID:
              <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
              <br />
              Beskrivning:
              <asp:TextBox ID="Beskrivning2TextBox" textmode=MultiLine 
					Width=500 Rows=10 runat="server" 
                  Text='<%# Bind("Beskrivning2") %>' />
              <br />
              Todayd:
              <asp:Calendar  ID="Calendar1" runat="server" SelectedDate='<%# Bind("Todayd") %>' ></asp:Calendar>


              <br />
              Lank:
              <asp:TextBox ID="LankTextBox" runat="server" Text='<%# Bind("Lank") %>' />
              <br />
              Rubrik:
              <asp:DropDownList ID="dropdownlist1"
DataSourceID="accessdatasource2"
DataTextField="Rubrik"
DataValueField="idnr"
SelectedValue='<%# Bind("IDNR") %>' runat="server" >
</asp:DropDownList>


<br />

              <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                  CommandName="Update" Text="Uppdatera" />
              &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                  CausesValidation="False" CommandName="Cancel" Text="Avbryt" />
          </EditItemTemplate>
          <InsertItemTemplate>
              Beskrivning2:
              <asp:TextBox ID="Beskrivning2TextBox" textmode=MultiLine 
					Width=500 Rows=10 runat="server" 
                  Text='<%# Bind("Beskrivning2") %>' />
              <br />
              Todayd:
             <asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%# Bind("Todayd") %>' 
    ></asp:Calendar>
              <br />
              Lank:
              <asp:TextBox ID="LankTextBox" runat="server" Text='<%# Bind("Lank") %>' />
              <br />
              Rubrik:
              <asp:DropDownList ID="dropdownlist1"
DataSourceID="accessdatasource2"
DataTextField="Rubrik"
DataValueField="IDNR"
SelectedValue='<%# Bind("IDNR") %>' runat="server" >
</asp:DropDownList>
              <br />
              <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                  CommandName="Insert" Text="Lägg till" />
              &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                  CausesValidation="False" CommandName="Cancel" Text="Avbryt" />
          </InsertItemTemplate>
          <ItemTemplate>
              ID:
              <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
              <br />
              Beskrivning:
              <asp:Label ID="Beskrivning2Label" runat="server" 
                  Text='<%# InsertLink2(Eval("Beskrivning2")) %>' />
              <br />
              Todayd:
              <asp:Label ID="TodaydLabel" runat="server" 
                  Text='<%# Bind("Todayd") %>' />
              <br />
              Lank:
              <asp:Label ID="LankLabel" runat="server" Text='<%# bind("lank") %>' />
              <br />
              Rubrik
              <asp:Label ID="IDNRLabel" runat="server" Text='<%# Bind("idnr") %>' />
              <br />
              <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                  CommandName="Edit" Text="Ändra" />
              &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                  CommandName="Delete" Text="Radera" />
              &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                  CommandName="New" Text="Ny post" />
          </ItemTemplate>
                <emptydatatemplate>
          <table>
            <tr>
              <td>
               <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                  CommandName="New" Text="Ny post" />
              </td>
             
            </tr>
          </table>
        </emptydatatemplate>
      </asp:FormView>

Open in new window

0
 
LVL 21

Accepted Solution

by:
masterpass earned 1600 total points
ID: 38772773
I hope this will answer your question : http://stackoverflow.com/questions/5854320/set-current-date-in-formview

Happy coding :)
0
 

Expert Comment

by:kottamgari
ID: 38791877
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If Calendar1.SelectedDate = Nothing Then
        '    TextBox1.Text = DateTime.Today.ToShortDateString()
        'End If
        Calendar1.SelectedDate = DateTime.Today.ToShortDateString()
        TextBox1.Text = Calendar1.SelectedDate.ToShortDateString()
    End Sub

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        TextBox1.Text = Calendar1.SelectedDate.ToShortDateString()
    End Sub
0
 

Author Comment

by:marcgu
ID: 38791919
Thanks for all comments: I will evaluate all suggestions in the next couple of days.
0
 

Author Comment

by:marcgu
ID: 38896495
Hi!

I am still very grateful for all comments, but I have not so far been able to integrate any of the solutions into my code to make it work. Probably, all suggested solutions are correct but I just do not seem exactly where to integrate them into my code or I am making some other misstake.
/Marcus
0
 

Author Comment

by:marcgu
ID: 38927178
Hi!

I am still working on this question. Hopefully I will be able  to accept one of the solutions in the next week.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

801 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