Making Use of the Repeater Web Server Control - Ooooh, Bubbles!
(Page 4 of 5 )
Suppose you've decided that along with your nicely styled repetition of your inventory, it would be somewhat beneficial to your business for people to actually be able to purchase one or more of the items. A nice way to do that is stick a little button unambiguously labeled 'buy'. When you click 'buy', you want a subroutine that takes the ID of the item you want to buy, and takes you to the next step of credit card processing and such.
I won't bother with the next step here, but I will show you how to embed a button control in the Repeater, and how to configure the button (child) to make the Repeater (parent) aware of its events. This is called bubbling. Think of bubbles starting at the bottom of a boiling pot, and rising to the surface to pop. That's essentially what's happening; the events/bubbles start at the bottom - the child - and rise to the top – the parent – to be handled.
To do this, first we add the code to the container, the Repeater, to capture the events. Our repeater will now look like this:
<asp:Repeater id="prodRepeater" runat="server" onItemCommand="getItem">
Then, we add the child control, the button to the item template, and the alternating item template if you are using one. It would look like this:
<asp:button runat="server" text="buy" CommandArgument='<%# Container.DataItem("id") %>' />
We hide the id of the item in the CommandArgument member of the button, which we will pick up later. Once a user clicks 'buy', this raises an event, which is bubbled up to the repeater, which will hand it off to the following getItem() subroutine:
Sub getItem( s as object, e as RepeaterCommandEventArgs )
'response.write( "id: " + e.CommandSource.CommandArgument )
Dim id as String = e.CommandSource.CommandArgument.ToString()
Response.redirect( "buy.aspx?intID=" + id )
End Sub
As you can see, this subroutine picks up the value from the button control though the CommandArgument member. We could do lots of things with this now, but for simplicity's sake I have just chosen to hand it off to another page ('buy.aspx') with the ID of the desired product.
Next: Conclusion >>
More ASP.NET Articles
More By Justin Cook