Dynamic Links

  • Thread starter Thread starter Chad THomas
  • Start date Start date
C

Chad THomas

I have a data repeater controll on a page. in each row that is displayed, I
would like to have an action column that has differnet links shown depending
on certain criteria from the database. In traditional asp you could check
the value as you looped through the recordset, but in asp.net I am not sure
how to do it if you are binding to a control.

Thanks,
Chad
 
Hi,

you could use helper function or handle the ItemDataBound event of the
Repeater and manage the items from there (one by one)

See this article for info about helper functions (they are general concept
with databound controls like DataGrid,DataList and Repeater)

http://www.aspalliance.com/31
 
Hi Chad,

Teemu has a great answer. You can also use the repeater's ItemDataBound
event. This event fires for each row in the repeater (one by one).

Here's a small sample that demonstrates a little of what you can do in this
event.

*** Here is my repeater control
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<input id=one name=one type=text runat=server value=
'<%# Container.DataItem("au_id")%>'> &nbsp;
<br>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>


*** Here is my code
Dim Counter As Integer = 0

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Repeater1.DataSource = Qry1
Repeater1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
Repeater1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
If e.Item.DataItem("au_lname") = "Green" Then
Dim l As LinkButton = New LinkButton
l.Text = e.Item.DataItem("au_lname")
l.ForeColor = System.Drawing.Color.Green()
e.Item.Controls.Add(l)
End If
Counter += 1
End If
If e.Item.ItemType = ListItemType.Footer Then
Dim t As TextBox = New TextBox
t.Text = Counter.ToString & " People"
t.ForeColor = System.Drawing.Color.Green()
t.ReadOnly = True
e.Item.Controls.Add(t)
End If
End Sub

I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 
Back
Top