Binding Current date into listview textbox

Last post 07-04-2009 4:49 PM by RatheeshC. 2 replies.

Sort Posts:

  • Binding Current date into listview textbox

    07-04-2009, 9:42 AM
    • Member
      5 point Member
    • Yaniv.net
    • Member since 07-04-2009, 1:31 PM
    • Posts 59

     Hi,
    i am trying to bind the current date into one of the textbox in my insertitem template in listview,
    this line does not work:

    <asp:TextBox ID="JobDateTextBox" runat="server" Text="<%# System.DateTime.Now.ToString() %>"/>

    is there any way to do that ? even from the code behind, how can i access this control from the  page load event ?

    Thanks

  • Re: Binding Current date into listview textbox

    07-04-2009, 3:17 PM
    Answer
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 2:05 PM
    • Italy
    • Posts 1,103

    Your observation is correct. The inserttemplate doesn't get databound and as such databinder exprssions <%# are not evaluated. This is normal as it is intended for you collect input from the user and not bind it to data (afterall this is supposedly an insert operation).

    What you can do is subscribe to the ItemCreated handler of your list view and add your default datetime in there eg:

    protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.InsertItem)
            {
                TextBox jobDateTextBox = (TextBox)e.Item.FindControl("JobDateTextBox");
                if (jobDateTextBox != null)
                    jobDateTextBox.Text = DateTime.Now.ToString();
            }
        }

     <asp:ListView ID="ListView1" OnItemCreated="ListView1_ItemCreated" runat="server" ....

    note the OnItemCreated handler wired declaratively to the listview and the method it's pointing to :-)

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: Binding Current date into listview textbox

    07-04-2009, 4:49 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 2:05 PM
    • Posts 1,198

    Hello,

    You could use the e.Item.FindControl() method to find those items which are in Data controls like Grid, List View,

    DataLists , repeater etc

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
Page 1 of 1 (3 items)