I can't seem to find any examples of accessing a control in a template from code-behind. My grid is bound to a dataset, and the bound data fields display fine. I want to have a hyperlink in one of the parent columns, however, so I did this:
<ig:WebHierarchicalDataGrid ID="WebHierarchicalDataGrid1" DataKeyFields="T$ORNO,T$ITEM" AutoGenerateBands="false" AutoGenerateColumns="false" runat="server" Height="350px" Width="800px"> <Columns> <ig:TemplateDataField Header-Text="RMA" Key="T$ORNO,T$ITEM"> <ItemTemplate> <asp:HyperLink ID="lnkRMA" runat="server" /> </ItemTemplate> </ig:TemplateDataField> <ig:BoundDataField DataFieldName="T$PRDI" Key="T$PRDI" Header-Text="Product" /> </Columns>
If I was using a standard GridView control, I would set the Hyperlink properties in my code-behind like this:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim itemType As ListItemType = CType(e.Row.RowType, ListItemType)
If itemType = ListItemType.Item Or itemType = ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim lnkRMA As HyperLink = CType(e.Row.FindControl("lnkRMA"), HyperLink)
lnkRMA.Text = drv("T$ORNO").ToString & "." & drv("T$ITEM").ToString
etc.
How do I accomplish the same thing with the WHDG? Which event would I use?
Hi,
If you want to set the values of the hyperlink for each row you can set them in the InitializeRow event. if you want to access the control during an update process you can access it in the RowUpdating event. the following link demonstrates using item templates:
http://help.infragistics.com/NetAdvantage/ASPNET/2010.1/CLR3.5/?page=WebDataGrid_Using_Item_Template.html
Please update the forum post regarding any questions.
Magued
I am following up with you and if the issue was resolved.
Hi Magued,
You state that "If you want to set the values of the hyperlink for each row you can set them in the InitializeRow event.", but the link you provided does not provide any example of this. This is what I tried:
Protected Sub WebHierarchicalDataGrid1_InitializeRow(ByVal sender As Object, ByVal e As Infragistics.Web.UI.GridControls.RowEventArgs) Handles WebHierarchicalDataGrid1.InitializeRow
lnkRMA.Text = "AAA"
This code fails -- the hyperlink is not found. What I need to know is how I can obtain a reference to my hyperlink! Pointing me to examples that use the designer is not helpful. I do not use the designer.
Thanks,
Mark
Your code in the InitializeRow event may look similar to:
HyperLink myhyperlink = e.Row.Items[0].FindControl("HyperLink1") as HyperLink;
In VB.net
Dim myhyperlink As HyperLink = TryCast(e.Row.Items(0).FindControl("HyperLink1"), HyperLink)
The link that I sent you doesn't point to examples that uses the designer. The Templated column is accessed at runtime.
Thank you for the example -- that's what I was looking for. Unfortunately, FindControl only seems to work within a specified grid column. This means that, if I swap columns in my markup, I will have to remember to update the code-behind as well.
The nice thing about the MSFT grid controls is that FindControl works on the entire row, regardless of which column the control is in. I can live with this, though.
Thanks for the update. Please update the forum for any further discussion.