get cell value on button click

1 Answer 106 Views
Grid
Michel
Top achievements
Rank 1
Michel asked on 31 Mar 2022, 07:57 AM

Hello,

 

i am trying to read the values of columns on button click in a telerik:GridTemplateColumn but seems not working


<telerik:RadGrid RenderMode="Lightweight" runat="server" AutoGenerateColumns="False" ID="RadGrid1"
AllowFilteringByColumn="false"  Width="100%" BorderWidth="0px" GridLines="Both"
AlternatingItemStyle-HorizontalAlign="Center" AllowSorting="false">
<MasterTableView TableLayout="Fixed" HeaderStyle-Font-Bold="true" ClientDataKeyNames="RepCRD" DataKeyNames="RepCRD" >
<Columns>
<telerik:GridBoundColumn DataField="RepCRD" Visible="false" HeaderText="RepCRD" UniqueName="RepCRD"  ReadOnly="True" />
<telerik:GridTemplateColumn HeaderText=""  UniqueName="AddAdv" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" AllowSorting="false" AllowFiltering="false">
<ItemTemplate>
<telerik:RadButton ID="btnAddAdv" CommandName="AddAdv" OnClick="btnAddAdv_Click" RenderMode="Lightweight" runat="server" AutoPostBack="true" Text="Add Advisor" />
<asp:Label ID="RepCRDlbl" Visible="false" Text='<%# Eval("RepCRD")%>' runat="server"></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridHyperLinkColumn HeaderText="CRD#" DataTextFormatString="{0}" DataNavigateUrlFields="RepCRD" Target="_blank" ItemStyle-HorizontalAlign="Center"  UniqueName="RepCRD"  DataTextField="RepCRD" />
<telerik:GridBoundColumn DataField="FullName" HeaderText="Name" SortExpression="FullName" UniqueName="FullName" ItemStyle-HorizontalAlign="Center" />
<telerik:GridHyperLinkColumn HeaderText="Broker/Dealer Firm CRD#" DataTextFormatString="{0}" DataNavigateUrlFields="BDFirmCRD" Target="_blank" ItemStyle-HorizontalAlign="Center" UniqueName="BDFirmCRD"  DataTextField="BDFirmCRD" />
<telerik:GridBoundColumn DataField="BrokerDealerName" HeaderText="Broker/Dealer Name" SortExpression="BrokerDealerName" UniqueName="BrokerDealerName" ItemStyle-HorizontalAlign="Center" />
<telerik:GridTemplateColumn HeaderText="" UniqueName="AddFirm1" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" AllowSorting="false" AllowFiltering="false">
<ItemTemplate>
<telerik:RadButton ID="btnAddFirm1" CommandName="AddFirm1" OnClick="btnAddFirm1_Click" RenderMode="Lightweight" runat="server" AutoPostBack="true" Text="Add Firm" />
<asp:hiddenfield ID="BDFirmCRDlbl"  Value='<%# Eval("BDFirmCRD")%>' runat="server"></asp:hiddenfield>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>

On Click of Button btnAddFirm1 it should read the BDFirmCRDlbl value like here


    Protected Sub btnAddFirm1_Click(sender As Object, e As EventArgs)
        Dim Button As RadButton = CType(sender, RadButton)
        Dim sBDFirmCRDlbl As String
        For Each item As GridDataItem In RadGrid1.MasterTableView.Items
            Dim strBDFirmCRD As String = item("BDFirmCRD").Text
            sBDFirmCRDlbl = (TryCast(item.FindControl("BDFirmCRDlbl"), HiddenField)).Value
        Next


    End Sub

the problem that it is only returning the last item in the grid and not the clicked value

 

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Doncho
Telerik team
answered on 04 Apr 2022, 04:26 PM

Hi Michel,

You can get the GridDataItem holding the clicked button via the NamingContainer property of the Button itself. Then you can search for the hidden field in the respective grid cell, as demonstrated in the Accessing Controls in Template Column section.

In the current case, you can try the code below:

Protected Sub btnAddFirm1_Click(sender As Object, e As EventArgs)
    Dim button = CType(sender, RadButton)
    Dim dataItem = TryCast(button.NamingContainer, GridDataItem)
    If dataItem IsNot Nothing Then
        'get the grid cell via column's UniqueName
        Dim addFirmGridCell = dataItem("AddFirm1")
        'find the hidden field in the grid cell
        Dim hiddenField = TryCast(addFirmGridCell.FindControl("BDFirmCRDlbl"), HiddenField)
        Dim sBDFirmCRDlbl As String = hiddenField.Value
    End If
End Sub

I would also suggest you review the 6 Server-Side Debugging Tips That Will Make Developing Easier blog post explaining how to use the debugging capabilities of Visual Studio to find the NamingContainer of a Control and define its type and some more useful debugging tips.

I hope this will help you achieve the desired behavior.

Please let me know if any questions come up.

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Michel
Top achievements
Rank 1
commented on 05 Apr 2022, 12:17 PM

Thanks Doncho for your support.

The provided solution worked perfectly.

Tags
Grid
Asked by
Michel
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or