John M
John M

Reputation: 14668

ASP .NET - Set default values of ListView InsertItemTemplate

For a DetailsView I use the following code:

 protected void DetailsView1_PreRender(object sender, EventArgs e)
{
     DetailsView myDetailsView = (DetailsView)sender;
    //set value to current datetime
    ((TextBox)myDetailsView.FindControl("TextBox1")).Text =
    DateTime.Now.ToString("M/d/yyyy HH:mm");
}

When I try to do the same thing for a ListView control (specifically the InsertItemTemplate) I get a 'NullReferenceException' error message.

How do I set the default value of a Listview InsertItemTemplate textbox?

Upvotes: 2

Views: 6956

Answers (1)

Ahmad Mageed
Ahmad Mageed

Reputation: 96497

You could try using the ListView's InsertItem property:

((TextBox)myDetailsView.InsertItem.FindControl("TextBox1")).Text =
    DateTime.Now.ToString("M/d/yyyy HH:mm");

Upvotes: 4

Related Questions