Setting value of textbox

liunx

Guest
I am attempting to setup a textbox control with visibility of false that I want to contain a value for datetime=now. This textbox will hold the value of the datetime for when the record is inserted into the database.

<asp:TextBox ID="Rec_Date" runat="server" Visible="false" Text='<%# Bind("Rec_Date") %>'><%# System.DateTime.Now.ToString() %></asp:TextBox>

The problem I am experiencing is that I get an error stating that server blocks are not allowed in this context.

Can someone inform me as to how this is done in asp.net2.0?

Thanks,

MustangTry this:
<script language="C#" runat="server">
void Page_Load(Object Src, EventArgs E) {
Rec_Date.Text = System.DateTime.Now.ToString();
}
</script>
<html>
<head>
<title>Textbox Test</title>
</head>
<body>
<form runat="server">
<asp:TextBox ID="Rec_Date" runat="server" Visible="false" />
</form>
</body>
</html>Dont use script to store a time that a record was added to a database.

It will not be giving an accurate representation of the true date-time that the record was added on the server.

Use the function from the database that stores the current time. (All Relational Databases have these)

What you want to do is add the call to this function into the SQL statement that is inserting the record. This way it will store that date at the exact same time as it stores the record.

Ill assume you are using SQLServer due to the fact that this is a .NET forum. So in this case the function is: GETDATE() . If you are using another database then you will have to look at the documentation for the equivalent of this command.

All the Best :)Thank you both for your help on this. I elected to go with Oak''s position on this due to the accuracy of the time posted and the ease of the scripting.

Here was the result of my changes:


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HSCTIConnectionString %>"
SelectCommand="Select * FROM HSCTI WHERE ItemID=(SELECT max(ItemID)FROM HSCTI)"
InsertCommand="INSERT INTO [HSCTI]([Rec_Date], [Cust_Name], [Client_Number], [Region], [Disp], [Contact_Date], [Sales_Rep], [Reason], [Sale_Affect], [Service_Affect], [Lisc_key], [Strobe_Rel], [iStrobe_Rel], [Feat_Affect], [CT_Number], [PT_Number], [Short_Desc]) VALUES (GetDate(), @Cust_Name, @Client_Number, @Region, @Disp, @Contact_Date, @Sales_Rep, @Reason, @Sale_Affect, @Service_Affect, @Lisc_key, @Strobe_Rel, @iStrobe_Rel, @Feat_Affect, @CT_Number, @PT_Number, @Short_Desc)"
OnInserted="SqlDataSource1_Inserted">


Thank you again!

MustangGood choice Mustang :)

You are really getting the hang of things now ;)Good choice Mustang :)
I'd have to agree on that. Databases arn't my strong point, but I'm learning! I'll have to keep that method in mind for any future database problems that I might work on.
 
Back
Top