Solved

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

Posted on 2013-01-13
9
244 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
Comment Utility
Hey Try this

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

Author Comment

by:marcgu
Comment Utility
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
Comment Utility
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
 

Author Comment

by:marcgu
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 21

Accepted Solution

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

Happy coding :)
0
 

Expert Comment

by:kottamgari
Comment Utility
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
Comment Utility
Thanks for all comments: I will evaluate all suggestions in the next couple of days.
0
 

Author Comment

by:marcgu
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now