How to get the primary key value in DeleteCommand event of a RadGrid

First of all you should set the MasterTableView.DataKeyNames property of the RadGrid to the primary key columns:
<MasterTableView DataKeyNames="Id">

<telerik:RadGrid ID="customerOverviewRadGrid" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="customersDS" EnableLinqExpressions="false" runat="server" ondeletecommand="customerOverviewRadGrid_DeleteCommand" > <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> <MasterTableView DataKeyNames="Id"> <Columns> . . . </Columns> </MasterTableView> </telerik:RadGrid>

Then in you’re DeleteCommand you can get you’re primary key value:

protected void customerOverviewRadGrid_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
int id = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
// run code to delete customer in the database . . . 
}

Exception on filtering a telerik RadGrid

I was getting an error when I filtered a telerik RadGrid:

 

Error

System.Web.HttpUnhandledException (0x80004005): Exception of type ‘System.Web.HttpUnhandledException’ was thrown. —> No property or field ‘Name’ exists in type ‘EntityBase2’ (at index 5) at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.customeroverview_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\dd255f7a\3799692b\App_Web_1qm10fjh.0.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

 

Solution

After setting the RadGrid property “EnableLinqExpression” to false, the exception was resolved and I could filter my telerik RadGrid 

EnableLinqExpressions="false"

Using the Telerik GridHyperlinkColumn

I wanted a GridHyperlinkColumn to have a static text

 

DataTextField, contains the datasource columns that can be used in the property “DataTextFormatString”

DataTextFormatString, is used to display the text of the hyperlink. Must be set, else the column would be empty!

DataNavigateUrlFields, contains the datasource columns that can be used in the property “DataNavigateUrlFormatString”

DataNavigateUrlFormatString, is used to set the href property of the link

 

The following column will show the text “Edit” for each row. The href of each link will by dynamically set. Example link would be <a href=”http://localhost/CustomerDetail.aspx?CustomerId=112”>Edit</a> 

 

<telerik:GridHyperlinkColumn
UniqueName="EditColumn"
DataTextFormatString="Edit"
DataTextField="Name"
DataNavigateUrlFields="Id"
DataNavigateUrlFormatString="~/CustomerDetail.aspx?CustomerId={0}"
meta:resourcekey="CustomerEditColumn">
</telerik:GridHyperlinkColumn>

Navigate content pages by using a top menu, a telerik radajaxpanel and response.redirect in the MasterPage

When you use a MasterPage with a menu and navigate between you’re content pages in normal ASP .NET, you will see a brief flickering, caused by the ASP .NET postback. You can prevent this flickering by ajaxifying the postback.

This can be done by using the Telerik RadAjaxPanel. In my case I used LinkButtons in the masterpage for navigating the content pages and surrounded the Linbuttons by a Telerik RadAjaxPanel. The click events of the LinkButtons use Reponse.Redirect to navigate between the content pages.

 

Site.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Page</title>
<link href="Styles/Master.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server" class="form">
<telerik:RadScriptManager ID="generalRadScriptManager" runat="server" />
<!-- One RadAjaxManager for all content pages --> 
<telerik:RadAjaxManager ID="generalRadAjaxManager" runat="server" />
<!-- One RadAjaxLoadingPanel for all content pages --> 
<telerik:RadAjaxLoadingPanel ID="generalRadAjaxLoadingPanel" runat="server" BackgroundPosition="Center" Skin="Default" />
<div class="page">
<div class="inside">
<div class="header">
<!-- All postback's in the RadAjaxPanel1 will be converted to AJAX postback's and the page will not flicker on navigation -->
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="generalRadAjaxLoadingPanel">
<div class="navigation">
<div class="navigationPrimary">
<ul> 
<li><asp:LinkButton ID="administrationLinkButton" runat="server" onclick="AdministrationLinkButton_Click">Menu1</asp:LinkButton></li>
<li><asp:LinkButton ID="zakelijkLinkButton" runat="server" onclick="AdministrationLinkButton_Click">Menu2</asp:LinkButton></li>
</ul>
</div>
<div class="navigationSeparator"></div>
<div class="navigationSecondairy">
<ul> 
<li><asp:LinkButton ID="customerLinkButton" runat="server" onclick="CustomerLinkButton_Click">SubMenu1</asp:LinkButton></li>
<li><asp:LinkButton ID="PakkettenLinkButton" runat="server" onclick="AdministrationLinkButton_Click">SubMenu2</asp:LinkButton></li>
<li></li>
</ul>
</div>
</div>
</telerik:RadAjaxPanel>
</div>
<div class="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
<div class="footerPlaceholder"></div>
</div>
<div class="footer">
<ul class="footerLeftNav">
<li>Test</li>
<li>Test</li>
</ul>
</div>
</div>
</form>
</body>
</html>

 

