Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ASP.NET Nested Repeater Button Event

Posted on 2007-10-05
12
Medium Priority
?
416 Views
Last Modified: 2013-11-26
Using information garnered from the Exchange in particular:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_20697348.html
http://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?
0
Comment
Question by:CyberUnDead
  • 7
  • 5
12 Comments
 
LVL 5

Expert Comment

by:-Thespian-
ID: 20022187
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. ;)
0
 

Author Comment

by:CyberUnDead
ID: 20022448
-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).
0
 
LVL 5

Expert Comment

by:-Thespian-
ID: 20022515
hm... can u provide the interface code of the repeater?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CyberUnDead
ID: 20022577
-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>
0
 
LVL 5

Accepted Solution

by:
-Thespian- earned 2000 total points
ID: 20022735
try this:

<asp:Button ID="btnReg_<%#Container.DataItem("Activity_ID")%>" runat="Server" Text="Register" OnClick="btnReg_Click" Visible="false" />
0
 

Author Comment

by:CyberUnDead
ID: 20023003
-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
0
 

Author Comment

by:CyberUnDead
ID: 20023012
Thanks for you patience -Thespian-:
0
 
LVL 5

Assisted Solution

by:-Thespian-
-Thespian- earned 2000 total points
ID: 20023070
You don't need to pick it from there in such way.
yours OnClick will be:

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

And to find the button:
Dim btnReg As Button = rptChild.Items(0).FindControl("btnReg_" & CType(rptChild.Items(0).FindControl("litActivityID"), Literal).Text)
0
 

Author Comment

by:CyberUnDead
ID: 20023960
-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
0
 

Author Comment

by:CyberUnDead
ID: 20023981
Opps the line that  reads

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

should be:

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

0
 

Author Comment

by:CyberUnDead
ID: 20023989
I am also increasing the point value to 500.
0
 
LVL 5

Assisted Solution

by:-Thespian-
-Thespian- earned 2000 total points
ID: 20024051
In debugger try to iterate all controls in rptChild.Items(0) maybe the "btnReg_..." is not exist there. So, then try to find it in a parent.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question