how to assign the current date and time in textbox in design?

Posted by Prabu_Spark under ASP.NET on 12/11/2012 | Points: 10 | Views : 11791 | Status : [Member] | Replies : 8
Hi sir,
Tell me the steps to assign the current date and time in textbox in design time. I put the code like this, but i did not get the output. Kindly give me the solution for this problem.


<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Text="<%# System.DateTime.Now.ToString() %>" runat="server"></asp:TextBox>

</div>
</form>

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Pavanandey on: 12/11/2012 [Member] Bronze | Points: 25

Up
0
Down
<script type="text/javascript" language="javascript">
function GetDateTime() {
var param1 = new Date();
var param2 = param1.getDate() + '/' + param1.getMonth() + '/' + param1.getFullYear() + ' ' + param1.getHours() + ':' + param1.getMinutes() + ':' + param1.getSeconds();
document.getElementById('lbl').innerHTML = param2;
}
</script>

<body onload="GetDateTime()">

<asp:Label ID="lbl" runat="server"></asp:Label>

Mark the answer if this helps

Thanks
Pavan Kumar
Mark Answer if this fits the need

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
@Pavan,
Your Code is working for Label only.If we take textbox in that we get unknown error.

Thanks & Regards
Hari

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 12/11/2012 [Member] Starter | Points: 25

Up
0
Down
<script type="text/javascript" language="javascript">
function GetDateTime() {
var param1 = new Date();
var t2=param1.getMonth()+1;
var param2 = param1.getDate() + '/' + t2 + '/' + param1.getFullYear();
document.getElementById('<%=txttime.ClientID%>').value = param2;
}
</script>
</head>
<body onload="GetDateTime()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txttime" runat="server"></asp:TextBox>
</div>
</form>
</body>

Thanks & Regards
Hari

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/11/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
try this

<script type="text/javascript" language="javascript">
function GetDateTime() {
var param1 = new Date();
var t2 = param1.getMonth() + 1;
var param2 = param1.getDate() + '/' + t2 + '/' + param1.getFullYear() + ' ' + param1.getHours() + ':' + param1.getMinutes() + ':' + param1.getSeconds();
document.getElementById('txttime').value=param2;
}
</script>
</head>
<body onload="GetDateTime()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txttime" runat="server"></asp:TextBox>
</div>
</form>
</body>


Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Md_Azaz1 on: 4/21/2013 [Member] Starter | Points: 25

Up
0
Down
you can acheive it by simply using below code in aspx itself no need of javascript
<asp:TextBox runat="server" ID="textbox1" Text='<%# DateTime.Now.ToString() %>'>
</asp:TextBox>

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Annaharris on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
You can use this code to insert date and time in textbox
displaydt.Text = DateTime.Now.ToString();

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 4/23/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

try this Code

TextBox1.Text = DateTime.Now.ToShortDateString();

Mark as Answer if its helpful to you

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 4/23/2013 [Member] Starter | Points: 25

Up
0
Down
hi

textbox1.text=DateTime.Now.ToString();
for that particular textbox ithink it will helpful to you
Thanks& Regards

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response