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

Select Row through Telerik LinkButton and Redirect

5 Answers 498 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wendy Hunt
Top achievements
Rank 2
Wendy Hunt asked on 19 Feb 2010, 03:55 PM
Hi -

I'm new to programming... so this might be a simple fix.  What I'd like to do is use a linkbutton to select a row of data, put that data into a session variable, then redirect to a page that will display the selected data.  Right now I'm trying to figure out how to use the linkbutton to select the row.
Here is my linkbutton
<telerik:GridButtonColumn ButtonType="LinkButton" Text="Select" UniqueName="ButtonColumn">  
  <HeaderStyle Width="50px" /> 
</telerik:GridButtonColumn> 
There are no onClick properties for the GridButtonColumn.  How would I initiate the event from this button?

Next, here is what I have for the behind code... once I can figure out where to put it in an event... but I know I'm missing a lot of the code...
        RadPanelItem SOISearchItem = (RadPanelItem)RadPanelBar1.FindItemByValue("SOISearch");  
        RadGrid soiRadGrid = (RadGrid)SOISearchItem.FindControl("SoiRadGrid");  
        LinkButton linkButton = (LinkButton)SOISearchItem.FindControl("LinkButton");  
          
        if (soiRadGrid.SelectedItems.Count > 0)  
        {  
          GridItem selectedItem;  
          
          // grab selected row  
        }  
 
        Session[Tip.Utility.UIConstants.SoiSession] = soi;  // soi is my class variable  
          
        Response.Redirect("SOI.aspx");  
 

Here is my Grid, which is a table of what I'm calling SOI data (I have an SOI class in the BO):
                      <telerik:RadGrid ID="SoiRadGrid" runat="server"   
                                    AllowSorting="False"   
                                    GridLines="None"   
                                    Skin="WebBlue" 
                                    AllowFilteringByColumn="False"   
                                    AllowPaging="False"   
                                    ShowGroupPanel="True">  
                      <ClientSettings Resizing-AllowColumnResize="true" AllowKeyboardNavigation="false"></ClientSettings> 
                      <MasterTableView AutoGenerateColumns="False" PageSize="5">  
                        <RowIndicatorColumn> 
                          <HeaderStyle Width="20px" /> 
                        </RowIndicatorColumn> 
                        <ExpandCollapseColumn> 
                          <HeaderStyle Width="20px" /> 
                        </ExpandCollapseColumn> 
 
                        <Columns> 
                            <telerik:GridButtonColumn ButtonType="LinkButton" Text="Select" UniqueName="ButtonColumn">  
                              <HeaderStyle Width="50px" /> 
                            </telerik:GridButtonColumn> 
                            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" Visible="false"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Tier" HeaderText="Tier"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Org" HeaderText="Org"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="ProjectGroup" HeaderText="Project Group"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="RequestingEntity" HeaderText="Requesting Entity"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="SubmittalDate" HeaderText="Submittal Date"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="ContactInfo" HeaderText="Contact Info"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Unredacted" HeaderText="Unredacted"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Redacted" HeaderText="Redacted"></telerik:GridBoundColumn> 
                        </Columns> 
                        
                      </MasterTableView> 
                      <ClientSettings AllowDragToGroup="True">  
                        <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
                      </ClientSettings> 
                    </telerik:RadGrid> 

I want to use the ID column, that is hidden, to grab the soi data.

What other information would be helpful to give out to solve my problem?  Did I not explain something well enough?

Also, we are using an Entity Framework, and Visual Studio 2008.

wen






5 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Feb 2010, 05:06 AM
Hello Wendy Hunt,

You can set custom CommandName for the GridButtonColumn and check for the CommandName in the ItemCommand event. Then perform the required functions and redirect to page with corresponding value as url parameter.

ASPX:
 
<telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Redirect" Text="Select" UniqueName="ButtonColumn"
    <HeaderStyle Width="50px" /> 
