I dont really know if this has been answered for you, but Ill post the down and dirty easy fixes for both of these.
First there is a fairly simple way to get a handle to anything in a repeater.
First Insert you label
---------------------------------
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:label id="myLabel" runat=server>
</ItemTemplate>
</asp:Repeater>
----------------------------------
Yes its empty! It will be bound to your data.
So in your code you will call the 'bind' and link it to a data-table. I dont have time to teach you how to pull from sql or create one manually, but there are plenty of tuts out there for doing that. In this case we will assume we pulled back a datatable, and it has records in it.
private sub bindRepeater()
dim DT as datatable 'Just so you know its a datatable
DT = getDataTableCode() ' Returns a datatable
repeater1.datasource = DT
repeater1.databind()
end sub
Now this is the sub that handles databinding for htis object, that has been called automatically when you did repeater1.databind()
You will then 'find your label' on each bound row
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
dim _label as label
_label = e.Item.FindControl("mylabel") 'This is what we called it on aspx page
_label.text = e.Item.DataItem("valueInDatabase") 'Current Data item in row being processed
End Sub
That effectively binds the data, on each item bound to the appropriate row.
Now, if you want to make a 'clickable' item instead of a label, use a 'linkbutton' or 'button' or whatever.
Once you have grabbed it with
dim _myButton as new linkbutton = e.Item.FindControl("myButton")
Were assuming that 'myButton exists as an id in your repeater, and its a button, if not it will fail, and you should code for such checks, but for simplicity we wont here.
You can assign to the props.
_myButton.commandArgument = e.item.dataitem("id")
_myButton.commandName = "delete"
Now, you can catch these events through the 'repeaters item command as stated above, or you can point them to a seperate function by doing
addhandler _myButton.click, addressof mySub()
Now each time a button in the repeater is clicked it will call that sub, ASSUMING you have created the sub. It must be created just like a REAL onClick event for a button, with the same arguements passed in. Remember that the SUB / SENDER does not know its a button, so you have to tell it to have the 'buttons' exposed calls, by SETTING it to a button.
Protected Sub mySub(ByVal sender As Object, ByVal e As System.EventArgs)
dim _myButton as linkbutton
_myButton = Sender
Now _mybutton will have the exposed items that a linkbutton has
response.write(_myButton.commandArguement)
response.write(_myButton.commandName)
End Sub
As a shorter simple solution you can bind the data, on databind, by putting in the aspx page.
<asp:label text="<%# databinder.eval(container.dataitem("DB_COL")) %>"></asp:label>
and assuming when you call databind, that DB_COL is the name of a column in the bound table, it will bind the text up for you on the fly.
The BIG benefit here is, you dont have to recompile your code, if you make the changes on the .aspx pag.
You can NOT copy and paste this code and expct it to work, as I didnt have any itellisense while doing it, and I havent checked it fully, but I wanted to give you the full breakdown on the 'dirty easy' way ppl do this.
I hope this helps you some.
Read it a couple times, it will probably give you other ideas on how to use similar controls.
|