Set default text on databound object

M

MasterChief

I have an insert template textbox that is bound to a field in SQL.
Since it is the insert template it really doesn't have anything in it.
How do I set the default text? Like for example if I have a insert
template field that is databound to a date sql column how can I set the
text to be a default like todays date?
 
N

Nathan Sokalski

Use the ItemDataBound event. This event fires after the databinding occurs
for each Item, so it occurs the same number of times as the number of Items.
 
M

MasterChief

I can't seem to find the ItemDataBound event. I can find it for the
gridview but not the formview which is where the insert template I am
using is. I am trying to use the databinding event is that the same
thing as ItemDataBound? Also how do I reference the textbox in the form
view? Like I can't see to go plan_startdateTextBox.text =
DateTime.Today.ToString()

Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)

End Sub
 
N

Nathan Sokalski

Here is an example from code that I wrote:


Private Sub datResults_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datResults.ItemDataBound

If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblRegion1"), Label).Text = "*NONE*" Then

CType(e.Item.FindControl("lblRegion1"), Label).Text = " "

End If

If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblRegion2"), Label).Text = "*NONE*" Then

CType(e.Item.FindControl("lblRegion2"), Label).Text = " "

End If

If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblPhone1"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone1"), Label).Text = " "

If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblPhone2"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone2"), Label).Text = " "

End Sub



Notice that at the end of the first line it refers to the event as
datResults.ItemDataBound (this is the standard objectID.eventname format).
As for your question about referencing a textbox (or any other Control used
in your Template), you must use
CType(e.Item.FindControl(controlID),ControlType) to reference the Control.
This is necessary because since there are many Items in a DataList (for
example, my example will include many controls with the ID lblPhone1), you
cannot simply use just the ID. All my sample code is in VB.NET, if you have
any questions let me know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top