Hi all,
I have radgrid with 'EditMode="EditForms"'.
I am trying to put the grid in 'add new record' mode in page load, to insert a new row into the grid so that user need not click the add new button.
I have tried the below code,grid appears in insert mode ,but insert is not working properly.On clicking 'Insert',Radgrid_InsertCommand is not getting fired.I am trying to keep the add/refresh buttons and paging disabled in Prerender event.Please help me on this.
I have radgrid with 'EditMode="EditForms"'.
I am trying to put the grid in 'add new record' mode in page load, to insert a new row into the grid so that user need not click the add new button.
I have tried the below code,grid appears in insert mode ,but insert is not working properly.On clicking 'Insert',Radgrid_InsertCommand is not getting fired.I am trying to keep the add/refresh buttons and paging disabled in Prerender event.Please help me on this.
protected void gvNation_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
gvNation.MasterTableView.IsItemInserted = true;
gvNation.Rebind();
}
GridPagerItem pagerItem = (GridPagerItem)gvNation.MasterTableView.GetItems(GridItemType.Pager)[1];
GridCommandItem commandItem = (GridCommandItem)gvNation.MasterTableView.GetItems(GridItemType.CommandItem)[1];
GridCommandItem commandItem1 = (GridCommandItem)gvNation.MasterTableView.GetItems(GridItemType.CommandItem)[0];
if (gvNation.EditIndexes.Count > 0 || gvNation.MasterTableView.IsItemInserted)
{
pagerItem.Enabled = false;
commandItem.FindControl("PlaceHolder1").Visible = false;
Button btn = (Button)commandItem1.FindControl("btnAdd");
btn.Enabled = false;
Button btnR = (Button)commandItem1.FindControl("btnRefresh");
btnR.Enabled = false;
gvNation.AllowFilteringByColumn = false;
}
else
{
pagerItem.Enabled = true;
commandItem.FindControl("PlaceHolder1").Visible = true;
Button btn = (Button)commandItem1.FindControl("btnAdd");
btn.Enabled = true;
Button btnR = (Button)commandItem1.FindControl("btnRefresh");
btnR.Enabled = true;
gvNation.AllowFilteringByColumn = true;
}
}
4 Answers, 1 is accepted
0

Soumya
Top achievements
Rank 1
answered on 01 Aug 2012, 06:01 AM
Any suggestions?
Thanks,
Soumya
Thanks,
Soumya
0
Hello,
Could you post your full page source code along with the code-behind file content? Thus all the people who wants to help you will have better understanding of your project.
Greetings,
Andrey
the Telerik team
Could you post your full page source code along with the code-behind file content? Thus all the people who wants to help you will have better understanding of your project.
Greetings,
Andrey
the Telerik team
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 their blog feed now.
0

