Wednesday, March 21, 2012

Default current date in Calendar

When using the Calendar extender, is there a way to put a default current date in the textbox?

Auto populate the text field of the textbox with <%# DateTime.Now.ToShortDateString() %> or make the assignment in code-behind.


I realized that I can assign in code behind but I don't want to do that, that's why I asked because I thought the calendar extender should allow me to do that easily. I can't do this, <%# DateTime.Now.ToShortDateString() %>, since this textbox is bind to a database.


I don't believe the calendar extender can do it automatically, but you could through JavaScript; though, you could also check the textbox and if nothing has been assigned after binding, then make the assignment.


bmains:

Auto populate the text field of the textbox with <%# DateTime.Now.ToShortDateString() %> or make the assignment in code-behind.

Okay, I thought I can try something like this:

<asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("dateCreate", "{0:d}", DateTime.Now.ToShortDateString())%>' Width="80px"></asp:TextBox><br />   

But when compile it gave me this error:

Error 1 A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind. E:\Website\Outlook\admin\admOLissue.aspx 110

Any othe ideas?


Hello,

I'm not sure, but I don't think Bind supports the last parameter. If that is the case, you could create a public method that takes an object as such:

public function FormatDate(objDate As Object) As String
if (objDate is Nothing orelse objDate.Equals(DbNull.Value)) then
return DateTime.Now.ToShortDateString()
else
return CType(objDate, DateTime).ToShortDateString()
end if
end function

And in the textbox:

<asp:TextBox Text='<%# FormatDate(Bind("dateCreate"))%>'


Okay, I got the following error when compiled:

Error 2 The name 'Bind' does not exist in the current context E:\Website\admOLissue.aspx 110

This error comes from this line:

<asp:TextBox Text='<%# FormatDate(Bind("dateCreate"))%>'

By the way, I'm using C#.


Try using Eval instead.


If I use Eval instead of Bind, will it bind to the database if I click on the Update button?


Updates are not based on Bind or Eval statements, but on the value in textbox, not the value passed through Bind or Eval statements... If Bind doesn't work anyway, try to see if Eval will work in that context...


Okay, that seems to fix it.Thanks so much!


Hi there,

I have tried the statement with Eval and didn't work for me. When I click on edit and the field shows the correct date. But once i click on update it doesn't show anything. Is there any other possible way of doing this?

Thanks


I'd be able to help better if you posted the relevant code. Post the control you are doing this with, as well as the parent control that it is in, and how you are updating.


I think the current date ends up in the bottom of the calendar control and you can click on it to take today's date. I know this isn't great, but with a little bit of user training, it can work. I'd prefer to be able to specify today's date to be the default date as well, but since it's not available, this is the best option I could find and not have to rig it up programmatically.


bmains:

Try using Eval instead.

Not sure why, but it's not working. The value will not get updated in the database. Code in .aspx page:

<asp:TextBox ID="txtModDate" runat="server" Text='<%# defaultDate(Eval("modDate"))%>'></asp:TextBox>

Code behind:

1public string defaultDate(Object crntDate)2 {3string currentDate = Convert.ToDateTime(crntDate).ToShortDateString();4if (!String.IsNullOrEmpty(currentDate))5 {6return DateTime.Now.ToShortDateString();7 }8else9 {10return currentDate;11 }

Here is the popup error:

Cannot insert the value NULL into the column 'modDate', table "myDB.dbo.myTable'; column does not allow nulls. INSERT fails. The statement has been terminated.

No comments:

Post a Comment