OnItemCommand & OnItemDataBound

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RN1

    OnItemCommand & OnItemDataBound

    Consider the following code:

    ------------------------------
    <script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    If Not (Page.IsPostBac k) Then
    'binding data from DB to the Repeater
    End If
    End Sub

    Sub ItemCmd(ByVal obj As Object, ByVal ea As
    RepeaterCommand EventArgs)
    'some code
    End Sub

    Sub BindData(ByVal obj As Object, ByVal ea As
    RepeaterItemEve ntArgs)
    If (ea.Item.ItemTy pe = ListItemType.Al ternatingItem Or
    ea.Item.ItemTyp e = ListItemType.It em) Then
    Dim lnkDel As LinkButton
    lnkDel = CType(ea.Item.F indControl("lnk Delete"),
    LinkButton)
    lnkDel.Attribut es.Add("OnClick ", "if(!confirm('D elete this
    record?')) return false;")
    End If
    End Sub
    </script>

    <form runat="server">
    <asp:Repeater ID="rptrCols" OnItemCommand=" ItemCmd"
    OnItemDataBound ="BindData" runat="server">
    <HeaderTemplate >
    <table>
    <tr>
    <th>Col1</th>
    <th>Col2</th>
    <th>Col3</th>
    <th>Delete</th>
    </tr>
    </HeaderTemplate>

    <ItemTemplate >
    <tr>
    <td>
    <asp:Label ID="lblCol1" Text='<%# Container.DataI tem("Col1") %>'
    runat="server"/>
    </td>
    <td>
    <asp:LinkButt on ID="lnkCol2" CommandName='<% #
    Container.DataI tem("Col2") %>' Text='<%# Container.DataI tem("Col2")
    %>' runat="server"/>
    </td>
    <td>
    <asp:Label ID="lblCol3" Text='<%# Container.DataI tem("Col3") %>'
    runat="server"/>
    </td>
    <td>
    <asp:LinkButt on ID="lnkDelete" Text="DELETE" runat="server"/>
    </td>
    </tr>
    </ItemTemplate>

    <FooterTemplate >
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </form>
    ------------------------------

    When the DELETE link is clicked, a JavaScript confirm dialog pops-up.
    This is what the above code does. But if I move the entire (JavaScript
    confirm dialog) code from the OnItemDataBound event function to the
    OnItemCommand event function, the JavaScript confirm dialog doesn't
    pop-up when the DeLETE link is clicked. Why?

    Thanks,

    Ron
  • bruce barker

    #2
    Re: OnItemCommand &amp; OnItemDataBound

    you are rendering a javascript onclick hander for a button. if you do it
    in databind the javascript will exist when the user clicks the button.
    if you add in the oncommand the javascript is added only after the
    postback, so there is no way for it run, unless after the render of the
    postback the user clicks the button again (but I assume you don't
    rerender deleted items).

    -- bruce (sqlwork.com)


    RN1 wrote:
    Consider the following code:
    >
    ------------------------------
    <script runat="server">
    Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
    If Not (Page.IsPostBac k) Then
    'binding data from DB to the Repeater
    End If
    End Sub
    >
    Sub ItemCmd(ByVal obj As Object, ByVal ea As
    RepeaterCommand EventArgs)
    'some code
    End Sub
    >
    Sub BindData(ByVal obj As Object, ByVal ea As
    RepeaterItemEve ntArgs)
    If (ea.Item.ItemTy pe = ListItemType.Al ternatingItem Or
    ea.Item.ItemTyp e = ListItemType.It em) Then
    Dim lnkDel As LinkButton
    lnkDel = CType(ea.Item.F indControl("lnk Delete"),
    LinkButton)
    lnkDel.Attribut es.Add("OnClick ", "if(!confirm('D elete this
    record?')) return false;")
    End If
    End Sub
    </script>
    >
    <form runat="server">
    <asp:Repeater ID="rptrCols" OnItemCommand=" ItemCmd"
    OnItemDataBound ="BindData" runat="server">
    <HeaderTemplate >
    <table>
    <tr>
    <th>Col1</th>
    <th>Col2</th>
    <th>Col3</th>
    <th>Delete</th>
    </tr>
    </HeaderTemplate>
    >
    <ItemTemplate >
    <tr>
    <td>
    <asp:Label ID="lblCol1" Text='<%# Container.DataI tem("Col1") %>'
    runat="server"/>
    </td>
    <td>
    <asp:LinkButt on ID="lnkCol2" CommandName='<% #
    Container.DataI tem("Col2") %>' Text='<%# Container.DataI tem("Col2")
    %>' runat="server"/>
    </td>
    <td>
    <asp:Label ID="lblCol3" Text='<%# Container.DataI tem("Col3") %>'
    runat="server"/>
    </td>
    <td>
    <asp:LinkButt on ID="lnkDelete" Text="DELETE" runat="server"/>
    </td>
    </tr>
    </ItemTemplate>
    >
    <FooterTemplate >
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </form>
    ------------------------------
    >
    When the DELETE link is clicked, a JavaScript confirm dialog pops-up.
    This is what the above code does. But if I move the entire (JavaScript
    confirm dialog) code from the OnItemDataBound event function to the
    OnItemCommand event function, the JavaScript confirm dialog doesn't
    pop-up when the DeLETE link is clicked. Why?
    >
    Thanks,
    >
    Ron

    Comment

    • Cowboy \(Gregory A. Beamer\)

      #3
      Re: OnItemCommand &amp; OnItemDataBound

      The DataBind event is adding your snippet to every line as it is painted on
      the page. You can confirm this by examining the source. What this means is
      every row you bind runs your code to produce the rendered HTML output.

      ItemCommand does not paint as you bind. It sets up a delegate back to the
      event you specify, but it is resolved by firing back to the server. With
      DataBind, which is fired during binding (for each row), you can add any type
      of server side code you desire and have it rendered in the page. It does not
      fire after the page is rendered; your code that you embedded is fired.

      Hope this makes sense.

      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA

      Subscribe to my blog


      or just read it:


      *************** *************** **************
      | Think outside the box! |
      *************** *************** **************
      "RN1" <rn5a@rediffmai l.comwrote in message
      news:2150302c-83ff-4621-8afc-bbdfd3d3bef0@x1 6g2000prn.googl egroups.com...
      Consider the following code:
      >
      ------------------------------
      <script runat="server">
      Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
      If Not (Page.IsPostBac k) Then
      'binding data from DB to the Repeater
      End If
      End Sub
      >
      Sub ItemCmd(ByVal obj As Object, ByVal ea As
      RepeaterCommand EventArgs)
      'some code
      End Sub
      >
      Sub BindData(ByVal obj As Object, ByVal ea As
      RepeaterItemEve ntArgs)
      If (ea.Item.ItemTy pe = ListItemType.Al ternatingItem Or
      ea.Item.ItemTyp e = ListItemType.It em) Then
      Dim lnkDel As LinkButton
      lnkDel = CType(ea.Item.F indControl("lnk Delete"),
      LinkButton)
      lnkDel.Attribut es.Add("OnClick ", "if(!confirm('D elete this
      record?')) return false;")
      End If
      End Sub
      </script>
      >
      <form runat="server">
      <asp:Repeater ID="rptrCols" OnItemCommand=" ItemCmd"
      OnItemDataBound ="BindData" runat="server">
      <HeaderTemplate >
      <table>
      <tr>
      <th>Col1</th>
      <th>Col2</th>
      <th>Col3</th>
      <th>Delete</th>
      </tr>
      </HeaderTemplate>
      >
      <ItemTemplate >
      <tr>
      <td>
      <asp:Label ID="lblCol1" Text='<%# Container.DataI tem("Col1") %>'
      runat="server"/>
      </td>
      <td>
      <asp:LinkButt on ID="lnkCol2" CommandName='<% #
      Container.DataI tem("Col2") %>' Text='<%# Container.DataI tem("Col2")
      %>' runat="server"/>
      </td>
      <td>
      <asp:Label ID="lblCol3" Text='<%# Container.DataI tem("Col3") %>'
      runat="server"/>
      </td>
      <td>
      <asp:LinkButt on ID="lnkDelete" Text="DELETE" runat="server"/>
      </td>
      </tr>
      </ItemTemplate>
      >
      <FooterTemplate >
      </table>
      </FooterTemplate>
      </asp:Repeater>
      </form>
      ------------------------------
      >
      When the DELETE link is clicked, a JavaScript confirm dialog pops-up.
      This is what the above code does. But if I move the entire (JavaScript
      confirm dialog) code from the OnItemDataBound event function to the
      OnItemCommand event function, the JavaScript confirm dialog doesn't
      pop-up when the DeLETE link is clicked. Why?
      >
      Thanks,
      >
      Ron

      Comment

      Working...