Hello,
my problem it's like here:
http://www.telerik.com/community/forums/aspnet-ajax/grid/accessing-old-amp-new-values-in-itemupdated-iteminserted-and-itemdeleted.aspx
How can I get the old values in all input refill, if I get a exception on ItemUpdated?
I try here the code:
protected
void
RadGrid1_ItemUpdated(
object
sender, GridUpdatedEventArgs e)
{
if
(e.Exception !=
null
)
{
e.KeepInEditMode =
true
;
e.ExceptionHandled =
true
;
flag = 1;
GridEditFormItem editedItem = (GridEditFormItem)e.Item;
Hashtable new_values =
new
Hashtable();
Hashtable old_values =
new
Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(new_values, editedItem);
old_values = (Hashtable)editedItem.SavedOldValues;
foreach
(Object key
in
new_values.Keys)
{
string
value = old_values[key].ToString();
// Here I get a error! With: Object reference not set to an instance of an object.
}
}
}
Any ideas? Im still a beginner (;
Thanks..
4 Answers, 1 is accepted
0

McMins
Top achievements
Rank 1
answered on 26 Jul 2013, 10:31 AM
I'm trying to here, my problem is the insert in gridview.
Here is my Markup:
and my codebehind:
It is already included in the data grid view that works.
But if I want to insert new item, with a textbox field with Unique Key ("friendlyurl"), and the new insert enters the same value, which column in the database is already in friendlyurl, comes error. That's all right. Then stops with the e.ExceptionHandled = true; in ItemInserted.
Problem: After Inserted with exception not zero, the page reloads and the existing values will be lost. How can I retain the values if the error occurs? What's missing here, or what is wrong here?
Here is my Markup:
<%@ Page Title="" Language="C#" Debug="true" MasterPageFile="~/edit/default.master" AutoEventWireup="true" CodeBehind="event.aspx.cs" Inherits="cms.app.v2.edit._event" %>
<%@ Register TagPrefix="ajc" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
<
asp:Content
ID
=
"Content1"
ContentPlaceHolderID
=
"head"
runat
=
"server"
></
asp:Content
>
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"cph_main"
runat
=
"server"
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
></
telerik:RadAjaxManager
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
DataSourceID
=
"SqlDataSource1"
Skin
=
"Metro"
CssClass
=
"radgrid_custom"
PageSize
=
"15"
CellSpacing
=
"0"
GridLines
=
"None"
AllowSorting
=
"True"
AllowPaging
=
"True"
AllowAutomaticInserts
=
"True"
AllowAutomaticDeletes
=
"True"
AllowAutomaticUpdates
=
"True"
AutoGenerateEditColumn
=
"true"
AutoGenerateColumns
=
"False"
OnPreRender
=
"RadGrid1_PreRender"
OnItemCommand
=
"RadGrid1_ItemCommand"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
OnItemUpdated
=
"RadGrid1_ItemUpdated"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnItemInserted
=
"RadGrid1_ItemInserted"
>
<
PagerStyle
Mode
=
"NextPrevAndNumeric"
/>
<
ClientSettings
>
<
Selecting
AllowRowSelect
=
"True"
/>
</
ClientSettings
>
<
MasterTableView
HeaderStyle-CssClass
=
"rgrid_header"
CommandItemDisplay
=
"Top"
InsertItemDisplay
=
"Top"
DataSourceID
=
"SqlDataSource1"
DataKeyNames
=
"eventId"
AutoGenerateColumns
=
"False"
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
></
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
></
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridButtonColumn
ButtonType
=
"ImageButton"
CommandName
=
"Delete"
Text
=
"Delete"
HeaderStyle-Width
=
"10px"
UniqueName
=
"column"
ConfirmText
=
"Sind Sie sicher, dass Sie diese Zeile löschen möchten?"
/>
<
telerik:GridBoundColumn
DataField
=
"eventId"
UniqueName
=
"eventId"
Display
=
"false"
ReadOnly
=
"true"
/>
<
telerik:GridBoundColumn
DataField
=
"title"
FilterControlAltText
=
"Filter title column"
HeaderText
=
"Titel"
SortExpression
=
"title"
UniqueName
=
"title"
/>
<
telerik:GridBoundColumn
DataField
=
"startdate"
FilterControlAltText
=
"Filter startdate column"
HeaderText
=
"Beginnt"
SortExpression
=
"startdate"
UniqueName
=
"startdate"
DataType
=
"System.DateTime"
/>
<
telerik:GridBoundColumn
DataField
=
"enddate"
FilterControlAltText
=
"Filter enddate column"
HeaderText
=
"Endet"
SortExpression
=
"enddate"
UniqueName
=
"enddate"
DataType
=
"System.DateTime"
/>
<
telerik:GridBoundColumn
DataField
=
"subtitle"
UniqueName
=
"subtitle"
Display
=
"false"
/>
<
telerik:GridBoundColumn
DataField
=
"friendlyurl"
UniqueName
=
"friendlyurl"
Display
=
"false"
/>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
EditColumn
UniqueName
=
"EditCommandColumn1"
FilterControlAltText
=
"Filter EditCommandColumn1 column"
></
EditColumn
>
<
FormTemplate
>
<
div
class
=
"column1"
><
p
>Titel *</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadTextBox
ID
=
"tb_title"
runat
=
"server"
Text='<%# Bind("title") %>' Width="650px" Skin="Metro" CssClass="rinput"></
telerik:RadTextBox
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator2"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="tb_title" Display="Dynamic"></
asp:RequiredFieldValidator
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
><
p
>Subtitel</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadTextBox
ID
=
"RadTextBox8"
runat
=
"server"
Text='<%# Bind("subtitle") %>' Width="650px" Skin="Metro" CssClass="rinput"></
telerik:RadTextBox
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column2_1"
><
p
>Startdate *</
p
></
div
>
<
div
class
=
"column2_3"
>
<
telerik:RadDatePicker
ID
=
"rdp_startdate"
runat
=
"server"
DbSelectedDate='<%# Bind("startdate") %>' CssClass="tbdatepicker">
<
Calendar
ID
=
"Calendar2"
runat
=
"server"
>
<
SpecialDays
>
<
telerik:RadCalendarDay
Repeatable
=
"Today"
ItemStyle-BackColor
=
"#bcb997"
></
telerik:RadCalendarDay
>
</
SpecialDays
>
</
Calendar
>
</
telerik:RadDatePicker
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="rdp_startdate" Display="Dynamic"></
asp:RequiredFieldValidator
>
</
div
>
<
div
class
=
"column2_2"
><
p
>Enddate *</
p
></
div
>
<
div
class
=
"column2_3"
>
<
telerik:RadDatePicker
ID
=
"rdp_enddate"
runat
=
"server"
DbSelectedDate='<%# Bind("enddate") %>' CssClass="tbdatepicker">
<
Calendar
ID
=
"Calendar1"
runat
=
"server"
>
<
SpecialDays
>
<
telerik:RadCalendarDay
Repeatable
=
"Today"
ItemStyle-BackColor
=
"#bcb997"
></
telerik:RadCalendarDay
>
</
SpecialDays
>
</
Calendar
>
</
telerik:RadDatePicker
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator3"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="rdp_enddate" Display="Dynamic"></
asp:RequiredFieldValidator
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
><
p
>Friendly URL *</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadTextBox
ID
=
"rtb_friendly"
runat
=
"server"
Text='<%# Bind("friendlyurl") %>' Width="650px" Skin="Metro" CssClass="rinput"></
telerik:RadTextBox
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator4"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="rtb_friendly" Display="Dynamic"></
asp:RequiredFieldValidator
>
<
asp:CustomValidator
ID
=
"customvali_friendly"
runat
=
"server"
ErrorMessage
=
"Invalid input"
ControlToValidate
=
"rtb_friendly"
></
asp:CustomValidator
>
<
asp:Label
ID
=
"lbl_friendly_valid"
runat
=
"server"
></
asp:Label
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
> </
div
>
<
div
class
=
"column2"
>
<
p
class
=
"require"
>Bitte füllen Sie alle mit einem * gekennzeichneten Felder aus.</
p
>
<
div
class
=
"editbuttons"
>
<
telerik:RadButton
ID
=
"btn_update"
runat
=
"server"
OnClick
=
"btn_update_Click"
Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' ></
telerik:RadButton
>
<
telerik:RadButton
ID
=
"btn_cancel"
runat
=
"server"
Text
=
"Cancel"
CausesValidation
=
"False"
CommandName
=
"Cancel"
></
telerik:RadButton
>
</
div
>
</
div
>
<
div
class
=
"clear"
></
div
>
</
FormTemplate
>
</
EditFormSettings
>
<
CommandItemSettings
AddNewRecordText
=
"Neuer Eintrag"
AddNewRecordImageUrl
=
"/edit/img/add.png"
RefreshImageUrl
=
"/edit/img/refresh.png"
></
CommandItemSettings
>
<
PagerStyle
PageSizeControlType
=
"RadComboBox"
></
PagerStyle
>
<
HeaderStyle
CssClass
=
"rgrid_header"
></
HeaderStyle
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
<
asp:SqlDataSource
ID
=
"SqlDataSource1"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:CMSApp_TestConnectionString %>"
ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
<
DeleteParameters
>
<
asp:Parameter
Name
=
"original_eventId"
Type
=
"Int32"
/>
<
asp:Parameter
Name
=
"original_title"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"original_startdate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"original_enddate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"original_subtitle"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"original_friendlyurl"
Type
=
"String"
/>
</
DeleteParameters
>
<
InsertParameters
>
<
asp:Parameter
Name
=
"title"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"startdate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"enddate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"subtitle"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"friendlyurl"
Type
=
"String"
/>
</
InsertParameters
>
<
UpdateParameters
>
<
asp:Parameter
Name
=
"title"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"startdate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"enddate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"subtitle"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"friendlyurl"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"original_eventId"
Type
=
"Int32"
/>
<
asp:Parameter
Name
=
"original_title"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"original_startdate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"original_enddate"
Type
=
"DateTime"
/>
<
asp:Parameter
Name
=
"original_subtitle"
Type
=
"String"
/>
<
asp:Parameter
Name
=
"original_friendlyurl"
Type
=
"String"
/>
</
UpdateParameters
>
</
asp:SqlDataSource
>
</
asp:Content
>
and my codebehind:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
namespace cms.app.v2.edit
{
public partial class _event : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(gridMessage))
{
DisplayMessage(gridMessage);
}
}
private string gridMessage = null;
private void SetMessage(string message)
{
gridMessage = message;
}
private void DisplayMessage(string text)
{
RadGrid1.Controls.Add(new LiteralControl(string.Format("<
span
style
=
'color:red'
>{0}</
span
>", text)));
}
protected override void OnPreInit(EventArgs e)
{
// ----------- Bind SQL ----------- //
base.OnPreInit(e);
(this.Page.Master as _default).MasterSv_Event_de += new EventHandler(ContentPageSv_Event_de);
(this.Page.Master as _default).MasterSv_Event_en += new EventHandler(ContentPageSv_Event_en);
}
// SQL BINDS.... for ContentPageSv_Event_de / ContentPageSv_Event_en
//string SqCommand = "...
//.
//.
//.
//.
protected void btn_update_Click(object sender, EventArgs e)
{
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
// ----------- Friendly URL Error Message ----------- //
GridEditableItem editedItem = (GridEditableItem)e.Item;
if (flag == 1)
{
Label lbl_friendly_valid = (Label)editedItem.FindControl("lbl_friendly_valid");
lbl_friendly_valid.Text = "Diese URL ist schon vorhanden!";
}
}
}
// ----------- Friendly URL Exception ----------- //
public int flag = 0;
protected void RadGrid1_ItemUpdated(object sender, GridUpdatedEventArgs e)
{
GridEditableItem item = (GridEditableItem)e.Item;
String id = item.GetDataKeyValue("eventId").ToString();
if (e.Exception != null)
{
e.KeepInEditMode = true;
e.ExceptionHandled = true;
flag = 1;
SetMessage("Event with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
}
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
//if (e.CommandName == RadGrid.UpdateCommandName)
//{
// 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
// }
//}
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
RadGrid1.Controls.Add(new LiteralControl("Saved old value for the City editor is: " + (string)Session["savedOldValue"]));
}
protected void RadGrid1_ItemInserted(object sender, GridInsertedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
flag = 1;
SetMessage("Event cannot be inserted. Reason: " + e.Exception.Message);
}
}
}
}
It is already included in the data grid view that works.
But if I want to insert new item, with a textbox field with Unique Key ("friendlyurl"), and the new insert enters the same value, which column in the database is already in friendlyurl, comes error. That's all right. Then stops with the e.ExceptionHandled = true; in ItemInserted.
Problem: After Inserted with exception not zero, the page reloads and the existing values will be lost. How can I retain the values if the error occurs? What's missing here, or what is wrong here?
0
Hi Mins,
I suppose you need to use Validators and configure them accordingly to match your datasource requirements:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx
Adding validator explicitly:
http://www.telerik.com/help/aspnet-ajax/grid-validation.html
Generally, you can use the SavedOldValues collection to get the old values or use one of the following approaches to extract the new ones:
- Use the ExtractValues method to get the new values:
http://www.telerik.com/help/aspnet-ajax/grid-updating-inplace-and-editforms.html
Here is a live example:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
- Alternatively, you can use the ColumnEditors:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editmodes/defaultcs.aspx
- Another possible approach is to access the generated controls and get the values manually one by one:
( Section Accessing controls in edit/insert mode )
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html
Hope this helps.
Regards,
Eyup
Telerik
I suppose you need to use Validators and configure them accordingly to match your datasource requirements:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx
Adding validator explicitly:
http://www.telerik.com/help/aspnet-ajax/grid-validation.html
Generally, you can use the SavedOldValues collection to get the old values or use one of the following approaches to extract the new ones:
- Use the ExtractValues method to get the new values:
http://www.telerik.com/help/aspnet-ajax/grid-updating-inplace-and-editforms.html
Here is a live example:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
- Alternatively, you can use the ColumnEditors:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editmodes/defaultcs.aspx
- Another possible approach is to access the generated controls and get the values manually one by one:
( Section Accessing controls in edit/insert mode )
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html
Hope this helps.
Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0

