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

Getting the key value to pass in another page from a LinkButton

5 Answers 344 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 2
Bill asked on 13 Mar 2013, 03:02 PM
I have a grid which has the CarrierID specified as the "DataKeyNames" which is also an invisible property in the grid.

I have a hyperlink column that when clicked should pass the value of the CarrierNetID (the keyvalue for that dataitem) into my codebehind so I can then pass it to another page to bring up a record for editng based on that key value.

I'm having trouble getting the attached error:

My markup is as follows: You can see that the lnkPLMN is the hyperlink. Everything is coming out on the grid just fine, I just can't seem to pass the value.
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" ShowStatusBar="true"
        runat="server" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True"
        AutoGenerateColumns="false" ClientSettings-Resizing-AllowColumnResize="true"
        DataKeyNames="CarrierNetID" OnItemDataBound="RadGrid_ItemDataBound">
        <MasterTableView PageSize="10" Width="100%">
            <Columns>
                <telerik:GridBoundColumn DataField="CarrierNetID" Visible="false" UniqueName="CarrierNetID" ItemStyle-HorizontalAlign="Left"
                    HeaderStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" HeaderText="Carrier Name" UniqueName="Name"
                    SortExpression="Name" ReadOnly="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Region" HeaderText="Region" UniqueName="Region"
                    AllowFiltering="true" SortExpression="Region" ReadOnly="true" ItemStyle-HorizontalAlign="Left"
                    HeaderStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Country" HeaderText="Country" UniqueName="Country"
                    SortExpression="Country" ReadOnly="true" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="PLMN" UniqueName="PLMN">
                    <ItemTemplate>
                        <asp:HyperLink ID="lnkPLMN" runat="server" Target="_blank" ForeColor="Blue">
                        </asp:HyperLink>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="TechnologyTypeCode" HeaderText="Technology" UniqueName="TechnologyTypeCode"
                    SortExpression="TechnologyTypeCode" ReadOnly="true" ItemStyle-HorizontalAlign="Left"
                    HeaderStyle-HorizontalAlign="Left">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="SMSMORate" HeaderText="SSMSO Rate" UniqueName="SMSMORate"
                    AllowFiltering="false" SortExpression="SMSMORate" ReadOnly="true" ItemStyle-HorizontalAlign="Right"
                    HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="SMSMTRate" HeaderText="SMSMT Rate" UniqueName="SMSMTRate"
                    AllowFiltering="false" SortExpression="SMSMTRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right"
                    HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="DataRate" HeaderText="Data Rate" UniqueName="DataRate"
                    AllowFiltering="false" SortExpression="DataRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right"
                    HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="MOCRate" HeaderText="MOC Rate" UniqueName="MOCRate"
                    AllowFiltering="false" SortExpression="MOCRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right"
                    HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="MTCRate" HeaderText="MTC Rate" UniqueName="MTCRate"
                    AllowFiltering="false" SortExpression="MTCRate" ReadOnly="true" ItemStyle-HorizontalAlign="Right"
                    HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:G}">
                </telerik:GridBoundColumn>
                <telerik:GridCheckBoxColumn DataField="FlatRate" HeaderText="Flat Rate" UniqueName="FlatRate"
                    SortExpression="FlatRate" ReadOnly="true" ItemStyle-HorizontalAlign="Center"
                    HeaderStyle-HorizontalAlign="Center">
                </telerik:GridCheckBoxColumn>
                <telerik:GridCheckBoxColumn DataField="Discount" HeaderText="Discount" UniqueName="Discount"
                    SortExpression="Discount" ReadOnly="true" ItemStyle-HorizontalAlign="Center"
                    HeaderStyle-HorizontalAlign="Center">
                </telerik:GridCheckBoxColumn>
                <telerik:GridCheckBoxColumn DataField="ActiveFlag" HeaderText="Active" UniqueName="ActiveFlag"
                    SortExpression="ActiveFlag" ReadOnly="true" ItemStyle-HorizontalAlign="Center"
                    HeaderStyle-HorizontalAlign="Center">
                </telerik:GridCheckBoxColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings EnableRowHoverStyle="true">
        </ClientSettings>
        <PagerStyle Mode="NumericPages"></PagerStyle>
    </telerik:RadGrid>


Here is my codebehind which gets the attached error on the following line:
HyperLink link = (HyperLink)item["PLMN"].Controls[0]; 


protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            HyperLink link = (HyperLink)item["PLMN"].Controls[0];
            string value = item.GetDataKeyValue("CarrierNetID").ToString();
            link.NavigateUrl = "~/CarrierLaunchStatusForm.aspx?addRecord=0&ID=" + value;
        }
    }


Can someone please help?
Code behind:



5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Mar 2013, 04:03 AM
Hi,

Try modifying your code as shown below.
C#:
protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            HyperLink link = (HyperLink)item.FindControl("lnkPLMN");
            string value = item.GetDataKeyValue("CarrierNetID").ToString();
            link.NavigateUrl = "~/CarrierLaunchStatusForm.aspx?addRecord=0&ID=" + value;
        }
}

Thanks,
Shinu
0
Bill
Top achievements
Rank 2
answered on 14 Mar 2013, 12:38 PM
Shinu, when I run it, I'm getting the attached error.

During debugging, I'm trying to see what content is in the item variable, but I don't see any pertinent properties for the DataKeyValue.

Could you please help me find out what else I need to do in order to get it working? I'm stumped on this one. Should be a simple thing to do...

Thanks so much,

Bill....
0
Bill
Top achievements
Rank 2
answered on 14 Mar 2013, 12:59 PM
Shinu, I also tried putting in a hyperlink column instead of the code behind and am getting the below error:

After placing the hyperlink in the markup (instead of the code behind), I'm getting the following compilation error:

Error 2 The GridHyperLinkColumn control with a two-way databinding to field PLMN must have an ID. C:\Projects\NPP\NPP\NPP\NPP\CarrierLaunchStatus.ascx 53 
Error 3 Literal content ('</telerik:GridHyperLinkColumn>') is not allowed within a 'Telerik.Web.UI.GridColumnCollection'. C:\Projects\NPP\NPP\NPP\NPP\CarrierLaunchStatus.ascx 56

The Hyperlink column looks as follows:

<telerik:GridHyperLinkColumn UniqueName="PLMN" DataTextFormatString='<%# Bind("PLMN") %>' DataTextField="PLMN" DataNavigateUrlFields="CarrierNetID" DataNavigateUrlFormatString="~/CarrierLaunchStatusForm.aspx?addRecord=0&ID={0}" Target="_blank"> </telerik:GridHyperLinkColumn> 

What am I missing here?

I would appreciate the correct way for both the markup and the codebehind so I know in the future how to handle this type of column.

Thanks so much for your help.

0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Mar 2013, 01:57 PM
Hello,

Please add below line code in your aspx page in grid.
<MasterTableView DataKeyNames="CarrierNetID"   >



protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            HyperLink link = (HyperLink)item.FindControl("lnkPLMN");
            string value = item.GetDataKeyValue("CarrierNetID").ToString();
            link.NavigateUrl = "~/CarrierLaunchStatusForm.aspx?addRecord=0&ID=" + value;
        }
}


Thanks,
Jayesh Goyani
0
Bill
Top achievements
Rank 2
answered on 14 Mar 2013, 05:25 PM
Thank you so much... Problem resolved...

I forgot to put the DataKeyNames on the MasterTableView.
Tags
Grid
Asked by
Bill
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Bill
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
Share this question
or