</telerik:GridButtonColumn> 

CS:
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Redirect"
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            string value = item.GetDataKeyValue("ID").ToString(); // Get the value in clicked row 
            // Save the required  value in session 
            string url = "Page2.aspx?id="+value; 
            Response.Redirect(url); 
        } 
    } 

Also set the DataKeyNames as ID in order to retrieve the keyvalue using GetDataKeyValue() method.

                <MasterTableView DataSourceID="SqlDataSource1"  DataKeyNames="ID">

Best wishes,
Shinu.
0
Wendy Hunt
Top achievements
Rank 2
answered on 23 Feb 2010, 04:15 PM
Hi Shinu -

It worked, but now I'm getting a DataKeyValue error on another method that uses the same table.

This is my error message...
//There was a problem extracting DataKeyValues from the DataSource.

 

//Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.

 


The method that is throwing the error is a Search method that will display search results in the same table. 

So... my select button will redirect the user from the table to another page 
..but the search button will search for like results and display them in the table.

Here is my Search Method:
      private void SOIGridSearch()  
      {  
        DateTime? submittalDate = null;  
        int? participatingEntity = null;  
        int? requestingEntity = null;  
        int? location = null;  
        int? leadOrganization = null;  
        int? tier = null;  
        int? renewableSource = null;  
          
 
        RadPanelItem SOISearchItem = (RadPanelItem)RadPanelBar1.FindItemByValue("SOISearch");  
        RadGrid soiRadGrid = (RadGrid)SOISearchItem.FindControl("SoiRadGrid");  
        RadDatePicker submittalDateRadDatPicker = (RadDatePicker)SOISearchItem.FindControl("submitDateRadDatePicker");  
        RadComboBox participatingEntityRadComboBox = (RadComboBox)SOISearchItem.FindControl("participatingEntityRadComboBox");  
        RadComboBox tierRadComboBox = (RadComboBox)SOISearchItem.FindControl("tierRadComboBox");  
        RadComboBox leadOrganizationRadComboBox = (RadComboBox)SOISearchItem.FindControl("leadOrganizationRadComboBox");  
        RadComboBox requestingEntityRadComboBox = (RadComboBox)SOISearchItem.FindControl("requestingEntityRadComboBox");  
        RadComboBox locationRadComboBox = (RadComboBox)SOISearchItem.FindControl("locationRadComboBox");  
        RadComboBox renewableSourceRadComboBox = (RadComboBox)SOISearchItem.FindControl("renewableSourceRadComboBox");  
 
        if (submittalDateRadDatPicker.SelectedDate != null)  
        {  
          submittalDate = submittalDateRadDatPicker.SelectedDate;  
        }  
 
        if (participatingEntityRadComboBox.SelectedIndex > 0)  
        {  
          participatingEntity = participatingEntityRadComboBox.SelectedIndex;  
        }  
 
        if (requestingEntityRadComboBox.SelectedIndex > 0)  
        {  
          requestingEntity = requestingEntityRadComboBox.SelectedIndex;  
        }  
 
        if (locationRadComboBox.SelectedIndex > 0)  
        {  
          location = locationRadComboBox.SelectedIndex;  
        }  
 
        if (leadOrganizationRadComboBox.SelectedIndex > 0)  
        {  
          leadOrganization = leadOrganizationRadComboBox.SelectedIndex;  
        }  
 
        if (tierRadComboBox.SelectedIndex > 0)  
        {  
          tier = tierRadComboBox.SelectedIndex;  
        }  
 
        if (renewableSourceRadComboBox.SelectedIndex > 0)  
        {  
          renewableSource = renewableSourceRadComboBox.SelectedIndex;  
        }  
          
        List<TipData.BO.SOI> all = TipData.BO.SOI.GetAll(requestingEntity, participatingEntity, location, null,       
                                                          renewableSource, tier, leadOrganization, submittalDate);  
        soiRadGrid.DataSource = all;  
        soiRadGrid.DataBind();  // error is here  
          
      } 

