While working with the repeater control I found myself in a scenario where I have a button in the repeater for every column and on the click of that button I need to change the color(highlight) of that row. It’s a very usual case where we need to prepopulate some controls from the databound controls and for good user experience we want to highlight that particular row.
Before using the repeater control i was using the GridView where it is very easy task like you can do it in ‘RowDatabound’ event of GridView and then get the DataRow and highlight it.
Eg:
ProtectedSub GridView_RowDataBound(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound 'Creates the datarowview object Dim l_dtRview As Data.DataRowView = CType(e.Row.DataItem, Data.DataRowView) 'Check for the data row If e.Row.RowType = DataControlRowType.DataRow Then 'Change the back color If (condition) Then 'highlight the row e.Row.BackColor = Drawing.Color.FromArgb(232, 217, 195) EndIf EndSub
But as in the Repeater there is no ‘RowDataBound’ event and furthur we can’t find a row. So we have to use another trick to do it. So to highlight the row you need to make the row of the table that you are creating in the Repeater, a server side row and assign it some id
ex:
Now on the ItemDataBound Perform the following task:
ProtectedSub Repeater_ItemDataBound(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) HandlesRepeater.ItemDataBound 'get the dataRow Dim l_objDtRowView As DataRowView = CType(e.Item.DataItem, DataRowView) 'If Data Row is not null IfNot l_ objDtRowView IsNothingThen 'Check the condition If (Check Condition) Then 'If selected then change the color of the row Dim l_objHtmlAreaCell As System.Web.UI.HtmlControls.HtmlTableRow = DirectCast(e.Item.FindControl("rwSeleceted"), System.Web.UI.HtmlControls.HtmlTableRow) l_objHtmlAreaCell.Style.Add("background-color", SELECTED_ROW_COLOR) EndIf EndSub
Now on the ItemDataBound Perform the following task. So here we are checking the condition and then finding the Id of the row and highlighting it.