Site.Master.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication1

{

    public partial class SiteMaster : System.Web.UI.MasterPage

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void AdministrationLinkButton_Click(object sender, EventArgs e)

        {

            Response.Redirect("Default.aspx");

        }

        protected void CustomerLinkButton_Click(object sender, EventArgs e)

        {

            Response.Redirect("CustomerOverview.aspx");

        }

    }

}

 

Master.css

*
{
margin: 0px; /* All elements start with 0px margin */
padding: 0px;  /* All elements start with 0px padding */
}
html
{
height: 100%; /* Must be 100% for footer at the bottom of the page */
font-family: Arial, Helvetica, sans-serif;
font-size: 100%;
font-weight: normal;
color: #222;
}
body
{
height: 100%; /* Must be 100% for footer at the bottom of the page */
}
form
{
height: 100%; /* Must be 100% for footer at the bottom of the page */
}
a
{
color: #222;
text-decoration: none;
}
div.page
{
margin-right: auto; /* Center the page */
margin-left:auto; /* Center the page */
height: 100%;  /* Must be 100% for footer at the bottom of the page */
text-align: left;
width: 1000px; /* Fixed width for screen resolution 1024px optimalisation */
}
div.inside
{
border-left: 1px solid rgb(224,225,226);
border-right: 1px solid rgb(224,225,226);
min-height: 100%;  /* Must be 100% for footer at the bottom of the page, using min-height instead of height allows content to be larger then intial page size */
margin-bottom: -33px; /* Negative bottom margin is used for correct the 100% height with the height of the footer, else vertical scrollbars will be shown */
}
div.header
{
padding: 10px 0px 8px 0px;
}
div.header img
{
height: 60px;
margin-left: 20px;
width: 194px;
}
div.content
{
color: #666;
font-size: 70%;
padding: 10px 15px 15px 15px;
}
div.navigation
{
border-bottom: 2px solid rgb(255,102,0);
position: absolute;
top: 0px;
width: 998px;
}
div.navigationSeparator
{
border-top: 1px solid rgb(222,222,222);
margin-left: 279px;
}
div.navigationPrimary ul
{
height: 26px;
margin-top: 13px;
padding-left: 299px;
}
div.navigationPrimary ul li
{
background: url(../Images/TabLeft.gif) no-repeat scroll left top;
float: left; /* Makes the unoderdered list flow horizontal instead of vertical */
font-weight: bold;
font-size: 75%;
height: 26px;
line-height: 26px;
list-style-type: none; /* Don't display bullets */
margin: 0px 4px 0px 0px;
text-decoration: none;
text-transform: uppercase;
white-space: nowrap;
}
div.navigationPrimary ul li a
{
background: url(../Images/TabRight.gif) no-repeat scroll right top;
float: left;
height: 26px;
line-height: 26px;
padding: 0px 15px 0px 15px;
}
div.navigationPrimary ul li a:hover
{
color: rgb(255,102,0);
}
div.navigationSecondairy ul
{
padding-left: 310px;
}
div.navigationSecondairy ul li
{
background: url(../Images/MenuSeparator.gif) no-repeat scroll left 18px;
float: left; /* Makes the unoderdered list flow horizontal instead of vertical */
font-size: 80%;
height: 30px;
list-style-type: none; /* Don't display bullets */
margin-right: 12px;
padding-top: 12px;
padding-left: 10px;
white-space: nowrap;
}
div.navigationSecondairy ul li a
{
float: left;
}
div.navigationSecondairy ul li a:hover
{
color: rgb(255,102,0);
}
div.footerPlaceholder
{
height: 33px; /* The height should be equal to the full height of the footer (including any padding or borders you may add). Without the "footerPlaceholder" div, the content will flow beneath the footer */
}
div.footer
{
background: url(../Images/Footer.gif) #f60 repeat-x;
color: rgb(255,255,255);
font-size: 60%;
padding: 10px 46px 10px 46px;
width: 906px;
}
ul.footerLeftNav
{
margin: 0px;
padding: 0px;
page-break-inside: avoid;
list-style-position: outside;
list-style-type: none;
}
ul.footerLeftNav li
{
display: inline;
padding-right: 18px;
}

