Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataBind In ItemDataBound Event

Reply
Thread Tools

DataBind In ItemDataBound Event

 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      09-24-2007
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....

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFuaXNo?=
Guest
Posts: n/a
 
      09-25-2007
Please refer to the MSDN link below:

http://msdn.microsoft.com/msdnmag/is...6/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

"" wrote:

> 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....
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeater: Items.count in ItemDataBound Event Josh Daws ASP .Net 2 09-11-2006 10:32 PM
GV Databound Event vs DG ItemDataBound Event GaryDean ASP .Net 1 08-18-2006 03:47 AM
ItemDataBound Event - How to access the previous record when this event is raised in DataGrid? NH ASP .Net Datagrid Control 2 12-17-2004 03:58 PM
BUG in DataBind? After .DataBind there are more DataGrid Items than DataSet Rows! Michael ASP .Net Datagrid Control 0 12-29-2003 07:47 PM
ref dropdownlist in itemdatabound event? Tina ASP .Net 1 10-14-2003 06:33 PM



Advertisments