get gridboundcolum text

23 posts, 0 answers
  1. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 27 Jul 2012 Link to this post

    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

     

  2. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 27 Jul 2012 Link to this post

    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 
  3. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 28 Jul 2012 Link to this post

    Does anyone know the answer? Thanks.
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 Jul 2012 Link to this post

    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.
  5. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 30 Jul 2012 Link to this post

    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.
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 31 Jul 2012 Link to this post

    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.
  7. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 31 Jul 2012 Link to this post

    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" />  
  8. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 31 Jul 2012 Link to this post

    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.
  9. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 01 Aug 2012 Link to this post

    <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)
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Aug 2012 Link to this post

    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.
  11. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 02 Aug 2012 Link to this post

    AddHandler

     

    is not recognized, it gives syntax error.

  12. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 02 Aug 2012 Link to this post

    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.
  13. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 03 Aug 2012 Link to this post

    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.
  14. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 Aug 2012 Link to this post

    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.
  15. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 09 Aug 2012 Link to this post

    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
  16. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Aug 2012 Link to this post

    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.
  17. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 10 Aug 2012 Link to this post

    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

  18. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Aug 2012 Link to this post

    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.
  19. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 13 Aug 2012 Link to this post

    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>")
  20. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 Aug 2012 Link to this post

    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.
  21. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 14 Aug 2012 Link to this post

    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.
  22. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Aug 2012 Link to this post

    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.
  23. Lisa
    Lisa avatar
    43 posts
    Member since:
    Jul 2012

    Posted 16 Aug 2012 Link to this post

    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.
Back to Top