IE 6 and Telerik error: Sys.InvalidOperationException Telerik.Web.UI.Orientation has already been registered

If you get the error: Microsoft JScript runtime error: Sys.InvalidOperationException: Type Telerik.Web.UI.Orientation has already been registered. The type may be defined multiple times or the script file that defines it may have already been loaded. A possible cause is a change of settings during a partial update. in IE 6, change the <asp:ScriptManager in you’re master page to <telerik:RadScriptManager

Set initial sorting on a Telerik RadGrid column

If you want to set an initial sorting on a column on a Telerik RadGrid, use the “NeedDataSource” event and add a GridSortExpression to the MasterTableView.SortExpressions

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
// Set datasource here…

if (!IsPostBack)
{
                // Clear the current sortexpressions
this.RadGrid.MasterTableView.SortExpressions.Clear();
// Create "Date" sorting
GridSortExpression expression = new GridSortExpression();
expression.FieldName = "Date";
expression.SortOrder = GridSortOrder.Descending;
// Set initial sortexpression to the [Date] column
this.opdrachtenRadGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

            }
}

In the aspx page:

              <telerik:GridDateTimeColumn
DataField="Date"
HeaderText="Date"
SortExpression="Date"
UniqueName="Date"
FilterControlWidth="70px"
ShowFilterIcon="false"
DataType="System.DateTime"
CurrentFilterFunction="EqualTo"
PickerType="DatePicker"
AutoPostBackOnFilter="true"
HtmlEncode="false"
DataFormatString="{0:dd-MM-yyyy}"
meta:resourcekey="Date">
<ItemStyle Width="70px" />
</telerik:GridDateTimeColumn>


Or you could use the “SortExpressions” tag in the MasterTableView tag:

<telerik:RadGrid ID="RadGrid1" runat="server"> <MasterTableView> <SortExpressions> <telerik:GridSortExpression FieldName="Name" SortOrder="Ascending" /> </SortExpressions> <Columns> <telerik:GridBoundColumn AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" DataField="Name" FilterControlWidth="200px" meta:resourcekey="CustomerNameColumn" UniqueName="Name" SortExpression="Name" ShowFilterIcon="true"> <ItemStyle Width="200px" /> </telerik:GridBoundColumn>

……

Set initial filter on GridDateTimeColumn in a Telerik RadGrid

If you want to set an initial filter on a Telerik RadGrid, use the “NeedDataSource” event and set the MasterTableView.FilterExpression and use the function MasterTableView.GetColumnSafe

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
// Set datasource here…

if (!IsPostBack)
{
// Set initial filter on [RadGrid]
this.RadGrid.MasterTableView.FilterExpression = string.Format("([Date] = '{0}')", DateTime.Today);
// Set current filter function and value on [Date]
GridColumn column = this.RadGrid.MasterTableView.GetColumnSafe("Date");
column.CurrentFilterFunction = GridKnownFunction.EqualTo;
column.CurrentFilterValue = DateTime.Today.ToString();
}
}

In the aspx page use “EqualTo” as filter, not “Contains” and use a GridDateTimeColumn not a GridBoundColumn

              <telerik:GridDateTimeColumn
DataField="Date"
HeaderText="Date"
SortExpression="Date"
UniqueName="Date"
FilterControlWidth="70px"
ShowFilterIcon="false"
DataType="System.DateTime"
CurrentFilterFunction="EqualTo"
PickerType="DatePicker"
AutoPostBackOnFilter="true"
HtmlEncode="false"
DataFormatString="{0:dd-MM-yyyy}"
meta:resourcekey="Date">
<ItemStyle Width="70px" />
</telerik:GridDateTimeColumn>

Cannot find column Customer when sorting a Telerik RadGrid column with LLBLGen entities

When you sort a column in a Telerik RadGrid you get the error “Cannot find column Customer”, make sure the  SortExpression is the same as the DataField

                   DataField="Customer.Description"
HeaderText="Customer"
SortExpression="Customer.Description"
UniqueName="Customer" 


