This is a migrated thread and some comments may be shown as answers.

Dynamically adding hyperlink to grid disappearing on loading detail table

1 Answer 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pradip
Top achievements
Rank 1
pradip asked on 28 Dec 2012, 10:11 AM
Hi

I am adding hyperlink in column of mastertable based on certain condition inside itemdatabound event. Same grid has detail table, when I am trying to get detail table data - data is coming properly and getting bind as expected but only hyperlink in master table is disappearing. Please guide me how to retain hyperlink while loading detail table. I m developing in Vb.net.

thanks,
Pradip

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Dec 2012, 10:30 AM
Hi,

In general the proper place for adding controls to the grid items is in ItemCreated. But in the case of adding controls to the cells of GridBoundColumn,you cannot use ItemCreated only, but a combination of ItemCreated and ItemDataBound. This is due to the fact that the control created in ItemCreated will be erased when data-binding this control. Also, if you create the control in ItemDataBound when the controls are created from ViewState, the grid will not raise ItemDataBound, and the control will not be created and would not raise postback events. The solution for such cases is to create the control in ItemDataBound and recreate this control if needed on ItemCreated for subsequent postbacks. Here is the sample code that I tried which worked as expected.
VB:
Protected Sub grid_ItemCreated(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "Detailtable1" Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim hlControl As New HyperLink()
        hlControl.Text = "text"
        hlControl.NavigateUrl = "Page.aspx"
        item("Uniquename").Controls.Add(hlControl)
    End If
End Sub
Protected Sub grid_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "Detailtable1" Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim hlControl As New HyperLink()
        hlControl.Text = "text"
        hlControl.NavigateUrl = "Page.aspx"
        item("Uniquename").Controls.Add(hlControl)
    End If
End Sub

Thanks,
Shinu.
Tags
Grid
Asked by
pradip
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or