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

get gridboundcolum text

22 Answers 138 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lisa
Top achievements
Rank 1
Lisa asked on 27 Jul 2012, 05:06 PM
How can I get the text of phonenumber in code behind with the following code?

<

 

telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumber" AllowFiltering="false" />

 

 

<telerik:GridTemplateColumn>

 

 

      <ItemTemplate>

 

 

           <asp:LinkButton ID="LinkButton1" runat="server" Text="get phone number" OnClick="getphone"></asp:LinkButton>

 

 

      </ItemTemplate>

 

 

</telerik:GridTemplateColumn>

 

Protected

 

Sub getphone(ByVal sender As Object, ByVal e As System.EventArgs)

 

 

 

    Dim dataItem As GridDataItem

 

 

    Dim cell As TableCell = dataItem("phonenumber")

 

 

    Dim itemValue As String = dataItem["phonenumber"].Text  --------this gives error: Identifier expected.

    I also try the following and still cannot get the value of the phonenumber field:

 

    RadGrid1.MasterTableView.GetColumn(

"phonenumber").ToString())

 

 

End Sub

 

22 Answers, 1 is accepted

Sort by
0
Lisa
Top achievements
Rank 1
answered on 27 Jul 2012, 05:08 PM

Here's an easier to read version:

<telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumber" AllowFiltering="false" /> 
<telerik:GridTemplateColumn
    <ItemTemplate
           <asp:LinkButton ID="LinkButton1" runat="server" Text="get phone number" OnClick="getphone"></asp:LinkButton
      </ItemTemplate
</telerik:GridTemplateColumn>
  
Protected Sub getphone(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim dataItem As GridDataItem 
    Dim cell As TableCell = dataItem("phonenumber")
    Dim itemValue As String = dataItem["phonenumber"].Text  --------this gives error: Identifier expected.
  
    I also try the following and still cannot get the value of the phonenumber field:
    RadGrid1.MasterTableView.GetColumn("phonenumber").ToString())
End Sub 
0
Lisa
Top achievements
Rank 1
answered on 29 Jul 2012, 02:25 AM
Does anyone know the answer? Thanks.
0
Shinu
Top achievements
Rank 2
answered on 30 Jul 2012, 05:12 AM
Hello Lisa,

