Solved

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

Posted on 2013-01-13
9
252 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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