DataBind In ItemDataBound Event

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Sep 24, 2007.

  1. Guest

    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....
     
    , Sep 24, 2007
    #1
    1. Advertising

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

    "" 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....
    >
    >
     
    =?Utf-8?B?TWFuaXNo?=, Sep 25, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tina
    Replies:
    1
    Views:
    381
    Suresh
    Oct 14, 2003
  2. Josh Daws
    Replies:
    2
    Views:
    22,228
    Claes
    Sep 11, 2006
  3. GaryDean
    Replies:
    1
    Views:
    8,740
    Walter Wang [MSFT]
    Aug 18, 2006
  4. Michael
    Replies:
    0
    Views:
    238
    Michael
    Dec 29, 2003
  5. NH
    Replies:
    2
    Views:
    555
    Andrew L. Van Slaars
    Dec 17, 2004
Loading...

Share This Page