If SortExpression is not the same as the DataFiel, you will get the error:

Message: Cannot find column Customer

Exception: System.IndexOutOfRangeException

Targetsite: System.Data.IndexField[] ParseSortString(System.String)

Source: System.Data

StackTrace: at System.Data.DataTable.ParseSortString(String sortString)

at System.Data.DataView.CheckSort(String sort)

at System.Data.DataView.set_Sort(String value)

at Telerik.Web.UI.GridEnumerableFromDataView.PerformTransformation()

at Telerik.Web.UI.GridEnumerableFromDataView.TransformEnumerable()

at Telerik.Web.UI.GridTableView.GetEnumerator(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ArrayList dataKeysArray)

at Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource)

at Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource)

at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)

at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)

at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)

at System.Web.UI.WebControls.DataBoundControl.PerformSelect()

at Telerik.Web.UI.GridTableView.PerformSelect()

at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()

at Telerik.Web.UI.GridTableView.DataBind()

at Telerik.Web.UI.GridSortCommandEventArgs.ExecuteCommand(Object source)

at Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)

at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Data count: 0

Ajaxification of ASP .NET controls with the Telerik RadAjaxManagerProxy (Master / Content page scenario)

If you have a ASP .NET web application with a master page and a content page and you want a LinkButton to update a label with AJAX, you can use the Telerik R.A.D. controls:

Web.config


configuration > system.web > pages > controls should contain:
<add tagPrefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI" />

configuration > system.web > httpHandlers should contain:
<add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false" />

configuration > system.webServer > handlers should contain:
<add name="Telerik_Web_UI_WebResource_axd" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" preCondition="integratedMode,runtimeVersionv2.0" />

Project

Should reference Telerik.Web.UI

Master page

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Test.master.cs" Inherits="Rvl.HelperTools.Website.Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Ajaxification with Telerik</title>
<!-- Use a contentplaceholder in the head, for adding styling from within the user controls -->
<asp:ContentPlaceHolder ID="head" runat="server"/>
</head>
<body>
<form id="mainForm" runat="server">
<div>
<asp:ScriptManager ID="scriptManager" runat="server" />
<!-- One RadAjaxManager for all content pages that use this master page -->
<telerik:RadAjaxManager ID="ajaxManager" runat="server" />
<!-- The skin should be set, else the loading panel is not shown -->
<telerik:RadAjaxLoadingPanel ID="radAjaxLoadingPanel" runat="server" BackgroundPosition="Center" Skin="Default" />
<asp:ContentPlaceHolder ID="mainContent" runat="server" />
</div>
</form>
</body>
</html>



Content page (aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestContent.aspx.cs" Inherits="Rvl.HelperTools.Website.TestContent" MasterPageFile="~/Test.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<!-- Add a css class to the head of the master page -->
<style type="text/css">
.ViewPanel
{
height: 75px;
}
</style>
</asp:Content>
<asp:Content ID="MainContent" ContentPlaceHolderID="mainContent" runat="server">
<!-- Use the RadAjaxManagerProxy instead of the RadAjaxManager on content pages –>
<!-- Use the RadAjaxLoadingPanel on the master page --> <telerik:RadAjaxManagerProxy ID="ajaxManagerProxy" runat="server"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="changeLinkButton"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="viewPanel" LoadingPanelID="radAjaxLoadingPanel" /> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings> </telerik:RadAjaxManagerProxy> <asp:Panel ID="viewPanel" runat="server" CssClass="ViewPanel"> <asp:Label ID="changeLabel" runat="server" Text="Default text"></asp:Label> </asp:Panel> <asp:Panel ID="actionPanel" runat="server"> <asp:LinkButton ID="changeLinkButton" runat="server" onclick="ChangeLinkButton_Click">Change</asp:LinkButton> </asp:Panel> </asp:Content>

Content page (.cs)

using System;
using System.Threading;
namespace Rvl.HelperTools.Website
{
public partial class TestContent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ChangeLinkButton_Click(object sender, EventArgs e)
{
string textChanged = "Text changed";
string textChangedAgain = "Text changed again";
string currentText = string.Format("{0}", changeLabel.Text);
if (currentText.Equals(textChanged))
{
changeLabel.Text = textChangedAgain;
}
else
{
changeLabel.Text = textChanged;
}
}
}
}

Result

image