McMins
Top achievements
Rank 1
answered on 31 Jul 2013, 01:39 PM
Hi Eyup,
thank u for answer. I use now with ur suggestion link:
http://www.telerik.com/help/aspnet-ajax/grid-updating-inplace-and-editforms.html
But now I try with Needdatasource, bind in the grid in the content page work well. (I have a masterpage)
Only the insert a new item in the grid don't work. How I insert the new item in the sql datasource with needdatasource?
After I click the Button Update, keep the Editmode and not display only the gridview without the Editmode.. and the new row in the sqldata isn't there.
How I can do it?
My markup code here (short version without masterpage):
and codebehind:
Thanks
thank u for answer. I use now with ur suggestion link:
http://www.telerik.com/help/aspnet-ajax/grid-updating-inplace-and-editforms.html
But now I try with Needdatasource, bind in the grid in the content page work well. (I have a masterpage)
Only the insert a new item in the grid don't work. How I insert the new item in the sql datasource with needdatasource?
After I click the Button Update, keep the Editmode and not display only the gridview without the Editmode.. and the new row in the sqldata isn't there.
How I can do it?
My markup code here (short version without masterpage):
<
form
id
=
"form1"
runat
=
"server"
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
>
<
Scripts
>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.Core.js"
/>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.jQuery.js"
/>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.jQueryInclude.js"
/>
</
Scripts
>
</
telerik:RadScriptManager
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"RadGrid1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
telerik:RadGrid
ID
=
"RadGrid1"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
runat
=
"server"
AllowAutomaticInserts
=
"true"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnDeleteCommand
=
"RadGrid1_DeleteCommand"
OnPreRender
=
"RadGrid1_PreRender"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
MasterTableView
DataKeyNames
=
"eventId"
EditMode
=
"EditForms"
AutoGenerateColumns
=
"False"
CommandItemDisplay
=
"Top"
CommandItemSettings-AddNewRecordText
=
"Add New Event"
>
<
Columns
>
<
telerik:GridEditCommandColumn
UniqueName
=
"GridEditCommandColumn"
></
telerik:GridEditCommandColumn
>
<
telerik:GridButtonColumn
ButtonType
=
"ImageButton"
CommandName
=
"Delete"
Text
=
"Delete"
HeaderStyle-Width
=
"10px"
UniqueName
=
"column"
ConfirmText
=
"Sind Sie sicher, dass Sie diese Zeile löschen möchten?"
/>
<
telerik:GridBoundColumn
DataField
=
"eventId"
UniqueName
=
"eventId"
Display
=
"false"
ReadOnly
=
"true"
/>
<
telerik:GridBoundColumn
DataField
=
"title"
FilterControlAltText
=
"Filter title column"
HeaderText
=
"Titel"
SortExpression
=
"title"
UniqueName
=
"title"
/>
<
telerik:GridBoundColumn
DataField
=
"startdate"
FilterControlAltText
=
"Filter startdate column"
HeaderText
=
"Beginnt"
SortExpression
=
"startdate"
UniqueName
=
"startdate"
DataType
=
"System.DateTime"
/>
<
telerik:GridBoundColumn
DataField
=
"enddate"
FilterControlAltText
=
"Filter enddate column"
HeaderText
=
"Endet"
SortExpression
=
"enddate"
UniqueName
=
"enddate"
DataType
=
"System.DateTime"
/>
<
telerik:GridBoundColumn
DataField
=
"friendlyurl"
UniqueName
=
"friendlyurl"
Display
=
"false"
/>
<
telerik:GridCheckBoxColumn
DataField
=
"reglinkwebInApp"
UniqueName
=
"reglinkwebInApp"
Display
=
"false"
/>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
EditColumn
UniqueName
=
"EditCommandColumn1"
FilterControlAltText
=
"Filter EditCommandColumn1 column"
></
EditColumn
>
<
FormTemplate
>
<
div
class
=
"column1"
><
p
>Titel *</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadTextBox
ID
=
"tb_title"
runat
=
"server"
Text='<%# Bind("title") %>' Width="650px" Skin="Metro" CssClass="rinput"></
telerik:RadTextBox
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator2"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="tb_title" Display="Dynamic"></
asp:RequiredFieldValidator
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
><
p
>Subtitel</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadTextBox
ID
=
"RadTextBox8"
runat
=
"server"
Text='<%# Bind("subtitle") %>' Width="650px" Skin="Metro" CssClass="rinput"></
telerik:RadTextBox
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column2_1"
><
p
>Startdate *</
p
></
div
>
<
div
class
=
"column2_3"
>
<
telerik:RadDatePicker
ID
=
"rdp_startdate"
runat
=
"server"
DbSelectedDate='<%# Bind("startdate") %>' CssClass="tbdatepicker">
<
Calendar
ID
=
"Calendar2"
runat
=
"server"
>
<
SpecialDays
>
<
telerik:RadCalendarDay
Repeatable
=
"Today"
ItemStyle-BackColor
=
"#bcb997"
></
telerik:RadCalendarDay
>
</
SpecialDays
>
</
Calendar
>
</
telerik:RadDatePicker
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="rdp_startdate" Display="Dynamic"></
asp:RequiredFieldValidator
>
</
div
>
<
div
class
=
"column2_2"
><
p
>Enddate *</
p
></
div
>
<
div
class
=
"column2_3"
>
<
telerik:RadDatePicker
ID
=
"rdp_enddate"
runat
=
"server"
DbSelectedDate='<%# Bind("enddate") %>' CssClass="tbdatepicker">
<
Calendar
ID
=
"Calendar1"
runat
=
"server"
>
<
SpecialDays
>
<
telerik:RadCalendarDay
Repeatable
=
"Today"
ItemStyle-BackColor
=
"#bcb997"
></
telerik:RadCalendarDay
>
</
SpecialDays
>
</
Calendar
>
</
telerik:RadDatePicker
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator3"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="rdp_enddate" Display="Dynamic"></
asp:RequiredFieldValidator
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
><
p
>Friendly URL *</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadTextBox
ID
=
"rtb_friendly"
runat
=
"server"
Text='<%# Bind("friendlyurl") %>' Width="650px" Skin="Metro" CssClass="rinput"></
telerik:RadTextBox
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator4"
runat
=
"server"
ErrorMessage="<br />Bitte füllen Sie dieses Feld aus!<
br
/><
br
/>" CssClass="require_field" ControlToValidate="rtb_friendly" Display="Dynamic"></
asp:RequiredFieldValidator
>
<
asp:CustomValidator
ID
=
"customvali_friendly"
runat
=
"server"
ErrorMessage
=
"Invalid input"
ControlToValidate
=
"rtb_friendly"
></
asp:CustomValidator
>
<
asp:Label
ID
=
"lbl_friendly_valid"
runat
=
"server"
></
asp:Label
>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
><
p
>Reglink Web In App</
p
></
div
>
<
div
class
=
"column2"
>
<
asp:CheckBox
ID
=
"cb_reglinkinapp"
runat
=
"server"
Checked='<%# (DataBinder.Eval(Container.DataItem,"reglinkwebInApp") is DBNull ? false : Eval("reglinkwebInApp")) %>' />
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
><
p
>Inhalt</
p
></
div
>
<
div
class
=
"column2"
>
<
telerik:RadEditor
ID
=
"RadEditor1"
runat
=
"server"
Content='<%# Bind("content") %>' Skin="Metro" CssClass="reditor" Width="100%" EnableResize="false">
<
Content
></
Content
>
<
Tools
>
<
telerik:EditorToolGroup
Tag
=
"MainToolbar"
>
<
telerik:EditorTool
Name
=
"Undo"
></
telerik:EditorTool
>
<
telerik:EditorTool
Name
=
"Redo"
></
telerik:EditorTool
>
<
telerik:EditorSeparator
Visible
=
"true"
/>
<
telerik:EditorTool
Name
=
"Cut"
></
telerik:EditorTool
>
<
telerik:EditorTool
Name
=
"Copy"
></
telerik:EditorTool
>
<
telerik:EditorTool
Name
=
"Paste"
ShortCut
=
"CTRL+V"
></
telerik:EditorTool
>
<
telerik:EditorTool
Name
=
"PasteStrip"
/>
<
telerik:EditorSeparator
Visible
=
"true"
/>
<
telerik:EditorTool
Name
=
"FormatBlock"
/>
<
telerik:EditorTool
Name
=
"Bold"
ShortCut
=
"CTRL+B"
/>
<
telerik:EditorTool
Name
=
"Italic"
ShortCut
=
"CTRL+I"
/>
<
telerik:EditorTool
Name
=
"Underline"
ShortCut
=
"CTRL+U"
/>
<
telerik:EditorSeparator
Visible
=
"true"
/>
<
telerik:EditorTool
Name
=
"InsertUnorderedList"
/>
<
telerik:EditorTool
Name
=
"InsertOrderedList"
/>
<
telerik:EditorTool
Name
=
"Indent"
/>
<
telerik:EditorTool
Name
=
"Outdent"
/>
<
telerik:EditorSeparator
Visible
=
"true"
/>
<
telerik:EditorTool
Name
=
"LinkManager"
shortcut
=
"CTRL+K"
/>
<
telerik:EditorTool
Name
=
"Unlink"
shortcut
=
"CTRL+SHIFT+K"
/>
</
telerik:EditorToolGroup
>
</
Tools
>
<
Paragraphs
>
<
telerik:EditorParagraph
Title
=
"Head 1"
Tag="<h1>" />
<
telerik:EditorParagraph
Title
=
"Head 2"
Tag="<h2>" />
<
telerik:EditorParagraph
Title
=
"Head 3"
Tag="<h3>" />
</
Paragraphs
>
</
telerik:RadEditor
><
br
/>
</
div
>
<
div
class
=
"clear"
></
div
>
<
div
class
=
"column1"
> </
div
>
<
div
class
=
"column2"
>
<
p
class
=
"require"
>Bitte füllen Sie alle mit einem * gekennzeichneten Felder aus.</
p
>
<
div
class
=
"editbuttons"
>
<
telerik:RadButton
ID
=
"btn_update"
runat
=
"server"
Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' ></
telerik:RadButton
>
<
telerik:RadButton
ID
=
"btn_cancel"
runat
=
"server"
Text
=
"Cancel"
CausesValidation
=
"False"
CommandName
=
"Cancel"
></
telerik:RadButton
>
</
div
>
</
div
>
<
div
class
=
"clear"
></
div
>
</
FormTemplate
>
</
EditFormSettings
>
</
MasterTableView
>
</
telerik:RadGrid
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
/>
</
form
>
and codebehind:
public
partial
class
Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
private
DataTable GridSource
{
get
{
Object obj =
this
.ViewState[
"_gds"
];
if
(obj !=
null
)
{
return
(DataTable)obj;
}
else
{
String ConnString = ConfigurationManager.ConnectionStrings[
"App_TestConnectionString"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(ConnString);
SqlDataAdapter adapter =
new
SqlDataAdapter();
adapter.SelectCommand =
new
SqlCommand(
"SELECT [eventId], [title], [startdate], [enddate], [friendlyurl], [reglinkwebInApp] FROM [events] WHERE [enddate] > GETDATE() ORDER BY [startdate]"
, conn);
DataTable table =
new
DataTable();
conn.Open();
try
{
adapter.Fill(table);
}
finally
{
conn.Close();
}
this
.ViewState[
"_gds"
] = table;
return
table;
}
}
}
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource =
this
.GridSource;
}
protected
void
RadGrid1_InsertCommand(
object
sender, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item
as
GridEditableItem;
DataTable ordersTable =
this
.GridSource;
DataRow newRow = ordersTable.NewRow();
//As this example demonstrates only in-memory editing, a new primary key value should be generated
//This should not be applied when updating directly the database
DataRow[] allValues = ordersTable.Select(
""
,
"eventId"
, DataViewRowState.CurrentRows);
if
(allValues.Length > 0)
{
newRow[
"eventId"
] = (
int
)allValues[allValues.Length - 1][
"eventId"
] + 1;
}
else
{
newRow[
"eventId"
] = 1;
//the table is empty;
}
//Set new values
Hashtable newValues =
new
Hashtable();
//The GridTableView will fill the values from all editable columns in the hash
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
try
{
foreach
(DictionaryEntry entry
in
newValues)
{
newRow[(
string
)entry.Key] = entry.Value;
}
}
catch
(Exception ex)
{
Label1.Text +=
"Unable to insert into Orders. Reason: "
+ ex.Message;
e.Canceled =
true
;
}
ordersTable.Rows.Add(newRow);
//Code for updating the database ca go here...
Label1.Text +=
"Event "
+ newRow[
"eventId"
] +
" inserted"
;
}
protected
void
RadGrid1_UpdateCommand(
object
sender, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item
as
GridEditableItem;
DataTable ordersTable =
this
.GridSource;
//Locate the changed row in the DataSource
DataRow[] changedRows = ordersTable.Select(
"eventId = "
+
editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex][
"eventId"
].ToString());
if
(changedRows.Length != 1)
{
this
.Label1.Text +=
"Unable to locate the Order for updating."
;
e.Canceled =
true
;
return
;
}
//Update new values
Hashtable newValues =
new
Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
changedRows[0].BeginEdit();
try
{
foreach
(DictionaryEntry entry
in
newValues)
{
changedRows[0][(
string
)entry.Key] = entry.Value;
}
changedRows[0].EndEdit();
}
catch
(Exception ex)
{
changedRows[0].CancelEdit();
Label1.Text +=
"Unable to update Orders. Reason: "
+ ex.Message;
e.Canceled =
true
;
}
}
protected
void
RadGrid1_DeleteCommand(
object
sender, GridCommandEventArgs e)
{
}
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
if
(!
this
.IsPostBack)
{
this
.RadGrid1.MasterTableView.Rebind();
}
}
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
// ----------- Checkboxs fixed for Null ----------- //
if
((e.CommandName == RadGrid.InitInsertCommandName))
{
e.Canceled =
true
;
//Prepare an IDictionary with the predefined values
System.Collections.Specialized.ListDictionary newValues =
new
System.Collections.Specialized.ListDictionary();
//set initial checked state for the checkbox on init insert
newValues[
"reglinkwebInApp"
] =
false
;
//Insert the item and rebind
e.Item.OwnerTableView.InsertItem(newValues);
}
}
}
Thanks
0
Hi Mins,
You can check out various demos which demonstrate inserting records using the InsertCommand event handler:
- EditItemTemplates:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
- EditFormTemplate:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/templateformupdate/defaultcs.aspx
- EditForm UserControl:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/usercontroleditform/defaultcs.aspx
You can also take a look at the following article:
http://www.telerik.com/help/aspnet-ajax/grid-insert-update-delete-at-database-level.html
I hope this will prove helpful.
Regards,
Eyup
Telerik
You can check out various demos which demonstrate inserting records using the InsertCommand event handler:
- EditItemTemplates:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
- EditFormTemplate:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/templateformupdate/defaultcs.aspx
- EditForm UserControl:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/usercontroleditform/defaultcs.aspx
You can also take a look at the following article:
http://www.telerik.com/help/aspnet-ajax/grid-insert-update-delete-at-database-level.html
I hope this will prove helpful.
Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.