Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

DataBind In ItemDataBound Event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rn5a@rediffmail.com - 24 Sep 2007 07:37 GMT
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....
Manish - 25 Sep 2007 11:42 GMT
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

> Consider the following code:
>
[quoted text clipped - 82 lines]
>
> Thanks....

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.