i followed the link
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/automatic-datasource-operations to created RadGrid control. i would like to add hyperlink to all the columns so that it will open up a page or a small popup with the row.
I have managed the popup with RadWindowManager. Now the only thing left out is adding hyperlink to the cells of a databound Grid.
the below code does not work for me please help
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.Attributes["href"] = "javascript:void(0);";
link.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
//link.NavigateUrl = "Page.aspx";//for navigating to new page
//link.CssClass = "class1";//for showing underline in hyperlink
link.Text = item.GetDataKeyValue("ID").ToString();
item["ID"].Controls.Add(link);
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.Attributes["href"] = "javascript:void(0);";
link.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
item["ID"].Controls.Add(link);
}
}
please see the code
Aspx of Default
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head id="Head1" runat="server">
<title>Telerik ASP.NET Example</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server">
</telerik:RadStyleSheetManager>
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<script type="text/javascript">
function ShowEditForm(id, rowIndex) {
var grid = $find("<%= RadGrid1.ClientID %>");
var rowControl = grid.get_masterTableView().get_dataItems()[rowIndex].get_element();
grid.get_masterTableView().selectItem(rowControl, true);
window.radopen("EditForm_csharp.aspx?ID=" + id, "UserListDialog");
return false;
}
// function ShowInsertForm() {
// window.radopen("EditForm_csharp.aspx");
// return false;
// }
function refreshGrid(arg) {
if (!arg) {
$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Rebind");
}
else {
$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("RebindAndNavigate");
}
}
function RowDblClick(sender, eventArgs) {
window.radopen("EditForm_csharp.aspx?EmployeeID=" + eventArgs.getDataKeyValue("EmployeeID"), "UserListDialog");
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1"LoadingPanelID="gridLoadingPanel"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1"LoadingPanelID="gridLoadingPanel"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
<telerik:RadFormDecorator runat="server" DecorationZoneID="demo" EnableRoundedCorners="false"DecoratedControls="All" />
<div id="demo" class="demo-container no-bg">
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource2"
AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
AllowAutomaticDeletes="True" OnItemCreated="RadGrid1_ItemCreated"
OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender"
OnInsertCommand="RadGrid1_InsertCommand" GroupPanelPosition="Top"
ResolvedRenderMode="Classic"
EnableViewState="False" ShowGroupPanel="True" AllowMultiRowEdit="True"
AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True">
<PagerStyle Mode="NextPrevAndNumeric" />
<ClientSettings AllowDragToGroup="True">
<Selecting AllowRowSelect="True" />
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
<MasterTableView DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" CommandItemDisplay="Top" DataKeyNames="ID" >
<Columns>
<telerik:GridBoundColumn DataField="Self_development"
HeaderText="Self_development" SortExpression="Self_development"
UniqueName="Self_development"
FilterControlAltText="Filter Self_development column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Technical_Development"
HeaderText="Technical_Development" SortExpression="Technical_Development"
UniqueName="Technical_Development"
FilterControlAltText="Filter Technical_Development column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Project_Management"
HeaderText="Project_Management" SortExpression="Project_Management"
UniqueName="Project_Management"
FilterControlAltText="Filter Project_Management column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Safety"
HeaderText="Safety" SortExpression="Safety"
UniqueName="Safety"
FilterControlAltText="Filter Safety column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Client_Relationship"
HeaderText="Client_Relationship" SortExpression="Client_Relationship"
UniqueName="Client_Relationship" FilterControlAltText="Filter Client_Relationship column"
>
</telerik:GridBoundColumn>
<%--<telerik:GridHyperLinkColumn DataTextFormatString="Search Google for '{0}'"DataNavigateUrlFields="Client_Relationship"
UniqueName="Client_Relationship"DataNavigateUrlFormatString="http://www.google.com/search?hl=en&;q={0}&btnG=Google+Search"
DataTextField="Client_Relationship">
</telerik:GridHyperLinkColumn>--%>
<telerik:GridBoundColumn DataField="Leadership_Development"
FilterControlAltText="Filter Leadership_Development column"
HeaderText="Leadership_Development"
SortExpression="Leadership_Development"UniqueName="Leadership_Development"></telerik:GridBoundColumn><telerik:GridBoundColumn
DataField="ID" DataType="System.Int32" FilterControlAltText="Filter ID column"
HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="ID"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="TemplateEditColumn">
<ItemTemplate>
<asp:HyperLink ID="EditLink" runat="server" Text="Edit"></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<%-- <CommandItemTemplate>
<a href="#" onclick="return ShowInsertForm();">Add New Record</a>
</CommandItemTemplate>--%>
<EditFormSettings>
<EditColumn ButtonType="ImageButton" />
<EditColumn ButtonType="ImageButton"></EditColumn>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
</div>
<%--<asp:EntityDataSource ID="EntityDataSourceCustomers" runat="server"ConnectionString="name=NorthwindReadWriteEntities"
DefaultContainerName="NorthwindReadWriteEntities" EntitySetName="Customers" OrderBy="it.[ContactName]"
EntityTypeFilter="Customer" EnableDelete="True" EnableFlattening="False" EnableInsert="True"EnableUpdate="True">
</asp:EntityDataSource>--%>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=LMSDBEntities3" DefaultContainerName="LMSDBEntities3"
EntitySetName="Course_mainPage"
OrderBy="it.[Self_development]"
EnableDelete="True" EnableInsert="True" EnableUpdate="True" >
</asp:EntityDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LMSDBConnectionString %>"
SelectCommand="SELECT [Self_development] ,[Technical_Development] ,[Project_Management] ,[Safety] ,[Client_Relationship] ,[Leadership_Development] ,[ID] FROM [LMSDB].[dbo].[Course_mainPage]"
DeleteCommand="DELETE FROM Course_mainPage WHERE (ID = @ID)"
InsertCommand="INSERT INTO Course_mainPage(Self_development, Technical_Development, Project_Management, Safety, Client_Relationship, Leadership_Development) VALUES (@Self_development,@Technical_Development,@Project_Management,@Safety,@Client_Relationship,@Leadership_Development)"
UpdateCommand="UPDATE Course_mainPage SET Self_development = @Self_development, Technical_Development = @Technical_Development, Project_Management = @Project_Management, Safety = @Safety, Client_Relationship = @Client_Relationship, Leadership_Development = @Leadership_Development WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
<%--
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LMSDBConnectionString %>"
SelectCommand="SELECT [Self_development] ,[Technical_Development] ,[Project_Management] ,[Safety] ,[Client_Relationship] ,[Leadership_Development] ,[ID] FROM [LMSDB].[dbo].[Course_mainPage]"
DeleteCommand="DELETE FROM Course_mainPage WHERE (ID = @ID)"
InsertCommand="INSERT INTO Course_mainPage(Self_development, Technical_Development, Project_Management, Safety, Client_Relationship, Leadership_Development) VALUES (@Self_development,@Technical_Development,@Project_Management,@Safety,@Client_Relationship,@Leadership_Development)"
UpdateCommand="UPDATE Course_mainPage SET Self_development =@Self_development, Technical_Development =@Technical_Development, Project_Management =@Project_Management, Safety =@Safety, Client_Relationship =@Client_Relationship, Leadership_Development =@Leadership_Development where [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="@Self_development" Type="String" />
<asp:Parameter Name="@Technical_Development" Type="String" />
<asp:Parameter Name="@Project_Management" Type="String" />
<asp:Parameter Name="@Safety" Type="String" />
<asp:Parameter Name="@Client_Relationship" Type="String" />
<asp:Parameter Name="@Leadership_Development" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="@Self_development" Type="String" />
<asp:Parameter Name="@Technical_Development" Type="String" />
<asp:Parameter Name="@Project_Management" Type="String" />
<asp:Parameter Name="@Safety" Type="String" />
<asp:Parameter Name="@Client_Relationship" Type="String" />
<asp:Parameter Name="@Leadership_Development" Type="String" />
<asp:Parameter Name="@ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
--%>
<%-- Select="it.[Self_development], it.[Technical_Development], it.[Project_Management], it.[Client_Relationship], it.[Safety], it.[Leadership_Development], it.[ID]" --%>
</form>
</body>
</html>
C# of default
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
//{
// if (e.Item is GridEditableItem && e.Item.IsInEditMode)
// {
// if (!(e.Item is GridEditFormInsertItem))
// {
// GridEditableItem item = e.Item as GridEditableItem;
// GridEditManager manager = item.EditManager;
// GridTextBoxColumnEditor editor = manager.GetColumnEditor("ID") as GridTextBoxColumnEditor;
// editor.TextBoxControl.Enabled = false;
// }
// }
//}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
//if (e.Item is GridDataItem)
//{
// HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
//editLink.Attributes["href"] = "javascript:void(0);";
//editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
//}
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.Attributes["href"] = "javascript:void(0);";
link.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
//link.NavigateUrl = "Page.aspx";//for navigating to new page
//link.CssClass = "class1";//for showing underline in hyperlink
link.Text = item.GetDataKeyValue("ID").ToString();
item["ID"].Controls.Add(link);
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.NavigateUrl = "Page.aspx";//for navigating to new page
link.CssClass = "class1";//for showing underline in hyperlink
link.Text = item.GetDataKeyValue("ID").ToString();
item["ID"].Controls.Add(link);
}
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
if (e.Argument == "Rebind")
{
RadGrid1.MasterTableView.SortExpressions.Clear();
RadGrid1.MasterTableView.GroupByExpressions.Clear();
RadGrid1.Rebind();
}
else if (e.Argument == "RebindAndNavigate")
{
RadGrid1.MasterTableView.SortExpressions.Clear();
RadGrid1.MasterTableView.GroupByExpressions.Clear();
RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1;
RadGrid1.Rebind();
}
}
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
SetMessage("Record cannot be inserted. Reason: " + e.Exception.Message);
}
else
{
SetMessage("New Record is inserted!");
}
}
private void DisplayMessage(string text)
{
RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
}
private void SetMessage(string message)
{
gridMessage = message;
}
private string gridMessage = null;
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(gridMessage))
{
DisplayMessage(gridMessage);
}
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
//here editedItem.SavedOldValues will be the dictionary which holds the
//predefined values
//Prepare new dictionary object
Hashtable newValues = new Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
//the newValues instance is the new collection of key -> value pairs
//with the updated ny the user data
}
}
}
aspx of Editform_csharp.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Editform_csharp.aspx.cs" Inherits="Editform_csharp" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!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 id="Head1" runat="server">
<title>Edit dialog</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
function CloseAndRebind(args) {
GetRadWindow().BrowserWindow.refreshGrid(args);
GetRadWindow().close();
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)
return oWindow;
}
function CancelEdit() {
GetRadWindow().close();
}
</script>
<asp:ScriptManager ID="ScriptManager2" runat="server" />
<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" Skin="Vista" DecoratedControls="All"/>
<br />
<br />
<asp:DetailsView ID="DetailsView1" DataKeyNames="ID" runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource2" Height="50px" Width="125px"OnItemCommand="DetailsView1_ItemCommand"
BorderWidth="0px" CellPadding="0" CellSpacing="7" GridLines="None"
OnItemUpdating="DetailsView1_ItemUpdating"
onpageindexchanging="DetailsView1_PageIndexChanging" AllowPaging="True"
EnableModelValidation="True">
<Fields>
<asp:BoundField DataField="Self_development" HeaderText="Self_development"SortExpression="Self_development" />
<asp:BoundField DataField="Technical_Development" HeaderText="Technical_Development"SortExpression="Technical_Development" />
<asp:BoundField DataField="Project_Management" HeaderText="Project_Management"SortExpression="Project_Management" />
<asp:BoundField DataField="Safety" HeaderText="Safety" SortExpression="Safety"></asp:BoundField>
<asp:BoundField DataField="Client_Relationship"
HeaderText="Client_Relationship" SortExpression="Client_Relationship" />
<asp:BoundField DataField="Leadership_Development"
HeaderText="Leadership_Development" SortExpression="Leadership_Development" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"
InsertVisible="False" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LMSDBConnectionString %>"
SelectCommand="SELECT [Self_development] ,[Technical_Development] ,[Project_Management] ,[Safety] ,[Client_Relationship] ,[Leadership_Development] ,[ID] FROM [LMSDB].[dbo].[Course_mainPage]"
DeleteCommand="DELETE FROM Course_mainPage WHERE (ID = @ID)"
InsertCommand="INSERT INTO Course_mainPage(Self_development, Technical_Development, Project_Management, Safety, Client_Relationship, Leadership_Development) VALUES (@Self_development,@Technical_Development,@Project_Management,@Safety,@Client_Relationship,@Leadership_Development)"
UpdateCommand="UPDATE Course_mainPage SET Self_development = @Self_development, Technical_Development = @Technical_Development, Project_Management = @Project_Management, Safety = @Safety, Client_Relationship = @Client_Relationship, Leadership_Development = @Leadership_Development WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
C# of Editform_csharp.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Editform_csharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Request.QueryString["ID"] == null)
{
DetailsView1.DefaultMode = DetailsViewMode.Insert;
}
else
{
DetailsView1.DefaultMode = DetailsViewMode.Edit;
}
this.Page.Title = "ID";
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", true);
}
else if (e.CommandName == "Insert")
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey","CloseAndRebind('navigateToInserted');", true);
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CancelEdit();", true);
}
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
//logic to truncate long string to prevent SQL error
for (int i = 1; i < 4; i++)
{
string val = e.NewValues[i - 1].ToString();
int maxLength = i * 10;
if (val.Length > maxLength) e.NewValues[i - 1] = val.Substring(0, maxLength);
}
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
}
}
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/automatic-datasource-operations to created RadGrid control. i would like to add hyperlink to all the columns so that it will open up a page or a small popup with the row.
I have managed the popup with RadWindowManager. Now the only thing left out is adding hyperlink to the cells of a databound Grid.
the below code does not work for me please help
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.Attributes["href"] = "javascript:void(0);";
link.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
//link.NavigateUrl = "Page.aspx";//for navigating to new page
//link.CssClass = "class1";//for showing underline in hyperlink
link.Text = item.GetDataKeyValue("ID").ToString();
item["ID"].Controls.Add(link);
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.Attributes["href"] = "javascript:void(0);";
link.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
item["ID"].Controls.Add(link);
}
}
please see the code
Aspx of Default
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head id="Head1" runat="server">
<title>Telerik ASP.NET Example</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server">
</telerik:RadStyleSheetManager>
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<script type="text/javascript">
function ShowEditForm(id, rowIndex) {
var grid = $find("<%= RadGrid1.ClientID %>");
var rowControl = grid.get_masterTableView().get_dataItems()[rowIndex].get_element();
grid.get_masterTableView().selectItem(rowControl, true);
window.radopen("EditForm_csharp.aspx?ID=" + id, "UserListDialog");
return false;
}
// function ShowInsertForm() {
// window.radopen("EditForm_csharp.aspx");
// return false;
// }
function refreshGrid(arg) {
if (!arg) {
$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Rebind");
}
else {
$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("RebindAndNavigate");
}
}
function RowDblClick(sender, eventArgs) {
window.radopen("EditForm_csharp.aspx?EmployeeID=" + eventArgs.getDataKeyValue("EmployeeID"), "UserListDialog");
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1"LoadingPanelID="gridLoadingPanel"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1"LoadingPanelID="gridLoadingPanel"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
<telerik:RadFormDecorator runat="server" DecorationZoneID="demo" EnableRoundedCorners="false"DecoratedControls="All" />
<div id="demo" class="demo-container no-bg">
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource2"
AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
AllowAutomaticDeletes="True" OnItemCreated="RadGrid1_ItemCreated"
OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender"
OnInsertCommand="RadGrid1_InsertCommand" GroupPanelPosition="Top"
ResolvedRenderMode="Classic"
EnableViewState="False" ShowGroupPanel="True" AllowMultiRowEdit="True"
AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True">
<PagerStyle Mode="NextPrevAndNumeric" />
<ClientSettings AllowDragToGroup="True">
<Selecting AllowRowSelect="True" />
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
<MasterTableView DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" CommandItemDisplay="Top" DataKeyNames="ID" >
<Columns>
<telerik:GridBoundColumn DataField="Self_development"
HeaderText="Self_development" SortExpression="Self_development"
UniqueName="Self_development"
FilterControlAltText="Filter Self_development column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Technical_Development"
HeaderText="Technical_Development" SortExpression="Technical_Development"
UniqueName="Technical_Development"
FilterControlAltText="Filter Technical_Development column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Project_Management"
HeaderText="Project_Management" SortExpression="Project_Management"
UniqueName="Project_Management"
FilterControlAltText="Filter Project_Management column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Safety"
HeaderText="Safety" SortExpression="Safety"
UniqueName="Safety"
FilterControlAltText="Filter Safety column" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Client_Relationship"
HeaderText="Client_Relationship" SortExpression="Client_Relationship"
UniqueName="Client_Relationship" FilterControlAltText="Filter Client_Relationship column"
>
</telerik:GridBoundColumn>
<%--<telerik:GridHyperLinkColumn DataTextFormatString="Search Google for '{0}'"DataNavigateUrlFields="Client_Relationship"
UniqueName="Client_Relationship"DataNavigateUrlFormatString="http://www.google.com/search?hl=en&;q={0}&btnG=Google+Search"
DataTextField="Client_Relationship">
</telerik:GridHyperLinkColumn>--%>
<telerik:GridBoundColumn DataField="Leadership_Development"
FilterControlAltText="Filter Leadership_Development column"
HeaderText="Leadership_Development"
SortExpression="Leadership_Development"UniqueName="Leadership_Development"></telerik:GridBoundColumn><telerik:GridBoundColumn
DataField="ID" DataType="System.Int32" FilterControlAltText="Filter ID column"
HeaderText="ID" ReadOnly="True" SortExpression="ID" UniqueName="ID"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="TemplateEditColumn">
<ItemTemplate>
<asp:HyperLink ID="EditLink" runat="server" Text="Edit"></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<%-- <CommandItemTemplate>
<a href="#" onclick="return ShowInsertForm();">Add New Record</a>
</CommandItemTemplate>--%>
<EditFormSettings>
<EditColumn ButtonType="ImageButton" />
<EditColumn ButtonType="ImageButton"></EditColumn>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
</div>
<%--<asp:EntityDataSource ID="EntityDataSourceCustomers" runat="server"ConnectionString="name=NorthwindReadWriteEntities"
DefaultContainerName="NorthwindReadWriteEntities" EntitySetName="Customers" OrderBy="it.[ContactName]"
EntityTypeFilter="Customer" EnableDelete="True" EnableFlattening="False" EnableInsert="True"EnableUpdate="True">
</asp:EntityDataSource>--%>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=LMSDBEntities3" DefaultContainerName="LMSDBEntities3"
EntitySetName="Course_mainPage"
OrderBy="it.[Self_development]"
EnableDelete="True" EnableInsert="True" EnableUpdate="True" >
</asp:EntityDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LMSDBConnectionString %>"
SelectCommand="SELECT [Self_development] ,[Technical_Development] ,[Project_Management] ,[Safety] ,[Client_Relationship] ,[Leadership_Development] ,[ID] FROM [LMSDB].[dbo].[Course_mainPage]"
DeleteCommand="DELETE FROM Course_mainPage WHERE (ID = @ID)"
InsertCommand="INSERT INTO Course_mainPage(Self_development, Technical_Development, Project_Management, Safety, Client_Relationship, Leadership_Development) VALUES (@Self_development,@Technical_Development,@Project_Management,@Safety,@Client_Relationship,@Leadership_Development)"
UpdateCommand="UPDATE Course_mainPage SET Self_development = @Self_development, Technical_Development = @Technical_Development, Project_Management = @Project_Management, Safety = @Safety, Client_Relationship = @Client_Relationship, Leadership_Development = @Leadership_Development WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
<%--
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LMSDBConnectionString %>"
SelectCommand="SELECT [Self_development] ,[Technical_Development] ,[Project_Management] ,[Safety] ,[Client_Relationship] ,[Leadership_Development] ,[ID] FROM [LMSDB].[dbo].[Course_mainPage]"
DeleteCommand="DELETE FROM Course_mainPage WHERE (ID = @ID)"
InsertCommand="INSERT INTO Course_mainPage(Self_development, Technical_Development, Project_Management, Safety, Client_Relationship, Leadership_Development) VALUES (@Self_development,@Technical_Development,@Project_Management,@Safety,@Client_Relationship,@Leadership_Development)"
UpdateCommand="UPDATE Course_mainPage SET Self_development =@Self_development, Technical_Development =@Technical_Development, Project_Management =@Project_Management, Safety =@Safety, Client_Relationship =@Client_Relationship, Leadership_Development =@Leadership_Development where [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="@Self_development" Type="String" />
<asp:Parameter Name="@Technical_Development" Type="String" />
<asp:Parameter Name="@Project_Management" Type="String" />
<asp:Parameter Name="@Safety" Type="String" />
<asp:Parameter Name="@Client_Relationship" Type="String" />
<asp:Parameter Name="@Leadership_Development" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="@Self_development" Type="String" />
<asp:Parameter Name="@Technical_Development" Type="String" />
<asp:Parameter Name="@Project_Management" Type="String" />
<asp:Parameter Name="@Safety" Type="String" />
<asp:Parameter Name="@Client_Relationship" Type="String" />
<asp:Parameter Name="@Leadership_Development" Type="String" />
<asp:Parameter Name="@ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
--%>
<%-- Select="it.[Self_development], it.[Technical_Development], it.[Project_Management], it.[Client_Relationship], it.[Safety], it.[Leadership_Development], it.[ID]" --%>
</form>
</body>
</html>
C# of default
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
//{
// if (e.Item is GridEditableItem && e.Item.IsInEditMode)
// {
// if (!(e.Item is GridEditFormInsertItem))
// {
// GridEditableItem item = e.Item as GridEditableItem;
// GridEditManager manager = item.EditManager;
// GridTextBoxColumnEditor editor = manager.GetColumnEditor("ID") as GridTextBoxColumnEditor;
// editor.TextBoxControl.Enabled = false;
// }
// }
//}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
//if (e.Item is GridDataItem)
//{
// HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
//editLink.Attributes["href"] = "javascript:void(0);";
//editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
//}
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.Attributes["href"] = "javascript:void(0);";
link.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"], e.Item.ItemIndex);
//link.NavigateUrl = "Page.aspx";//for navigating to new page
//link.CssClass = "class1";//for showing underline in hyperlink
link.Text = item.GetDataKeyValue("ID").ToString();
item["ID"].Controls.Add(link);
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
HyperLink link = new HyperLink();
link.NavigateUrl = "Page.aspx";//for navigating to new page
link.CssClass = "class1";//for showing underline in hyperlink
link.Text = item.GetDataKeyValue("ID").ToString();
item["ID"].Controls.Add(link);
}
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
if (e.Argument == "Rebind")
{
RadGrid1.MasterTableView.SortExpressions.Clear();
RadGrid1.MasterTableView.GroupByExpressions.Clear();
RadGrid1.Rebind();
}
else if (e.Argument == "RebindAndNavigate")
{
RadGrid1.MasterTableView.SortExpressions.Clear();
RadGrid1.MasterTableView.GroupByExpressions.Clear();
RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1;
RadGrid1.Rebind();
}
}
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
SetMessage("Record cannot be inserted. Reason: " + e.Exception.Message);
}
else
{
SetMessage("New Record is inserted!");
}
}
private void DisplayMessage(string text)
{
RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
}
private void SetMessage(string message)
{
gridMessage = message;
}
private string gridMessage = null;
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(gridMessage))
{
DisplayMessage(gridMessage);
}
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
if (e.Item is GridEditableItem)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
//here editedItem.SavedOldValues will be the dictionary which holds the
//predefined values
//Prepare new dictionary object
Hashtable newValues = new Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
//the newValues instance is the new collection of key -> value pairs
//with the updated ny the user data
}
}
}
aspx of Editform_csharp.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Editform_csharp.aspx.cs" Inherits="Editform_csharp" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!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 id="Head1" runat="server">
<title>Edit dialog</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
function CloseAndRebind(args) {
GetRadWindow().BrowserWindow.refreshGrid(args);
GetRadWindow().close();
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)
return oWindow;
}
function CancelEdit() {
GetRadWindow().close();
}
</script>
<asp:ScriptManager ID="ScriptManager2" runat="server" />
<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" Skin="Vista" DecoratedControls="All"/>
<br />
<br />
<asp:DetailsView ID="DetailsView1" DataKeyNames="ID" runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource2" Height="50px" Width="125px"OnItemCommand="DetailsView1_ItemCommand"
BorderWidth="0px" CellPadding="0" CellSpacing="7" GridLines="None"
OnItemUpdating="DetailsView1_ItemUpdating"
onpageindexchanging="DetailsView1_PageIndexChanging" AllowPaging="True"
EnableModelValidation="True">
<Fields>
<asp:BoundField DataField="Self_development" HeaderText="Self_development"SortExpression="Self_development" />
<asp:BoundField DataField="Technical_Development" HeaderText="Technical_Development"SortExpression="Technical_Development" />
<asp:BoundField DataField="Project_Management" HeaderText="Project_Management"SortExpression="Project_Management" />
<asp:BoundField DataField="Safety" HeaderText="Safety" SortExpression="Safety"></asp:BoundField>
<asp:BoundField DataField="Client_Relationship"
HeaderText="Client_Relationship" SortExpression="Client_Relationship" />
<asp:BoundField DataField="Leadership_Development"
HeaderText="Leadership_Development" SortExpression="Leadership_Development" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"
InsertVisible="False" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LMSDBConnectionString %>"
SelectCommand="SELECT [Self_development] ,[Technical_Development] ,[Project_Management] ,[Safety] ,[Client_Relationship] ,[Leadership_Development] ,[ID] FROM [LMSDB].[dbo].[Course_mainPage]"
DeleteCommand="DELETE FROM Course_mainPage WHERE (ID = @ID)"
InsertCommand="INSERT INTO Course_mainPage(Self_development, Technical_Development, Project_Management, Safety, Client_Relationship, Leadership_Development) VALUES (@Self_development,@Technical_Development,@Project_Management,@Safety,@Client_Relationship,@Leadership_Development)"
UpdateCommand="UPDATE Course_mainPage SET Self_development = @Self_development, Technical_Development = @Technical_Development, Project_Management = @Project_Management, Safety = @Safety, Client_Relationship = @Client_Relationship, Leadership_Development = @Leadership_Development WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Self_development" />
<asp:Parameter Name="Technical_Development" />
<asp:Parameter Name="Project_Management" />
<asp:Parameter Name="Safety" />
<asp:Parameter Name="Client_Relationship" />
<asp:Parameter Name="Leadership_Development" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
C# of Editform_csharp.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Editform_csharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Request.QueryString["ID"] == null)
{
DetailsView1.DefaultMode = DetailsViewMode.Insert;
}
else
{
DetailsView1.DefaultMode = DetailsViewMode.Edit;
}
this.Page.Title = "ID";
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", true);
}
else if (e.CommandName == "Insert")
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey","CloseAndRebind('navigateToInserted');", true);
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CancelEdit();", true);
}
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
//logic to truncate long string to prevent SQL error
for (int i = 1; i < 4; i++)
{
string val = e.NewValues[i - 1].ToString();
int maxLength = i * 10;
if (val.Length > maxLength) e.NewValues[i - 1] = val.Substring(0, maxLength);
}
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
}
}