how to handle control’s event inside of nested repeater

In this article I will show you how can you handle control’s event that is inside of nested repeater control and pass informative message from user control to main page(f.e success/failure message).

I had a user control that submit the data to database and this control was inside both parent and child repeater.  Here is the aspx and user control code markup.

.ASPX page
<asp:Repeater runat="server" ID="rpt">
  <ItemTemplate>
     <uc:control runat="server" id="uc"></uc:control>

       <asp:Repeater runat="server" ID="rptChild">
         <ItemTemplate>
           <uc:control runat="server" id="ucChild"></uc:control>
         </ItemTemplate>
        </asp:Repeater>

   </ItemTemplate>
</asp:Repeater>

.ASCX (User Control)

<%@ Control Language="vb" AutoEventWireup="false" CodeFile="UserControl.ascx.vb" Inherits="UserControl" %>
I m user control <br />
<asp:TextBox runat="server" ID="txtName"></asp:TextBox><br />
<asp:Button runat="server" ID="btnSubmit" />
I need to know on .aspx page whether usercontrol submitted data successfully or not.  So I was thinking to declare an event in user control that can be raised on successful submission and that I can subscribe in .aspx page. To do that I need to find user control in repeater’s itemdatabound event and subscribe event to method in .aspx page.  But this wasn’t working.  User control was raising the event but wasn’t firing the method that subscribe to that event.  I have googled about that found that controls nested inside of repeaters do not intercept events.  So what’s alternative?

The way bubble event gets triggered is, first button click event of user control(thats inside repeater) gets triggered and then it triggers repeater’s itemcommand event.  So you need some kind of message (success/failure) passed over from button click event to repeater’s itemcommand event.  To do this, there are two importan fields of button control CommandName and CommandArgument can be used. Repeater’s ItemCommand event object also contains these two property to identify which object triggered the postback.  CommandArgument can be used to store status whether the submission is successful or not inside button’s click event.  Since repeater’s ItemCommand event gets triggered after button’s click event and has information about which object triggered the postback, you can retrieve CommandArgument value you set in usercontrol and easily figure it out whether the submission is successful or not.

Here is the complete code.
.ASCX (User Control)
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        'save data
        'set 'success' flag true/false based on successful data save
        Dim success As Boolean = True

        If (success) Then
            CType(sender, Button).CommandArgument = "1"
        Else
            CType(sender, Button).CommandArgument = "0"
        End If

 End Sub
.ASPX page
Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
        If (e.CommandName.ToUpper().Equals("SubmitData")) Then
            Dim Issuccess As String = e.CommandArgument.ToString()
            If (Issuccess.Equals("1")) Then
                'your logic on successful data submit
                'rebind repeater if required
            End If
        End If
End Sub

Hope this helps!

Posted in Microsoft Technology Tagged with: ,

Ads