<
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
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 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").TextEnd SubThanks,
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.
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 IfEnd SubProtected 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 IfEnd SubThanks,
Shinu.
<telerik:GridBoundColumn DataField="phonenumber" HeaderText="phone number " UniqueName="phonenumber" AllowFiltering="false" /> 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" . . .>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 IfEnd SubPrivate 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 IfEnd SubThanks,
Shinu.
<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) The proper way to add an event handler in VB code-behind is the following.
VB:
AddHandler RadGrid1.ItemDataBound, AddressOf RadGrid1_ItemDataBoundAddHandler RadGrid1.ItemCreated, AddressOf RadGrid1_ItemCreatedThanks,
Shinu.
AddHandler
is not recognized, it gives syntax error.
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.ItemCreatedEnd SubThanks,
Shinu.
I try to fix it by adding to the MasterTableView this attribute: ShowHeadersWhenNoRecords="false"
but still getting the same error.
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.
<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 SubI 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>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 SubProtected 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 SubThanks,
Shinu.
<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 SubThe DataKeyNames must be set same as that in the DataField i.e phonenumber instead of phonenumberColumn.
aspx:
<MasterTableView DataKeyNames="phonenumber,Id"></MasterTableView>Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreatedIf TypeOf e.Item Is GridDataItem ThenDim 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 IfEnd SubThanks,
Shinu.
Dim emailaddr = "mailto:a@abc.com" link.PostBackUrl = ClientScript.RegisterStartupScript(Me.GetType(), _ "mailto", "<script type = 'text/javascript'>" & _ "parent.location='" & emailaddr & "'</script>")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 SubThanks,
Shinu.
I add link.ID="myLinkButton" and change the sub link_Click to Sub myLinkButton_Click and that does not work either.
Try attaching the event as shown below.
VB:
AddHandler link.Click, AddressOf link_clickPrivate Sub link_click(ByVal sender As Object, ByVal e As EventArgs)'do something End SubThanks,
Shinu.
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 SubWhat is
AddHandler that you are using? My VB code does not recognize it.