[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Repeater Button

Posted on 2009-04-28
13
Medium Priority
?
435 Views
Last Modified: 2012-05-06
I have a repeater that returns 100+ records
Inside the repeater is 8 labels holding the repeater data and a button.

What I want to do is OnClick, set each of those label.text values for that repeater record to individual session variables to be used throughout my appliaction pages.
0
Comment
Question by:lrbrister
  • 7
  • 6
13 Comments
 
LVL 10

Accepted Solution

by:
ALaRiva earned 2000 total points
ID: 24251669
Use the ItemCommand Event of the Repeater.

You'll have a button per row (place the button inside of the <ItemTemplate>)

Then, in the ItemCommand Event, you can use code like:

e.Item.FindControl("YourLabelName")

Where e is the repeater object, Item is the current row item (the one you clicked) and the FindControl function will return the Label Object.

HTH
- Anthony
0
 

Author Comment

by:lrbrister
ID: 24251814
ALaRiva,
  What would that look like on the button?

I have

blah...blah...
 <asp:Button ID="btnDetails" runat="server" Text="Details" >

           </ItemTemplate>
        </asp:Repeater>

In my code behind is this right?

    Protected Sub RepeaterMain_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RepeaterMain.ItemCommand
        Session("ResortID") = e.Item.FindControl("lblResortID")
        Session("ResortName") = e.Item.FindControl("lblResortName")
        Session("checkindate") = e.Item.FindControl("checkInDate")
    End Sub
0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 2000 total points
ID: 24251856
Close, except the FindControl method returns the Control Object.  so you'll need to declare a label variable (Dim llbl as Label) and then set the FindControl result to that variable, then use

Session("ResortID") = llbl.Text.ToString


So ...

Dim llbl As Label

llbl = e.Item.FindControl("lblResortID")

Session("ResortID") = llbl.Text.ToString


HTH
- Anthony
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:lrbrister
ID: 24251989
ALaRiva

Received this error...
Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.RepeaterCommandEventArgs'.

on this line
<asp:Button ID="btnBookNow" runat="server" Text="Book Now" OnClick="RepeaterMain_ItemCommand"   />

THis is the vb code behind
Protected Sub RepeaterMain_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RepeaterMain.ItemCommand
        Dim llbl As Label

        llbl = e.Item.FindControl("lblResortID")

        Session("ResortID") = llbl.Text.ToString
        Response.Redirect("z.aspx")

    End Sub
0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 2000 total points
ID: 24252096
You don't need the onClick Event for the button.  If it is in the <ItemTemplate> it will execute the ItemCommand Event regardless.
0
 

Author Comment

by:lrbrister
ID: 24252707
ALaRiva:

??  That buttin has ti be bound somehow right?  

Nothing is happening when I have
<asp:Button ID="btnBookNow" runat="server" Text="Book Now"  />  and nothing in the vb for the button.
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 24252749
You don't need to bind the button to anything.

The button should work as is.  Can you post your HTML Source as well as your VB Source?

My only thought is that maybe since you don't have a CommandName and/or CommandArguments attribute defined for the button, but I'm not sure.  I usually always have more than one button per row so I have those items defined.  Let me confirm that, but I'm 99.9% sure that you don't need to have them set in order for the ItemCommand() Event to run.

- Anthony
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 24252776
Hmmm, now that I think about it, I think you have to set the CommandName attribute of the button.

Try this for your button.
<asp:Button ID="btnBookNow" runat="server" Text="Book Now"  CommandName="BookNow" />

You won't need CommandArguments.

HTH
- Anthony
0
 

Author Comment

by:lrbrister
ID: 24252920
ALaRiva:

That didn't work either.  There's nothing that ties that button or command name to the ItemsCommand. When I run in debug it doesn't get to that code at all

BUTTON
<asp:Button ID="btnBookNow" runat="server" Text="Book Now" CommandName="BookNow" />


VB-CODE
 Protected Sub RepeaterMain_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RepeaterMain.ItemCommand
        If e.CommandName = "BookNow" Then
            Dim llbl As Label
            llbl = e.Item.FindControl("lblResortID")
            Session("ResortID") = llbl.Text.ToString
            Response.Redirect("z.aspx")
        End If
    End Sub
0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 2000 total points
ID: 24252964
Try that as a LinkButton

Instead of <asp:button ...

Use <asp:LinkButton ...

Still include the CommandName.  I don't use standard Button for Repeater Commands, and if memory serves me, there is an issue with it.

0
 

Author Comment

by:lrbrister
ID: 24253062
ALaRiva:
That seems to have taken care of it.  Let me do some testing and I'll get back to yo.

Was absolutely making me crazy...
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 24253113
Cool.  My apologies for the initial confusion.  That whole asp:button thing didn't dawn on me until the last posting.

- Anthony
0
 

Author Closing Comment

by:lrbrister
ID: 31575501
Way to hang in there with me.  Much appreciated
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

831 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