Link to home
Create AccountLog in
Avatar of CyberUnDead
CyberUnDead

asked on

ASP.NET Nested Repeater Button Event

Using information garnered from the Exchange in particular:

https://www.experts-exchange.com/questions/20697348/ASP-NET-Bound-Repeater-in-Conditional-Table-Formatting.html
https://www.experts-exchange.com/questions/21185725/Repeater-Command-Not-being-executed.html

I have a nested repeater "Child1Repeater" inside of "ParentRepeater" that has "btnReg" as a button.  Also the ParentRepeater has a literal of "litActivityID" which contains an integer value say "122"

Whenever the user clicks on btnReg the following  method executes:

Protected Sub btnReg_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        HttpContext.Current.Response.Redirect("RegisterActivity.aspx?activityid=")
End Sub

My question is what is the best way pass the litActivityID.text to my a different webpage (RegisterActivity.aspx).  I was thinking through the URI but how do I add the litActivityID.text to pass?
Avatar of -Thespian-
-Thespian-
Flag of Ukraine image

Give the ID of each Button control like "btn_122", "btn_124", "btn_X".
Than in OnClick procedure you will get an ID:
  CType(sender, Button).ID.Substring(4)

And pass it as parameter. ;)
Avatar of CyberUnDead
CyberUnDead

ASKER

-Thespian-:

  How do I assign an ID in the markup?  My current button under the Child1Repeater looks like:

<asp:Button ID="btnReg" runat="Server" Text="Register" OnClick="btnReg_Click" Visible="false" />

The ID is in the ParentRepeater and it looks like:
<asp:Literal ID="litActivityID" runat="Server" Text='<%#Container.DataItem("Activity_ID")%>' Visible="true" />

Once assigned how will I find the correct button in my ParentRepeater_ItemDataBound subroutine which has the code

Dim btnReg As Button = rptChild.Items(0).FindControl("btnReg")

I am using ItemDataBound because depending upon the field value the button will be visible or not.  (i.e. You can't register for an activity you already part of).
hm... can u provide the interface code of the repeater?
-Thespian-:

Here is an abbreviated version but includes all the necessary parts for my question.

<asp:Repeater id="ParentRepeater" runat="server">
        <HeaderTemplate>
            <table style="width: 100%;">
        </HeaderTemplate>                
        <SeparatorTemplate>
        <tr>
            <td colspan="6">&nbsp;</td>
        </tr>        
        </SeparatorTemplate>
        <ItemTemplate>
             <tr>
                <td style="border-bottom: 2px solid black;" colspan="6">
                    <asp:Literal ID="litDate" runat="Server" Text='<%#CheckDate(Container.DataItem("date"))%>' />
                </td>
                <ul>                
             </tr>
                <asp:Repeater id="Child1Repeater" runat="server" datasource='<%# Container.DataItem.Row.GetChildRows("date") %>'>
                    <HeaderTemplate>
                    </HeaderTemplate>                
                    <ItemTemplate>
                            <tr>
                                <td style="width: 20px">
                                    <li/><asp:Literal ID="litActivityID" runat="Server" Text='<%#Container.DataItem("Activity_ID")%>' Visible="true" />
                                </td>
                                <td style="width: 200px;">
                                    <i><asp:Literal ID="litActivity" runat="Server" Text='<%#Container.DataItem("Activity_Title")%>' /></i>
                                </td>
                                <td style="width: 60px; text-align: center;">  
                                     <asp:Literal ID="litStatus" runat="Server" Text='<%#CheckStatus(Container.DataItem("Status"))%>' />
                                    <asp:Button ID="btnReg" runat="Server" Text="Register" OnClick="btnReg_Click" Visible="false" />
                                </td>
                    </ItemTemplate>
                    <SeparatorTemplate>
                    </SeparatorTemplate>
                    <FooterTemplate>
                                </ul>
                            </tr>
                    </FooterTemplate>
                </asp:Repeater>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>        
    </asp:Repeater>
ASKER CERTIFIED SOLUTION
Avatar of -Thespian-
-Thespian-
Flag of Ukraine image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
-Thespian-:

                That adds the activityid to the button's id.  However how can pick the correct button out from

ParentRepeater_ItemDataBound subroutine

                'Dim btnReg As Button = rptChild.Items(0).FindControl("btnReg")
                'btnReg.Visible = True
Thanks for you patience -Thespian-:
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
-Thespian-:

I have substituted your code in all the necessary places.  After the code to find my new button with the appropriate activityid I check a condition.

If the "Status" equals "Available" I want to change the button visibility to true from the default false.
So I use the code

btnReg.visible = true

However I receive an error doing this.

   Object reference not set to an instance of an object.
   System.NullReferenceException: Object reference not set to an instance of an object.

I have broken up your code and to display the output and I know I am good right up to findcontrol.  This is my ParentRepeater_ItemDataBound method:

    Protected Sub ParentRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemActivityArgs) Handles ParentRepeater.ItemDataBound
        If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem)) Then

            Dim rptChild As Repeater = CType(e.Item.FindControl("Child1Repeater"), Repeater)
            Dim litStatus As Literal = CType(rptChild.Items(0).FindControl("litStatus"), Literal)
            Dim strActivityID As Literal = CType(rptChild.Items(0).FindControl("litActivityID"), Literal)

            If (litStatus.Text = "Available") Then
                Dim strButton As String = "btnReg_" & strActivityID.Text
                Dim btnReg As Button = btnReg = rptChild.Items(0).FindControl(strButton)
                MsgBox(strButton)
                btnReg.Visible = True
                litStatus.Visible = False
            End If
        End If
    End Sub
Opps the line that  reads

Dim btnReg As Button = btnReg = rptChild.Items(0).FindControl(strButton)

should be:

Dim btnReg = rptChild.Items(0).FindControl(strButton)

I am also increasing the point value to 500.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.