Make Today's Date show as the Default Value of ASP Textbox

I have From and To dates that are passed to Crystal Reports as date parameters.  Using this code:
<asp:textbox id="txtFrom" text="01-01-2006" runat="server" MaxLength="10" CssClass="input" Width="96px"></asp:textbox>

I have January 1, 2006 as the default From date already displayed when the user clicks the Reports tab.

How do I get today's date to display as the Default Value for my txtTo textbox?
LVL 4
jcampanaliAsked:
Who is Participating?
 
alorentzConnect With a Mentor Commented:
Add this:

<script runat="server">
    Sub Page_Load(sender as Object, e as EventArgs)
       If Not Page.IsPostBack then
           txtTo.Text = DateTime.Now.ToString("d")  '<--------NEW LINE
       End If
    End Sub
</script>

If you already have this sub...

Sub Page_Load(sender as Object, e as EventArgs)

just add the new line in the middle...
0
 
Carl TawnSystems and Integration DeveloperCommented:
In your code behind:

    txtTo.Text = DateTime.Now.ToShortDateString();
0
 
jcampanaliAuthor Commented:
need more detail

The asp page is ReportMenu.aspx.
Our India programmers wrote this page.
In the top of the document, I see where it says CodeBehind="ReportMenu.aspx.vb", but this file does not exist.

Next?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Carl TawnSystems and Integration DeveloperCommented:
It has to exist otherwise the page won't compile.

With Source view active, go to the View menu and select Code. That should take you to the code-behind.
0
 
jcampanaliAuthor Commented:
I do not have VB or VS.NET, I am on the server where the pages are hosted and just making changes to the file there.
0
 
jcampanaliAuthor Commented:
All I have is ReportMenu.aspx  I cannot access CodeBehind

I don't see where I can add these lines.  I tried several places and they do not change the Page_Load behavior.
0
 
alorentzCommented:
The code I gave can go right in the page...but if there is codebehind, I guess it won't work.

Try Javascript then.


<script>
dt=new Date(intYear,month-1,day);
dt=dt.toString().replace(/\d{2}\:\d{2}\:\d{2}[ \w]*/,' ');
document.getElementById('txtTo').value = dt;
</script>


Put that at the bottom of the page.
0
 
jcampanaliAuthor Commented:
For being a big help, alorentz gets the points.  i know the code would work if I had access to code behind.  India fixed the "to" date for me overnight.

Regards
0
 
alorentzCommented:
Glad to help...could try the Javascript ... just to see if it works <wink>...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.