Here is my class code:
       public static List<SOI> GetAll(int? requestingEntityID, int? participatingEntityID, int? locationID,   
                                            int? projectID, int? renewableSourceID, int? tierID, int? leadOrgID,   
                                            DateTime? submittalDate)  
        {  
            IQueryable bindings = GetAllBindings(requestingEntityID, participatingEntityID, locationID,  
                                                projectID, renewableSourceID, tierID, leadOrgID,  
                                                submittalDate);  
            return GetAll(bindings);  
        }  
 
        public static IQueryable GetAllBindings()  
        {  
            return GetAllBindings(nullnullnullnullnullnullnullnull);  
        }  
 
        public static IQueryable GetAllBindings(int? requestingEntityID, int? participatingEntityID, int? locationID,  
                                            int? projectID, int? renewableSourceID, int? tierID, int? leadOrgID,  
                                            DateTime? submittalDate)  
        {  
            TipData.DataModel.AllDataModels allModels = new TipData.DataModel.AllDataModels();  
            //note: for the cross reference tables, we are essentially doing a join   
            //      between the SOI table and the cross reference table  
            var all = from a in allModels.SOI  
                      where (a.Entity.ID.Equals(requestingEntityID.Value) || !requestingEntityID.HasValue) &&  
                            (a.Tier.ID.Equals(tierID.Value) || !tierID.HasValue) &&  
                            (a.Org.ID.Equals(leadOrgID.Value) || !leadOrgID.HasValue) &&  
                            (a.SubmittalDate >= submittalDate.Value || !submittalDate.HasValue) &&  
                            (a.ID == allModels.SOIEntity.FirstOrDefault(p2 => p2.Entity.ID.Equals(participatingEntityID.Value) && p2.SOI.ID.Equals(a.ID)).SOI.ID || !participatingEntityID.HasValue) &&  
                            (a.ID == allModels.SOILocation.FirstOrDefault(p2 => p2.Location.ID.Equals(locationID.Value) && p2.SOI.ID.Equals(a.ID)).SOI.ID || !locationID.HasValue) &&  
                            (a.ID == allModels.SOIProject.FirstOrDefault(p2 => p2.Project.ID.Equals(projectID.Value) && p2.SOI.ID.Equals(a.ID)).SOI.ID || !projectID.HasValue) &&  
                            (a.ID == allModels.SOIRenewableSource.FirstOrDefault(p2 => p2.RenewableSource.ID.Equals(renewableSourceID.Value) && p2.SOI.ID.Equals(a.ID)).SOI.ID || !renewableSourceID.HasValue)  
                      select new 
                      {  
                          ID = a.ID,  
                          Tier = a.Tier.Value,  
                          RequestingEntity = a.Entity.Name,  
                          Org = a.Org.Name,  
                          SubmittalDate = a.SubmittalDate,  
                          ContactInfo = a.ContactInfo,  
                          Unredacted = a.UnredactedFOIA,  
                          Redacted = a.RedactedFOIA  
                      };  
            return all;  
        } 

