DataBind In ItemDataBound Event

R

rn5a

Consider the following code:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(".........")
sqlDapter = New SqlDataAdapter("SELECT * FROM NETUsers",
sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

rptrUsers.DataSource = dSet
rptrUsers.DataMember = "Users"
rptrUsers.DataBind()

sqlConn.Close()
End If
End Sub

Sub ItemList(ByVal obj As Object, ByVal ea As
RepeaterCommandEventArgs)
lblCmdSource.Text = "CommandName: " & ea.CommandName
lblCmdSource.Text += "<br>CommandSource: " &
CType(ea.CommandSource, LinkButton).Text"
lblCmdSource.Text += "<br>CommandArgument: " &
ea.CommandArgument
End Sub

Sub BindData(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Item Data Bound<br>")
'rptrUsers.DataBind()
End Sub

Sub ItemCreated(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Item Created<br>")
'rptrUsers.DataBind()
End Sub
</script>

<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemCommand="ItemList"
OnItemCreated="ItemCreated" OnItemDataBound="BindData" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>NAME</th>
<th>PHONE NO.</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th>
<asp:LinkButton ID="lnkName" CommandArgument='<%#
Container.DataItem("LastName") %>' CommandName='<%#
Container.DataItem("FirstName") %>' Text='<%#
Container.DataItem("FirstName") & " " & Container.DataItem("LastName")
%>' runat="server"></asp:LinkButton>
</th>
<th><%# Container.DataItem("Phone") %></th>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label ID="lblCmdSource" runat="server"/>
</form>

As such the above code works fine. Please note the 2 commented lines
(rptrUsers.DataBind()) in the subs BindData & ItemCreated. If I
uncomment either or both of these lines, then the above code doesn't
get executed. Instead it just displays a message saying "Server
Application Unavailable".

Now why is the presence of the line - rptrUsers.DataBind() - in the 2
subs BindItem & ItemCreated not allowing the ASPX code to get
executed?

Thanks....
 
G

Guest

Please refer to the MSDN link below:

http://msdn.microsoft.com/msdnmag/issues/05/06/CuttingEdge/

It says that When a RepeatItem is created in the Repeater control, the
ItemCreated event fires. The event simply signals the creation of the
element; it says nothing about the data associated with the element. Data
binding the item, in fact, happens through the ItemDataBound event.
ItemCreated and ItemDataBound are extremely useful for modifying the control
style dynamically to, say, alert users that values are at a critical
threshold.

Infact, GridView control also behaves the same way.

Regards,
Manish
 

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

Similar Threads

DataBind 1
IsPostBack 4
ViewState? 1
OnItemCommand & OnItemDataBound 2
Repeater OnItemCommand 1
Display DB Records in DataList? 1
DropDownList DataGrid 1
DataGrid Edit Problem 9

Top