Try accessing the column value as shown below.
VB:
Protected Sub getphone(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim link As LinkButton = DirectCast(sender, LinkButton)
    Dim item As GridDataItem = DirectCast(link.NamingContainer, GridDataItem)
    Dim value As String = item("phonenumber").Text
End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 30 Jul 2012, 03:59 PM
Hello Shinu,
Thank you so much for the code; it works great.

Is it possible to combine the LinkButton with the phoneNumber column so that the grid does not show two separate columns? I mean to keep all of that functionality and make phoneNumber a hyperlink or linkbutton so when it is click it will invoke the sub. This way there is no need to have a separate linkbutton column.
0
Shinu
Top achievements
Rank 2
answered on 31 Jul 2012, 05:39 AM
Hello,

One suggestion is that you can add a linkbutton dynamically to the GridBoundColumn. 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.
aspx:
<MasterTableView  DataKeyNames="phonenumber" . . .. >

VB:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim link As New LinkButton()
        link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
        item("phonenumber").Controls.Add(link)
    End If
End Sub
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim link As New LinkButton()
        link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
        item("phonenumber").Controls.Add(link)
    End If
End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 31 Jul 2012, 07:23 PM
I try that and with debug it does not even executing RadGrid1_ItemCreated() or RadGrid1_ItemDataBound() subs. May be the MasterTableView DataKeyNames is set to some thing else. Remember that I use GridBoundColumn for the phonenumber.
<telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumber" AllowFiltering="false" />  
0
Shinu
Top achievements
Rank 2
answered on 01 Aug 2012, 04:24 AM
Hi,

You can set multiple DataKeyNames in MasterTableView. Also the event sequence is firing as expected at my end and the GridBoundColumn cell is rendered as LinkButton.
aspx:
<MasterTableView DataKeyNames="phonenumber,Id" . . .>
VB:
RadGrid1.ItemDataBound += New GridItemEventHandler(RadGrid1_ItemDataBound)
RadGrid1.ItemCreated += New GridItemEventHandler(RadGrid1_ItemCreated)
Private Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim link As New LinkButton()
        link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
        item("phonenumber").Controls.Add(link)
    End If
End Sub
Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim link As New LinkButton()
        link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
        item("phonenumber").Controls.Add(link)
    End If
End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 01 Aug 2012, 05:13 PM
<MasterTableView DataKeyNames="Id,phonenumber" AllowMultiColumnSorting="true" ShowHeadersWhenNoRecords="false">

I added phonenumber to the DataKeyNames list and I get this error:
            phonenumber is neither a DataColumn nor a DataRelation for table .

Also these statements have compilation error: Declaration expected.
RadGrid1.ItemDataBound += New GridItemEventHandler(RadGrid1_ItemDataBound)
RadGrid1.ItemCreated += New GridItemEventHandler(RadGrid1_ItemCreated)
0
Shinu
Top achievements
Rank 2
answered on 02 Aug 2012, 06:27 AM
Hi,

The proper way to add an event handler in VB code-behind is the following.
VB:
AddHandler RadGrid1.ItemDataBound, AddressOf RadGrid1_ItemDataBound
AddHandler RadGrid1.ItemCreated, AddressOf RadGrid1_ItemCreated
Also make sure that the "phonenumber" field exists in your datasource.

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 02 Aug 2012, 07:11 PM

AddHandler

 

is not recognized, it gives syntax error.

0
Shinu
Top achievements
Rank 2
answered on 03 Aug 2012, 04:58 AM
Hello,

Try attaching the events from code as shown below.
VB:
Private Sub RadGrid1_ItemDataBound(sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
 End Sub
 Private Sub RadGrid1_ItemCreated(sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 03 Aug 2012, 03:14 PM
Now it gives error "phonenumber is neither a DataColumn nor a DataRelation for table".
I try to fix it by adding to the MasterTableView this attribute: ShowHeadersWhenNoRecords="false"
but still getting the same error.
0
Shinu
Top achievements
Rank 2
answered on 06 Aug 2012, 05:59 AM
Hi Lisa,

I cannot replicate the error at my end. The above code is working as expected. Please make sure that the "phonenumber" field exists in your datasource. Please provide your code so that I can assist you better.

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 09 Aug 2012, 09:48 PM
Here's the code:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="false">
  <MasterTableView DataKeyNames="phonenumber,Id" AllowMultiColumnSorting="true" ShowHeadersWhenNoRecords="false">
     <Columns>
        <telerik:GridBoundColumn DataField="Id" HeaderText="ID" UniqueName="IdColumn" AllowFiltering="false" />
        <telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumber" AllowFiltering="false" />   
     </Columns>
  </MasterTableView>
</telerik:RadGrid>
  
  
        Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
            If TypeOf e.Item Is GridDataItem Then
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
                Dim link As New LinkButton()
                link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumberColumn").ToString()
                item("phonenumberColumn").Controls.Add(link)
            End If
        End Sub
  
        Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
            If TypeOf e.Item Is GridDataItem Then
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
                Dim link As New LinkButton()
                link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumberColumn").ToString()
                item("phonenumberColumn").Controls.Add(link)
            End If
        End Sub
0
Shinu
Top achievements
Rank 2
answered on 10 Aug 2012, 04:54 AM
Hi,.

I have made some modifications in your code. Make sure that you are adding the control to the column using its UniqueName property(phonenumber here instead of phonenumberColumn). Since you are setting DataKeyValues as phonenumber which is a DataField in your DataSource, you need to access it using phonenumber.
aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="false">
  <MasterTableView DataKeyNames="phonenumber,Id" AllowMultiColumnSorting="true" ShowHeadersWhenNoRecords="false">
     <Columns>
        <telerik:GridBoundColumn DataField="Id" HeaderText="ID" UniqueName="IdColumn" AllowFiltering="false" />
        <telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumber" AllowFiltering="false" />  
     </Columns>
  </MasterTableView>
</telerik:RadGrid>
VB:
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
           If TypeOf e.Item Is GridDataItem Then
               Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
               Dim link As New LinkButton()
               link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
               item("phonenumber").Controls.Add(link)
           End If
       End Sub
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
           If TypeOf e.Item Is GridDataItem Then
               Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
               Dim link As New LinkButton()
               link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
               item("phonenumber").Controls.Add(link)
           End If
       End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 11 Aug 2012, 02:22 AM
Sorry I mistyped the uniquename. It should be phonenumberColumn. So the code is still not working. I am not sure if it is OK to add phonenumberColumn to DataKeyNames because it is not a primary key in the table. Id is the primary key.
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="false">
 
  <MasterTableView DataKeyNames="phonenumberColumn,Id" AllowMultiColumnSorting="true" ShowHeadersWhenNoRecords="false">
 
     <Columns>
 
        <telerik:GridBoundColumn DataField="Id" HeaderText="ID" UniqueName="IdColumn" AllowFiltering="false" />
 
        <telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumberColumn" AllowFiltering="false" />   
 
     </Columns>
 
  </MasterTableView>
 
</telerik:RadGrid>
 
   
  
   
  
        Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
 
            If TypeOf e.Item Is GridDataItem Then
 
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
 
                Dim link As New LinkButton()
 
                link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumberColumn").ToString()
 
                item("phonenumberColumn").Controls.Add(link)
 
            End If
 
        End Sub
 
   
  
        Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
 
            If TypeOf e.Item Is GridDataItem Then
 
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
 
                Dim link As New LinkButton()
 
                link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumberColumn").ToString()
 
                item("phonenumberColumn").Controls.Add(link)
 
            End If
 
        End Sub

0
Shinu
Top achievements
Rank 2
answered on 13 Aug 2012, 04:42 AM
Hi,

The DataKeyNames must be set same as that in the DataField i.e phonenumber instead of phonenumberColumn.
aspx:
<MasterTableView DataKeyNames="phonenumber,Id">
</MasterTableView>
VB:
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridDataItem Then
Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
Dim link As New LinkButton()
link.Text = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("phonenumber").ToString()
item("phonenumberColumn").Controls.Add(link)
End If
End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 13 Aug 2012, 08:39 PM
Ahhh... finally that works, it shows the phonenumber as a hyperlink. Thank you. How can I make the hyperlink to open up an Outlook email with the To field populated? I try link.PostbackUrl  before adding the link but does not pass compilation error: Expression does not produce a value.
Dim emailaddr = "mailto:a@abc.com"
link.PostBackUrl = ClientScript.RegisterStartupScript(Me.GetType(), _
"mailto", "<script type = 'text/javascript'>" & _
"parent.location='" & emailaddr & "'</script>")
0
Shinu
Top achievements
Rank 2
answered on 14 Aug 2012, 05:05 AM
Hi Lisa,

You can attach onclick event to the link button and then open Outlook email.
VB:
Private Sub link_Click(sender As Object, e As EventArgs)
    ClientScript.RegisterStartupScript(Me.[GetType](), "Loading", "window.open('mailto:a@abccom','email');", True)
End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 14 Aug 2012, 03:15 PM
Hi Shinu, I add the sub link_Click but the page just refreshes when I click the link. With debug I see it does not even invoke that sub.

I add link.ID="myLinkButton" and change the sub link_Click to Sub myLinkButton_Click and that does not work either.
0
Shinu
Top achievements
Rank 2
answered on 16 Aug 2012, 04:57 AM
Hi,

Try attaching the event as shown below.
VB:
AddHandler link.Click, AddressOf link_click
Private Sub link_click(ByVal sender As Object, ByVal e As EventArgs)
'do something
 End Sub

Thanks,
Shinu.
0
Lisa
Top achievements
Rank 1
answered on 16 Aug 2012, 09:27 PM
I try this and get Compiler Error Message: BC30287: '.' expected.
If I change link_Click to link.Click I get compilation error -- it does not recognize link.
Private Sub link_Click(ByVal sender As Object, ByVal e As EventArgs) Handles link_Click 
    ClientScript.RegisterStartupScript(Me.[GetType](), "Loading", "window.open('mailto:a@abccom','email');", True)
End Sub

What is AddHandler that you are  using? My VB code does not recognize it.
Tags
General Discussions
Asked by
Lisa
Top achievements
Rank 1
Answers by
Lisa
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or