Here is my HTML:
<div> 
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" style="position:absolute; top:130px;left:50px;">  
      <telerik:RadPanelBar runat="server" ID="RadPanelBar1" Skin="WebBlue"   
        ExpandMode="SingleExpandedItem" Width="700px">  
        <Items> 
          <telerik:RadPanelItem Text="SOI Search" runat="server" Font-Bold="true" Selected="true" Expanded="true" > 
            <Items> 
              <telerik:RadPanelItem Value="SOISearch" runat="server" > 
                <ItemTemplate> 
                  <div style="background-color:#ECE5B6;">  
                    <table> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom">  
                          <asp:Label ID="Label1" runat="server" Text="Submit Date:" style="width: 94px; vertical-align:bottom;"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom">  
                          <asp:Label ID="Label3" runat="server" Text="Location:" style="width: 94px;"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadDatePicker ID="submitDateRadDatePicker" runat="server" Skin="WebBlue" Calendar-Skin="WebBlue">  
                            <Calendar ID="Calendar2" runat="server" ShowRowHeaders="false"></Calendar> 
                          </telerik:RadDatePicker> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadComboBox ID="locationRadComboBox" runat="server" ZIndex="99999" Text="Any" Width="250px" Height="150px">  
                          </telerik:RadComboBox> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom">  
                          <asp:Label ID="Label13" runat="server" Text="Participating Entity:" style="width: 131px;"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom">  
                          <asp:Label ID="Label8" runat="server" Text="Lead Organization:"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadComboBox ID="participatingEntityRadComboBox" runat="server" ZIndex="99999" 
                            Width="350px" Height="150" Text="Any">  
                          </telerik:RadComboBox> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadComboBox ID="leadOrganizationRadComboBox" runat="server" ZIndex="99999"   
                            EnableViewState="True" Text="Any" 
                            Width="250px" Height="150"></telerik:RadComboBox> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom">  
                          <asp:Label ID="Label15" runat="server" Text="Requesting Entity:" style="width: 133px;"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom">  
                          <asp:Label ID="Label12" runat="server" Text="Tier:" style="width: 123px;"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadComboBox ID="requestingEntityRadComboBox" runat="server" ZIndex="99999" Text="Any" 
                            Width="350px" Height="150">  
                          </telerik:RadComboBox> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadComboBox ID="tierRadComboBox" runat="server" ZIndex="99999" EnableViewState="True" Text="Any" 
                            Width="250px" Height="150">  
                          </telerik:RadComboBox> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td></td>  
                        <td style="width:25px"></td> 
                        <td style="vertical-align:bottom;">  
                          <asp:Label ID="Label6" runat="server" Text="Renewable Source:"></asp:Label> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td> 
                          <asp:Button ID="SOISearchButton" runat="server" Text="Search"   
                            onclick="SOISearchButton_Click" SkinID="smallGreenButton" Width="50px" /> 
                        </td> 
                        <td style="width:25px"></td> 
                        <td style="vertical-align:top">  
                          <telerik:RadComboBox ID="renewableSourceRadComboBox" runat="server" ZIndex="99999" Text="Any" Width="250px" Height="150px">  
                          </telerik:RadComboBox> 
                        </td> 
                        <td style="width:25px"></td> 
                      </tr> 
                      <tr style="height:27px;">  
                        <td style="width:25px"></td> 
                        <td style="width:25px"></td> 
                        <td style="width:25px"></td> 
                        <td style="width:25px"></td> 
                        <td style="width:25px"></td> 
                      </tr> 
                    </table> 
                      
                   <telerik:RadGrid ID="SoiRadGrid" runat="server"   
                                    AllowSorting="false"   
                                    GridLines="None"   
                                    Skin="WebBlue" 
                                    AllowFilteringByColumn="false"   
                                    AllowPaging="False"   
                                    ShowGroupPanel="True"  OnItemCommand="SoiRadGrid_ItemCommand">  
                      <PagerStyle EnableSEOPaging="true" Mode="NextPrevAndNumeric" /> 
                      <ClientSettings Resizing-AllowColumnResize="true" AllowKeyboardNavigation="false"></ClientSettings> 
                      <MasterTableView AutoGenerateColumns="False" PageSize="5" DataKeyNames="ID">  
                        <RowIndicatorColumn> 
                          <HeaderStyle Width="20px" /> 
                        </RowIndicatorColumn> 
                        <ExpandCollapseColumn> 
                          <HeaderStyle Width="20px" /> 
                        </ExpandCollapseColumn> 
 
                        <Columns> 
                            <%--<telerik:GridTemplateColumn UniqueName="linkButtonTest">  
                              <ItemTemplate> 
                                <asp:LinkButton Text="Select" runat="server" ID="linkButton1" OnClick="linkButton1_Click"></asp:LinkButton> 
                              </ItemTemplate> 
                            </telerik:GridTemplateColumn>--%> 
                            <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Redirect" Text="Select" UniqueName="ButtonColumn">  
                              <HeaderStyle Width="50px" /> 
                            </telerik:GridButtonColumn> 
                            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" Visible="false"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Tier" HeaderText="Tier"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Org" HeaderText="Org"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="ProjectGroup" HeaderText="Project Group"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="RequestingEntity" HeaderText="Requesting Entity"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="SubmittalDate" HeaderText="Submittal Date"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="ContactInfo" HeaderText="Contact Info"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Unredacted" HeaderText="Unredacted"></telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn DataField="Redacted" HeaderText="Redacted"></telerik:GridBoundColumn> 
                        </Columns> 
                        
                      </MasterTableView> 
                      <ClientSettings AllowDragToGroup="True">  
                        <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
                      </ClientSettings> 
                    </telerik:RadGrid>   
                  </div> 
                </ItemTemplate> 
              </telerik:RadPanelItem> 
            </Items> 
          </telerik:RadPanelItem> 
