Solved

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

Posted on 2013-01-13
9
248 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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 400 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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