We help IT Professionals succeed at work.

ASP.NET Nested Repeater Button Event

CyberUnDead
CyberUnDead asked
on
515 Views
Last Modified: 2013-11-26
Using information garnered from the Exchange in particular:

https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_20697348.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_21185725.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?
Comment
Watch Question

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. ;)

Author

Commented:
-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?

Author

Commented:
-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>
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
-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

Author

Commented:
Thanks for you patience -Thespian-:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
-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

Author

Commented:
Opps the line that  reads

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

should be:

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

Author

Commented:
I am also increasing the point value to 500.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.