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

get linkbutton cell value in grid server side

2 Answers 139 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chom
Top achievements
Rank 1
Chom asked on 22 Jan 2010, 05:00 AM
Hi all. I need some help to get a value from a RadGrid. I have a RadGrid that contains several linkbutton fields. I am currently able to get a row value of a cell when I click on one of these links no problem. The problem I am having is I need to click on a link and then get the cell value of another link in the same row. Has anyone had any experience with this?

Here is my grid. I need to click on the Delete row and get the value of the SalesID row.

<

 

telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AllowPaging="True"

 

 

 

runat="server" GridLines="None" Width="100%" Skin="Web20"

 

 

 

PageSize="20">

 

 

 

<PagerStyle Mode="Slider" />

 

 

 

<HeaderStyle HorizontalAlign="Left" />

 

 

 

<ItemStyle HorizontalAlign="Left" />

 

 

 

<AlternatingItemStyle HorizontalAlign="Left" />

 

 

 

<MasterTableView CommandItemDisplay="TopAndBottom" AutoGenerateColumns="False"

 

 

 

DataKeyNames="SalesID" DataSourceID="SqlDataSource1" PageSize="20">

 

 

 

<Columns>

 

 

 

<telerik:GridButtonColumn CommandName="SalesID" DataTextField="SalesID"

 

 

 

HeaderText="Sales ID" UniqueName="SalesID" CommandArgument="SalesID">

 

 

 

</telerik:GridButtonColumn>

 

 

 

<telerik:GridBoundColumn DataField="UserName" HeaderText="UserName"

 

 

 

SortExpression="UserName" UniqueName="UserName">

 

 

 

</telerik:GridBoundColumn>

 

 

 

<telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name"

 

 

 

SortExpression="FirstName" UniqueName="FirstName">

 

 

 

</telerik:GridBoundColumn>

 

 

 

<telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name"

 

 

 

SortExpression="LastName" UniqueName="LastName">

 

 

 

</telerik:GridBoundColumn>

 

 

 

<telerik:GridBoundColumn DataField="HomePhone" HeaderText="Home Phone"

 

 

 

SortExpression="HomePhone" UniqueName="HomePhone">

 

 

 

</telerik:GridBoundColumn>

 

 

 

<telerik:GridBoundColumn DataField="CellPhone" HeaderText="Cell Phone"

 

 

 

SortExpression="CellPhone" UniqueName="CellPhone">

 

 

 

</telerik:GridBoundColumn>

 

 

 

<telerik:GridButtonColumn CommandName="Email" HeaderText="Email" Text="Mail"

 

 

 

UniqueName="Email">

 

 

 

</telerik:GridButtonColumn>

 

 

 

<telerik:GridButtonColumn CommandName="Notes" HeaderText="Notes" Text="Edit"

 

 

 

UniqueName="Notes">

 

 

 

</telerik:GridButtonColumn>

 

 

 

<telerik:GridButtonColumn CommandName="Delete" HeaderText="Del<br/>Acc"

 

 

 

Text="Del" UniqueName="Delete">

 

 

 

</telerik:GridButtonColumn>

 

 

 

</Columns>

 

 

 

<PagerStyle Mode="Slider" Position="TopAndBottom" />

 

 

 

<CommandItemTemplate>

 

 

 

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

 

 

 

</CommandItemTemplate>

 

 

 

</MasterTableView>

 

 

 

</telerik:RadGrid>

This is what I am doing in code behind. I am using the e.CommandName to display the values in 2 labels but I cannot get the value of the SalesID.
 

 

 

 

 

If e.CommandName = "Delete" Then

 

 

Dim myItem As GridDataItem = DirectCast(e.Item, GridDataItem)

 

 

Dim cell As TableCell = myItem("UserName")

 

 

Dim cellSID As TableCell = myItem("SalesID")

 

 

lblSalesID.Text = cell.Text

 

lblUserName.text = cellSID.Text

 

End If

 

 

 

 

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Jan 2010, 06:14 AM
Hello Gavin,

Since you have set the DatyaKeyNames as SalesID, a better option is retrieving the value using GetDataKeyValue method.
    lblUserName.Text = myItem.GetDataKeyValue("CustomerID").ToString()

Also you can access the linkbutton and get the text as shown below.
VB:
Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
    If e.CommandName = "Delete" Then 
        Dim myItem As GridDataItem = DirectCast(e.Item, GridDataItem) 
        lblSalesID.Text = myItem("UserName").Text 
        '  lblUserName.Text = myItem.GetDataKeyValue("SalesID").ToString(); ' Usiing GetDataJKeyValue 
        Dim lnkButton As LinkButton = DirectCast(myItem("SalesID").Controls(0), LinkButton) 
        lblUserName.Text = lnkButton.Text 
    End If 
End Sub 

-Shinu.
0
Chom
Top achievements
Rank 1
answered on 23 Jan 2010, 12:41 AM
Hi Shinu

Works like a charm. So simple, this had me foxed for a while.

Thanks for your help.
Tags
Grid
Asked by
Chom
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Chom
Top achievements
Rank 1
Share this question
or