[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
?
255 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
Independent Software Vendors: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

650 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