Soumya
Top achievements
Rank 1
answered on 02 Aug 2012, 07:47 AM
Hi,
Please find below my code.Please let me know your where I went wrong.
aspx:
aspx.cs
Please find below my code.Please let me know your where I went wrong.
aspx:
<%@ Page Language="C#" MasterPageFile="~/Masters/PageTemplate.Master" AutoEventWireup="true" CodeBehind="FrmNationality.aspx.cs" Inherits="UnicareSystemWeb.Settings.FrmNationality" %>
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"ContentPlaceHolder2"
runat
=
"server"
>
<
head
>
<
style
type
=
"text/css"
>
.heading_style
{
font-family:Tahoma;
color:#666;
font-size:18px;
font-weight:bold;
height:30px;
letter-spacing:1px;
}
body
{
margin:0px;
overflow:hidden;
font-family:Tahoma;
font-size:13px;
background-color:#B0E0E6;
}
.MyGridClass.rgDataDiv
{
height : auto;
}
</
style
>
</
head
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
Runat
=
"server"
>
</
telerik:RadScriptManager
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"gvNation"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"gvNation"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"MainMessage"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
table
border
=
"0"
cellpadding
=
"6"
cellspacing
=
"4"
width
=
"90%"
align
=
"center"
>
<
tr
>
<
td
colspan
=
"2"
align
=
"center"
class
=
"heading_style"
>
Nationality Data
</
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
align
=
"center"
>
<
span
id
=
"MainMessage"
runat
=
"server"
class
=
"errormessage"
style
=
"font: 10pt Verdana;font-weight:700;color:Red"
></
span
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"center"
>
<
telerik:RadGrid
ID
=
"gvNation"
runat
=
"server"
CellSpacing
=
"0"
GridLines
=
"None"
AllowSorting
=
"True"
AllowFilteringByColumn
=
"True"
AllowPaging
=
"True"
AutoGenerateColumns
=
"False"
PageSize
=
"10"
OnItemCommand
=
"gvNation_ItemCommand"
OnInsertCommand
=
"gvNation_InsertCommand"
OnUpdateCommand
=
"gvNation_UpdateCommand"
OnNeedDataSource
=
"gvNation_NeedDataSource"
OnItemCreated
=
"gvNation_ItemCreated"
OnPreRender
=
"gvNation_PreRender"
OnInit
=
"gvNation_Init"
Skin
=
"WebBlue"
Width
=
"350px"
CssClass
=
"MyGridClass"
>
<
PagerStyle
Mode
=
"NextPrevAndNumeric"
AlwaysVisible
=
"true"
/>
<
GroupingSettings
CaseSensitive
=
"false"
/>
<
ItemStyle
HorizontalAlign
=
"Left"
/>
<
HeaderStyle
HorizontalAlign
=
"Left"
/>
<
AlternatingItemStyle
HorizontalAlign
=
"Left"
/>
<
ClientSettings
>
<
Selecting
CellSelectionMode
=
"None"
></
Selecting
>
</
ClientSettings
>
<
MasterTableView
DataKeyNames
=
"ID"
CommandItemDisplay
=
"TopAndBottom"
EditMode
=
"EditForms"
InsertItemPageIndexAction
=
"ShowItemOnFirstPage"
NoMasterRecordsText
=
"No Nationality records exist!"
>
<%-- <
CommandItemSettings
AddNewRecordText
=
"Add New Nationality"
></
CommandItemSettings
>--%>
<%--<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
></
RowIndicatorColumn
>--%>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
></
ExpandCollapseColumn
>
<
CommandItemTemplate
>
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
></
asp:PlaceHolder
>
<
table
>
<
tr
>
<
td
align
=
"left"
>
<
asp:Button
ID
=
"btnAdd"
runat
=
"server"
CommandName
=
"InitInsert"
CssClass
=
"rgAdd"
/>
<
asp:Label
ID
=
"lbladd"
runat
=
"server"
Text
=
"Add Nationality"
></
asp:Label
>
</
td
>
<
td
align
=
"right"
>
<
asp:Button
ID
=
"btnRefresh"
runat
=
"server"
CommandName
=
"RebindGrid"
Text
=
"Refresh"
CssClass
=
"rgRefresh"
/>
<
asp:Label
ID
=
"lblref"
runat
=
"server"
Text
=
"Refresh"
></
asp:Label
>
</
td
>
</
tr
>
</
table
>
</
CommandItemTemplate
>
<
Columns
>
<
telerik:GridEditCommandColumn
>
</
telerik:GridEditCommandColumn
>
<
telerik:GridTemplateColumn
DataField
=
"NationName"
DefaultInsertValue
=
""
HeaderText
=
"Nationality"
UniqueName
=
"NationName"
FilterControlToolTip
=
"Search records using Filter"
FilterControlWidth
=
"200px"
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"txtNation"
Width
=
"200px"
runat
=
"server"
Text='<%# Bind("NationName") %>'></
asp:TextBox
>
<
span
style
=
"color: Red"
>*</
span
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator"
ControlToValidate
=
"txtNation"
ErrorMessage
=
"This field is required"
ForeColor
=
"Red"
runat
=
"server"
>
</
asp:RequiredFieldValidator
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label
ID
=
"LblNation"
runat
=
"server"
Text='<%# Eval("NationName") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
UniqueName
=
"EditCommandColumn1"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
td
>
</
tr
>
</
table
>
</
asp:Content
>
using
System;
using
System.Web.UI;
using
System.Collections.Generic;
using
System.Web.UI.WebControls;
using
Telerik.Web.UI;
namespace
UnicareSystemWeb.Settings
{
public
partial
class
FrmNationality : System.Web.UI.Page
{
private
NationalityService _nationalityService =
null
;
public
static
string
PageLetter =
string
.Empty;
protected
void
Page_Load(
object
sender, EventArgs e)
{
MainMessage.InnerHtml =
""
;
//gvNation.AllowFilteringByColumn = true;
}
protected
void
gvNation_Init(
object
sender, System.EventArgs e)
{
GridFilterMenu menu = gvNation.FilterMenu;
int
i = 0;
while
(i < menu.Items.Count)
{
if
(menu.Items[i].Text ==
"NoFilter"
|| menu.Items[i].Text ==
"Contains"
|| menu.Items[i].Text ==
"EqualTo"
)
{
i++;
}
else
{
menu.Items.RemoveAt(i);
}
}
}
public
string
Value
{
get
{
return
(
string
)ViewState[
"Value"
] ??
"%"
;
}
set
{
ViewState[
"Value"
] = value;
}
}
protected
void
gvNation_InsertCommand(
object
sender, GridCommandEventArgs e)
{
Nationality nationality =
new
Nationality();
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
try
{
GridEditableItem item = e.Item
as
GridEditableItem;
nationality.NationName = ((TextBox)item.FindControl(
"txtNation"
)).Text;
//nationality.IsActive = ((CheckBox)item.FindControl("ChkIsNationActive")).Checked;
_nationalityService =
new
NationalityService();
string
result=_nationalityService.SaveNationality(nationality);
if
(result ==
"S"
)
{
MainMessage.InnerHtml =
"Nationality "
+((TextBox)item.FindControl(
"txtNation"
)).Text+
" added successfully"
;
}
else
if
(result==
"E"
)
{
//e.Item.OwnerTableView.IsItemInserted = true;
MainMessage.InnerText =
"Nationality "
+((TextBox)item.FindControl(
"txtNation"
)).Text+
" add failed"
;
}
else
if
(result==
"D"
)
{
//e.Item.OwnerTableView.IsItemInserted = true;
MainMessage.InnerText =
"Nationality "
+((TextBox)item.FindControl(
"txtNation"
)).Text+
" already exist"
;
}
PageLetter = Value;
string
search = PageLetter;
_nationalityService =
new
NationalityService();
gvNation.DataSource = _nationalityService.GetAllNationalities(search);
}
catch
{
//raise an event for the error
}
}
}
public
void
gvNation_ItemCreated(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridCommandItem)
{
GridCommandItem commandItem = (e.Item
as
GridCommandItem);
if
(commandItem.NamingContainer
is
GridTFoot)
{
commandItem.FindControl(
"btnAdd"
).Visible =
false
;
commandItem.FindControl(
"lbladd"
).Visible =
false
;
commandItem.FindControl(
"btnRefresh"
).Visible =
false
;
commandItem.FindControl(
"lblref"
).Visible =
false
;
}
else
if
(commandItem.NamingContainer
is
GridTHead)
{
commandItem.FindControl(
"PlaceHolder1"
).Visible =
false
;
}
PlaceHolder container = (PlaceHolder)commandItem.FindControl(
"PlaceHolder1"
);
for
(
int
i = 65; i <= 65 + 25; i++)
{
LinkButton linkButton1 =
new
LinkButton();
LiteralControl lc =
new
LiteralControl(
" "
);
linkButton1.Text =
""
+ (
char
)i;
linkButton1.CommandName =
"alpha"
;
linkButton1.CommandArgument =
""
+ (
char
)i;
container.Controls.Add(linkButton1);
container.Controls.Add(lc);
}
LiteralControl lcLast =
new
LiteralControl(
" "
);
container.Controls.Add(lcLast);
LinkButton linkButtonAll =
new
LinkButton();
linkButtonAll.Text =
"All"
;
linkButtonAll.CommandName =
"NoFilter"
;
container.Controls.Add(linkButtonAll);
}
}
protected
void
gvNation_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
if
(PageLetter ==
""
)
{
PageLetter =
"%"
;
}
string
search = PageLetter;
_nationalityService =
new
NationalityService();
gvNation.DataSource = _nationalityService.GetAllNationalities(search);
}
protected
void
gvNation_UpdateCommand(
object
sender, GridCommandEventArgs e)
{
Nationality nationality =
new
Nationality();
if
(e.Item
is
GridEditableItem)
{
try
{
GridEditableItem item = e.Item
as
GridEditableItem;
nationality.ID = (
int
)item.GetDataKeyValue(
"ID"
);
nationality.NationName = ((TextBox)item.FindControl(
"txtNation"
)).Text;
//nationality.IsActive = ((CheckBox)item.FindControl("ChkIsNationActive")).Checked;
_nationalityService =
new
NationalityService();
string
result=_nationalityService.UpdateNationality(nationality);
if
(result ==
"S"
)
{
MainMessage.InnerHtml =
"Nationality "
+((TextBox)item.FindControl(
"txtNation"
)).Text+
" updated successfully"
;
}
else
if
(result ==
"E"
)
{
MainMessage.InnerText =
"Nationality "
+((TextBox)item.FindControl(
"txtNation"
)).Text+
" update failed"
;
}
else
if
(result ==
"D"
)
{
MainMessage.InnerText =
"Nationality "
+((TextBox)item.FindControl(
"txtNation"
)).Text+
" already exist"
;
}
gvNation.Rebind();
//string search = PageLetter;
//_nationalityService = new NationalityService();
//gvNation.DataSource = _nationalityService.GetAllNationalities(search);
}
catch
{
}
}
}
protected
void
gvNation_ItemCommand(
object
source, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.EditCommandName)
{
gvNation.MasterTableView.IsItemInserted =
false
;
gvNation.AllowFilteringByColumn =
false
;
}
if
(e.CommandName == RadGrid.InitInsertCommandName)
{
gvNation.MasterTableView.ClearEditItems();
gvNation.AllowFilteringByColumn =
false
;
if
(e.Item
is
GridEditableItem)
{
GridEditableItem item = e.Item
as
GridEditableItem;
((TextBox)item.FindControl(
"txtNation"
)).Focus();
}
}
if
(e.CommandName == RadGrid.CancelCommandName)
{
gvNation.AllowFilteringByColumn =
true
;
}
//String Value = null;
if
(e.CommandName == RadGrid.RebindGridCommandName)
{
gvNation.MasterTableView.SortExpressions.Clear();
gvNation.MasterTableView.GroupByExpressions.Clear();
gvNation.CurrentPageIndex = 0;
gvNation.MasterTableView.FilterExpression =
""
;
foreach
(GridColumn column
in
gvNation.MasterTableView.RenderColumns)
{
if
(column.SupportsFiltering())
{
column.CurrentFilterValue =
string
.Empty;
column.CurrentFilterFunction = GridKnownFunction.NoFilter;
}
}
gvNation.MasterTableView.IsItemInserted =
false
;
gvNation.MasterTableView.ClearEditItems();
PageLetter =
"%"
;
Value =
"%"
;
gvNation.Rebind();
}
switch
(e.CommandName)
{
case
(
"alpha"
):
{
Value =
string
.Format(
"{0}%"
, e.CommandArgument);
break
;
}
case
(
"NoFilter"
):
{
Value =
"%"
;
//gvNation.MasterTableView.IsItemInserted = false;
//gvNation.MasterTableView.ClearEditItems();
break
;
}
case
(
"Filter"
):
{
Pair filterPair = (Pair)e.CommandArgument;
string
filterpair = filterPair.First.ToString();
// contains the Filter function
GridFilteringItem item = (GridFilteringItem)e.Item;
TextBox searchValue = (TextBox)(e.Item
as
GridFilteringItem)[
"NationName"
].Controls[0];
string
searchvalue = searchValue.Text;
// contains the text given in the filter textbox
if
(filterpair ==
"Contains"
)
{
Value =
"%"
+ searchvalue +
"%"
;
break
;
}
else
if
(filterpair ==
"NoFilter"
)
{
Value =
"%"
;
break
;
}
else
if
(filterpair ==
"EqualTo"
)
{
Value = searchvalue;
}
break
;
}
//case ("Page"):
// {
// PageLetter = Value;
// break;
// }
}
PageLetter = Value;
gvNation.Rebind();
}
protected
void
gvNation_PreRender(
object
sender, EventArgs e)
{
//if (!Page.IsPostBack)
//{
// gvNation.MasterTableView.IsItemInserted = true;
// gvNation.Rebind();
//}
GridPagerItem pagerItem = (GridPagerItem)gvNation.MasterTableView.GetItems(GridItemType.Pager)[1];
GridCommandItem commandItem = (GridCommandItem)gvNation.MasterTableView.GetItems(GridItemType.CommandItem)[1];
GridCommandItem commandItem1 = (GridCommandItem)gvNation.MasterTableView.GetItems(GridItemType.CommandItem)[0];
if
(gvNation.EditIndexes.Count > 0 || gvNation.MasterTableView.IsItemInserted)
{
pagerItem.Enabled =
false
;
commandItem.FindControl(
"PlaceHolder1"
).Visible =
false
;
Button btn = (Button)commandItem1.FindControl(
"btnAdd"
);
btn.Enabled =
false
;
Button btnR = (Button)commandItem1.FindControl(
"btnRefresh"
);
btnR.Enabled =
false
;
gvNation.AllowFilteringByColumn =
false
;
}
else
{
pagerItem.Enabled =
true
;
commandItem.FindControl(
"PlaceHolder1"
).Visible =
true
;
Button btn = (Button)commandItem1.FindControl(
"btnAdd"
);
btn.Enabled =
true
;
Button btnR = (Button)commandItem1.FindControl(
"btnRefresh"
);
btnR.Enabled =
true
;
gvNation.AllowFilteringByColumn =
true
;
}
}
}
}
0
Hi,
I reviewed your code and noticed that there is only a button with a CommandName equal to "InitInsert", but this command does not raise the InsertCommand event, the event is raised from a button with CommandName equal to "PerformInsert".
Give this approach a try and check whether you get the desired behavior.
Kind regards,
Andrey
the Telerik team
I reviewed your code and noticed that there is only a button with a CommandName equal to "InitInsert", but this command does not raise the InsertCommand event, the event is raised from a button with CommandName equal to "PerformInsert".
Give this approach a try and check whether you get the desired behavior.
Kind regards,
Andrey
the Telerik team
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 their blog feed now.