|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Programatically Databinding controls within a RepeaterIf I have a Repeater control which contains one ItemTemplate, which in turn
contains one HyperLink control, is there anyway I can set that Hyperlink controls .Text and .NavigateUrl datasource values in the codebehind as opposed to using NavigateUrl='<%# Eval("URL") %>' style syntax in the .aspx? Hope that makes sense. Write a method to handle the ItemDataBound event of the Repeater control in
which you get a reference to the HyperLink using the FindControl method and then you can change any of the properties of the hyperlink based on the DataItem. For example in VB you would wite: Protected Sub repeater1_ItemDataBound(ByVal sender As System.Object, ByVal e As RepeaterItemEventArgs) Handles repeater1.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 'this line gets you the underlying data row view (if you used a datatable as 'the datasource for the repeater). Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) 'assuming that you gave the hyperlink an id="HyperLink1" Dim hlHyperlink1 As HyperLink = CType(e.Item.FindControl("HyperLink1"), HyperLink) 'assuming you have a field named Text in your dataTable hlHyperlink1.Text = drv("Text") 'assuming you have a field named Url in your datatable hlHyperlink1.NavigateUrl = drv("Url") End If End Sub Show quoteHide quote "dcew" wrote: > If I have a Repeater control which contains one ItemTemplate, which in turn > contains one HyperLink control, is there anyway I can set that Hyperlink > controls .Text and .NavigateUrl datasource values in the codebehind as > opposed to using NavigateUrl='<%# Eval("URL") %>' style syntax in the .aspx? > > Hope that makes sense. That's exactly what I was looking for!
Here's the c# code if anyone's interested (using a custom MyHyperLink object as the datasource): protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { MyHyperLink mhl = (MyHyperLink)e.Item.DataItem; HyperLink hyperLink = (HyperLink)e.Item.FindControl("navHyperLink"); hyperLink.Text = mhl.DisplayLink; hyperLink.NavigateUrl = mhl.NavLink; } } Thanks, dcew. Show quoteHide quote "Phillip Williams" wrote: > Write a method to handle the ItemDataBound event of the Repeater control in > which you get a reference to the HyperLink using the FindControl method and > then you can change any of the properties of the hyperlink based on the > DataItem. For example in VB you would wite: > > Protected Sub repeater1_ItemDataBound(ByVal sender As System.Object, ByVal e > As RepeaterItemEventArgs) Handles repeater1.ItemDataBound > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = > ListItemType.AlternatingItem Then > 'this line gets you the underlying data row view (if you used a > datatable as > 'the datasource for the repeater). > Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) > 'assuming that you gave the hyperlink an id="HyperLink1" > Dim hlHyperlink1 As HyperLink = > CType(e.Item.FindControl("HyperLink1"), HyperLink) > 'assuming you have a field named Text in your dataTable > hlHyperlink1.Text = drv("Text") > 'assuming you have a field named Url in your datatable > hlHyperlink1.NavigateUrl = drv("Url") > End If > End Sub > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "dcew" wrote: > > > If I have a Repeater control which contains one ItemTemplate, which in turn > > contains one HyperLink control, is there anyway I can set that Hyperlink > > controls .Text and .NavigateUrl datasource values in the codebehind as > > opposed to using NavigateUrl='<%# Eval("URL") %>' style syntax in the .aspx? > > > > Hope that makes sense.
Classic ASP String Manipulation - NOT .net
CustomValidator is not firing add a list item to a bound control Best way to have multiple pages show up as same selection on menu Simple String Conversion? DataGrid Conditional Formatting Dynamic checkbox loses checked state cancel toolbar buttonclick from client-side javascript MaxLenth doesn't work My WebControls stopped working |
|||||||||||||||||||||||