.... 

I added "ID" to my GetAllBindings method, but I still got the error message. 

wen




0
Princy
Top achievements
Rank 2
answered on 24 Feb 2010, 07:25 AM
Hi,

I have checked your code but am not sure where you are trying to access the DataKeyValue. When you bind the source to the grid on a search button click, please make sure that it contains the ID field as it has been set as  the DataKeyName.This might be the cause for the error.

Thanks,
Princy
0
Wendy Hunt
Top achievements
Rank 2
answered on 25 Feb 2010, 02:13 PM
I changed to the GetAllBindings() instead of the GetAll() from within the SoiGridSearch() and it worked.  You were right... the ID needed to be included in the Binding.  Thanks!

wen
0
M
Top achievements
Rank 1
answered on 07 Feb 2011, 04:00 PM
I'm trying to Display the selected record in a asp.formview on the same page as the RadGrid.
When the Page URL has a QueryString value (http://10.0.0.110/Listing1.aspx?id=781) the formview displays the record.
I tried the following:
<telerik:RadGrid ID="RadGrid1" runat="server" 
                    AllowFilteringByColumn="True" 
                    AllowPaging="True" 
                    AllowSorting="True"
                    DataSourceID="SqlDataSource1" 
                    GridLines="None" 
                    Height="700px" 
                    PageSize="20">
                    <ClientSettings AllowColumnsReorder="True">
                        <Selecting AllowRowSelect="True" />
                        <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                    </ClientSettings>
                    <MasterTableView datasourceid="SqlDataSource1" DataKeyNames="id">
                        <Columns>
                            <telerik:GridButtonColumn CommandName="Redirect" Text="Select" ButtonType="PushButton" UniqueName="ButtonColumn">
                                <HeaderStyle Width="70px" />
                            </telerik:GridButtonColumn
                        </Columns>
                        <RowIndicatorColumn>
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn>
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </ExpandCollapseColumn>
                        <NoRecordsTemplate>
                            Sorry No Records Found, Modify your search and try again.
                        </NoRecordsTemplate>
                    </MasterTableView>
                    <PagerStyle Position="TopAndBottom" />
                </telerik:RadGrid>
  
CodeBehind:
  
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Redirect")
        {
            GridDataItem item = (GridDataItem)e.Item;
            string value = item.GetDataKeyValue("id").ToString(); // Get the value in clicked row  
            // Save the required  value in session  
            string url = "listing1.aspx?id=" + value;
            Response.Redirect(url);
        }
    

But the page does not redirect.
I'm using 2009 Q3

What am I missing?
Tags
Grid
Asked by
Wendy Hunt
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Wendy Hunt
Top achievements
Rank 2
Princy
Top achievements
Rank 2
M
Top achievements
Rank 1
Share this question
or