OnItemCommand & OnItemDataBound


R

RN1

Consider the following code:

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

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

Sub BindData(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
If (ea.Item.ItemType = ListItemType.AlternatingItem Or
ea.Item.ItemType = ListItemType.Item) Then
Dim lnkDel As LinkButton
lnkDel = CType(ea.Item.FindControl("lnkDelete"),
LinkButton)
lnkDel.Attributes.Add("OnClick", "if(!confirm('Delete 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.DataItem("Col1") %>'
runat="server"/>
</td>
<td>
<asp:LinkButton ID="lnkCol2" CommandName='<%#
Container.DataItem("Col2") %>' Text='<%# Container.DataItem("Col2")
%>' runat="server"/>
</td>
<td>
<asp:Label ID="lblCol3" Text='<%# Container.DataItem("Col3") %>'
runat="server"/>
</td>
<td>
<asp:LinkButton 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
 
Ad

Advertisements

B

bruce barker

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)
 
Ad

Advertisements

C

Cowboy \(Gregory A. Beamer\)

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
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 

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


Top