| public void InstantiateIn(System.Web.UI.Control container) |
| { |
| //This throws exception |
| GridEditFormItem item = ((GridEditFormItem)(container.NamingContainer)); |
| } |
| <telerik:GridTemplateColumn DataField="Username" |
| HeaderText="Username" |
| UniqueName="Username" |
| SortExpression="Username"> |
| <ItemTemplate> |
| <asp:Label ID="lblUsername" runat="server" Text='<%# Eval("Username") %>' /> |
| </ItemTemplate> |
| </telerik:GridTemplateColumn> |
| protected void MembersGrid_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if ((e.Item is GridDataItem)) |
| { |
| GridDataItem dataItem = (GridDataItem)e.Item; |
| string username = dataItem["Username"].Text; |
| ImageButton btnEditUser = (ImageButton)e.Item.FindControl("EditUserImgBtn"); |
| btnEditUser.Attributes["href"] = "#"; |
| btnEditUser.Attributes["onclick"] = String.Format("return ShowEditUser('{0}', '{1}');", username, e.Item.ItemIndex); |
| } |
| } |
| <telerik:RadAjaxPanel ID="_panFilec" runat="server" Height="188px" |
| Width="90%" LoadingPanelID="_fileconloadpan" oninit="RadAjaxPanel1_Init" Visible="false" > |
| <telerik:RadAjaxLoadingPanel ID="_fileconloadpan" runat="server" Height="75px" |
| Width="75px" Transparency="30"> |
| <img src="/_layouts/images/myTestloader.gif" alt="Loading..." style="border: 0; margin-top: 45px; color:Black;"/> |
| </telerik:RadAjaxLoadingPanel> |
| </telerik:RadAjaxPanel> |
| public partial class TestWebControl : System.Web.UI.UserControl |
| { |
| private const string _LOADINGSOURCE = "/_controltemplates/TestWebControl.ascx"; |
| private const string _VIEWSTATEITEMID = "VIEWSTATEITEMID"; |
| private const string _DATASOURCECACHE = "_DATASORCECACHE"; |
| private ICachingOperations _caching; |
| private ITelerikBehavior _telerikbehavior; |
| private ISearchBehavior _search; |
| private RadGrid _gridViewContent; |
| private List<GridColumn> _columns; |
| public List<GridColumn> Columns |
| { |
| get |
| { |
| return this._columns; |
| } |
| set |
| { |
| this._columns= value; |
| } |
| } |
| private string _lastchacheDependency; |
| private string _chacheDependency; |
| public string CacheDependency |
| { |
| get { return _chacheDependency; } |
| set { _chacheDependency = value; } |
| } |
| public ISearchBehavior Search |
| { |
| get { return _search; } |
| set { _search = value; } |
| } |
| private bool _enableSelectColumn = true; |
| public bool EnableSelectColumn |
| { |
| get { return _enableSelectColumn ; } |
| set { _enableSelectColumn = value; } |
| } |
| public ITelerikBehavior Telerikbehavior |
| { |
| get |
| { |
| if (_telerikbehavior != null) |
| { return _telerikbehavior; } |
| else |
| { |
| this._telerikbehavior = new TestTelerikBehavior(); |
| return _telerikbehavior; |
| } |
| } |
| set |
| { |
| if (_telerikbehavior != null) |
| { |
| _telerikbehavior = value; |
| } |
| } |
| } |
| public ICachingOperations Caching |
| { |
| get { |
| if(_caching!=null) |
| {return _caching;} |
| else |
| { |
| this._caching = new ControlToSession(this.ID); |
| return _caching; |
| } |
| } |
| set |
| { |
| if (value != null) |
| {_caching = value;} |
| } |
| } |
| public static TestWebControl LoadFileContent(Page page) |
| { |
| return page.LoadControl(_LOADINGSOURCE) as FileContentWebControl; |
| } |
| private int pageSize = 5; |
| /// <summary> |
| /// Gets or sets the number of items to show per page |
| /// </summary> |
| public int PageSize |
| { |
| get { return pageSize; } |
| set { pageSize = value; } |
| } |
| //public void ResetColumns(List<GridColumn> columns) |
| //{ |
| // |
| //} |
| protected override void OnInit(EventArgs e) |
| { |
| base.OnInit(e); |
| _gridViewContent = new RadGrid(); |
| _gridViewContent.AutoGenerateColumns = false; |
| _gridViewContent.Attributes.Add("Width", "80%"); |
| _gridViewContent.AllowPaging = true; |
| _gridViewContent.PageSize = this.PageSize; |
| _gridViewContent.AllowSorting = true; |
| _gridViewContent.AllowMultiRowSelection = true; |
| _gridViewContent.PagerStyle.Mode=GridPagerMode.NextPrev; |
| //_gridViewContent.ClientSettings.AllowDragToGroup = true; |
| _gridViewContent.ClientSettings.EnableRowHoverStyle = true; |
| _gridViewContent.ClientSettings.EnablePostBackOnRowClick = true; |
| //_gridViewContent.ClientSettings.Selecting.AllowRowSelect = true; |
| //_gridViewContent.ItemCreated += new GridItemEventHandler(_gridViewContent_ItemCreated); |
| _gridViewContent.Skin = this.Telerikbehavior.TelerikSkin; |
| //_gridViewContent.EnableViewState = false; |
| _gridViewContent.NeedDataSource+=new GridNeedDataSourceEventHandler(_gridViewContent_NeedDataSource); |
| } |
| void _gridViewContent_NeedDataSource(object source, GridNeedDataSourceEventArgs e) |
| { |
| if (!string.IsNullOrEmpty(_chacheDependency) && Columns != null) |
| { |
| DataTable datssource = getData(); |
| if (source != null && datssource.Rows.Count > 0) |
| { |
| _gridViewContent.DataSource = datssource; |
| } |
| else |
| { |
| _gridViewContent.DataSource = null; |
| } |
| } |
| else |
| { |
| _gridViewContent.DataSource = null; |
| } |
| } |
| //void _gridViewContent_ItemCreated(object sender, GridItemEventArgs e) |
| //{ |
| // if (e.Item is GridDataItem) |
| // { |
| // e.Item.PreRender += new EventHandler(Item_PreRender); |
| // } |
| //} |
| //void Item_PreRender(object sender, EventArgs e) |
| //{ |
| // ((sender as GridDataItem)["CheckBoxTemplateColumn"].FindControl("IMGchbx") as CheckBox).Checked = (sender as GridDataItem).Selected; |
| //} |
| protected void RadAjaxPanel1_Init(object sender, EventArgs e) |
| { |
| } |
| protected override void CreateChildControls() |
| { |
| base.CreateChildControls(); |
| string traceConst = _TRACECONST + "CreateChildControls()"; |
| ECSpand.Logging.Log.WriteDebug("Entering", traceConst); |
| if (Columns != null) |
| { |
| if (_enableSelectColumn ) |
| { |
| TESTCHBoxColumn columnun = new TESTCHBoxColumn(); |
| columnun.UniqueName = "CheckBoxTemplateColumn"; |
| _gridViewContent.MasterTableView.Columns.Add(columnun); |
| } |
| //_gridViewContent.Columns.Clear(); |
| Columns.ForEach(o => _gridViewContent.Columns.Add(o)); |
| } |
| if (ViewState[_VIEWSTATEITEMID] != null) |
| _lastchacheDependency = (ViewState[_VIEWSTATEITEMID] as string); |
| _panFilec.Controls.Add(_gridViewContent); |
| } |
| protected override void OnPreRender(System.EventArgs e) |
| { |
| if (!string.IsNullOrEmpty(_chacheDependency) && Columns !=null) |
| { |
| DataTable source = getData(); |
| if (source !=null && source.Rows.Count > 0) |
| { |
| _gridViewContent.DataSource = source; |
| if (ItemChanged()) |
| { |
| _gridViewContent.Rebind(); |
| } |
| _panFilec.Visible = true; |
| } |
| else |
| { _gridViewContent.Rebind(); } |
| ViewState[_VIEWSTATEITEMID] = this._chacheDependency; |
| } |
| else |
| { |
| _gridViewContent.Rebind(); |
| ViewState[_VIEWSTATEITEMID] = null; |
| } |
| } |
| public void ResetGridView() |
| { |
| if (_gridViewContent != null) |
| { |
| //_gridViewContent.DataSource = null; |
| this._chacheDependency = null; |
| _gridViewContent.Rebind(); |
| _panFilec.Controls.Clear(); |
| _panFilec.Visible = false; |
| this.Caching.RemoveObjectFromSession(_DATASOURCECACHE); |
| } |
| } |
| private DataTable getData() |
| { |
| if (ItemChanged()&& Search!=null) |
| { |
| DataTable table = this.Search.PerformSearch(); |
| this.Caching.SaveObjectToSession(_DATASOURCECACHE, table); |
| return table; |
| } |
| else |
| { |
| return this.Caching.GetObjectFromSession(_DATASOURCECACHE) as DataTable; |
| } |
| } |
| protected override void Render(HtmlTextWriter writer) |
| { |
| _panFilec.RenderControl(writer); |
| } |
| /// <summary> |
| /// Checks, if a different item is displayed |
| /// </summary> |
| /// <returns></returns> |
| bool ItemChanged() |
| { |
| return !(_chacheDependency.CompareTo(_lastchacheDependency)==0); |
| } |
| } |
| } |
| public class TESTCHBoxColumn: GridTemplateColumn |
| { |
| public TESTCHBoxColumn() |
| { |
| ISImageButtonTemplate btntemplate = new ISImageButtonTemplate(); |
| ISImageCheckBox chbxtempalte = new ISImageCheckBox(); |
| this.HeaderTemplate = btntemplate; |
| this.ItemTemplate = chbxtempalte; |
| } |
| } |
| public class ISImageButtonTemplate : System.Web.UI.ITemplate |
| { |
| #region ITemplate Members |
| private const string _CHECKATT = "CHECKATT"; |
| ImageButton _btn; |
| public ISImageButtonTemplate() |
| { |
| _btn = new ImageButton(); |
| _btn.ID = "ISImgBtn"; |
| _btn.ImageUrl = "/_layouts/images/niceTestpic.png"; |
| _btn.Click += new System.Web.UI.ImageClickEventHandler(_btn_Click); |
| _btn.Attributes.Add(_CHECKATT, "false"); |
| } |
| public void InstantiateIn(System.Web.UI.Control container) |
| { |
| container.Controls.Add(_btn); |
| } |
| void _btn_Click(object sender, System.Web.UI.ImageClickEventArgs e) |
| { |
| ImageButton imgbtn = sender as ImageButton; |
| RadGrid grid = imgbtn.Parent.Parent.Parent.Parent.Parent.Parent as RadGrid; |
| if (grid != null) |
| { |
| bool check = !My.ConversionHelper.StringToBool(imgbtn.Attributes[_CHECKATT] as string); |
| foreach (GridDataItem dataItem in grid.Items) |
| { |
| (dataItem.FindControl("IMGchbx") as CheckBox).Checked = check; |
| dataItem.Selected = check; |
| } |
| imgbtn.Attributes[_CHECKATT] = (check).ToString(); |
| } |
| } |
| } |
| public class ISImageCheckBox :System.Web.UI.ITemplate |
| { |
| private CheckBox _box; |
| private int number = 0; |
| public ISImageCheckBox() |
| { |
| number = 0; |
| } |
| public void InstantiateIn(System.Web.UI.Control container) |
| { |
| number++; |
| _box = new CheckBox(); |
| _box.AutoPostBack = true; |
| _box.ID = "IMGchbx"; |
| _box.CheckedChanged += new EventHandler(_box_CheckedChanged); |
| container.Controls.Add(_box); |
| } |
| void _box_CheckedChanged(object sender, EventArgs e) |
| { |
| ((sender as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked; |
| } |
| } |
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
[FormatException: Input string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7469351 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119 System.Web.UI.WebControls.ImageButton.LoadPostData(String postDataKey, NameValueCollection postCollection) +193 System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +14 System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +693 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1743 |
Receiving the error: "Insert Item is available only when grid is in insert mode." Shown at the end of this post. I have a grid with 3 text boxes and 3 dropdown lists. When I click a link " Add new item" it calls the RadGrid1_InsertCommand which would add a blank record to my data source and then I rebind. I do not modify any of the controls directly just the data source and rebind. Suddenly I have started to recieve this error and I don't understand what has changed for me to start getting this error. I've had this working fine. Please provide any insight you may have.
<Page snippet>
<rad:RadGrid ID="RadGrid1"
runat="server"
AutoGenerateColumns="False"
BorderStyle="Groove"
BorderWidth="2px"
UseAccessibleHeader="true"
Width="95%"
EnableEmbeddedSkins="false"
Skin="Skin1"
OnPreRender="RadGrid1_PreRender" GridLines="None" >
<itemstyle backcolor="Gainsboro" />
<alternatingitemstyle backcolor="WhiteSmoke" />
<MasterTableView EditMode="InPlace" DataKeyNames="EKey"
CommandItemDisplay="Bottom" AddNewRecordText="New Item" horizontalalign="Center">
<CommandItemTemplate>
<asp:LinkButton ID="lnkAddNew" runat="server" CommandName="PerformInsert" ><img style="border:0px;" alt="New Item" src="/Grid/AddRecord.gif" /> Add CPE</asp:LinkButton>
</CommandItemTemplate>
<rowindicatorcolumn>
<HeaderStyle Width="20px" />
</rowindicatorcolumn>
<expandcollapsecolumn>
<HeaderStyle Width="20px" />
</expandcollapsecolumn>
<Columns>
<rad:GridBoundColumn DataField="EKey" ReadOnly="false" Visible="false" UniqueName="ElementCPEKey"/>
<%--<rad:GridBoundColumn DataField="CKey" ReadOnly="false" Visible="false" UniqueName="CPEKey"/>--%>
<rad:GridTemplateColumn HeaderText="Item Name" ItemStyle-Wrap="true" ItemStyle-width="20%" ItemStyle-CssClass="StdFont">
<ItemTemplate><%# Eval("ItemNameText")%></ItemTemplate>
<EditItemTemplate >
<rad:RadTextBox ID="txtItemName" runat="server" Columns="60" Text='<%# Eval("ItemName") %>' width="99%" OnTextChanged="txtItemName_OnTextChanged" AutoPostBack="true"></rad:RadTextBox>
<br />
</EditItemTemplate>
<ItemStyle Width="20%" Wrap="True" />
</rad:GridTemplateColumn>
</End Page Snippet>
<Code Behind Grid Events>
#region Grid methods
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
{
//LoadLookupData(RadGrid1.MasterTableView.Items.Count - 1);
GridEditableItem editedItem = e.Item as GridEditableItem;
EDTO gridDTO = e.Item.DataItem as EDTO;
VMSDropDownList ddl = editedItem.FindControl("ddlList") as VMSDropDownList;
ddl.DataTextField = "Descrip";
ddl.DataValueField = "ListKey";
ddl.DataSource = Manager.RetrieveList(session);
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("None"));
ddl.SelectedValue = 1;
<... repeated twice with two other lists ...>
}
}
void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
//if (dsDTO == null)
//{
// dsDTO = new DataSource<EDTO>();
// AddBlankRow();
//}
//else
//{
// GridDataItem dataItem = RadGrid1.MasterTableView.Items[RadGrid1.MasterTableView.Items.Count - 1];
// RadTextBox itemTextBox = dataItem.FindControl("txtItemName") as RadTextBox;
// RadTextBox itemIDTextBox = dataItem.FindControl("txtItemID") as RadTextBox;
// if (itemTextBox.Text.Length > 0 && itemIDTextBox.Text.Length > 0)
// {
// //dataItem.Enabled = false;
// AddBlankRow();
// }
// else
// {
// SetResultText("An item must be selected before adding a new record to this element.");
// BindElementGrid();
// }
//}
}
void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
{
if (dsDTO != null)
{
if (dsDTO.DataItems[e.Item.ItemIndex].IsLoadedFlag == 1) //we loaded from database
dsDTO.DataItems[e.Item.ItemIndex].DeleteFlag = 1;
else
dsDTO.DataItems.RemoveAt(e.Item.ItemIndex); //This is new, the user just added it so remove it from the list.
if (e.Item.ItemIndex == 0)
dsDTO = null;
BindElementGrid();
}
}
private void BindElementGrid()
{
if (dsDTO != null)
{
RadGrid1.DataSource = dsDTO;
//RadGrid1.DataSource = RetrieveActiveDataSource();
for (int i = 0; i < RadGrid1.PageSize; i++)
{
RadGrid1.EditIndexes.Add(i);
}
this.RadGrid1.DataBind();
ViewState[this.UniqueID + "dsDTO"] = dsDTO;
}
}
//Tried to use this instead to get rid of the GetInsert() error
private void RadGrid_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == "PerformInsert")
{
if (dsDTO == null)
{
dsDTO = new DataSource<EDTO>();
AddBlankRow();
}
else
{
GridDataItem dataItem = RadGrid1.MasterTableView.Items[RadGrid1.MasterTableView.Items.Count - 1];
RadTextBox itemTextBox = dataItem.FindControl("txtItemName") as RadTextBox;
RadTextBox itemIDTextBox = dataItem.FindControl("txtItemID") as RadTextBox;
if (itemTextBox.Text.Length > 0 && itemIDTextBox.Text.Length > 0)
{
//dataItem.Enabled = false;
AddBlankRow();
}
else
{
SetResultText("An item must be selected before adding a new record to this element.");
BindElementCPEGrid();
}
}
}
}
#endregion
</End Code Behind Grid Events>
Server Error in '/' Application.
--------------------------------------------------------------------------------
Insert item is available only when grid is in insert mode.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Telerik.Web.UI.GridException: Insert item is available only when grid is in insert mode.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[GridException: Insert item is available only when grid is in insert mode.]
Telerik.Web.UI.GridTableView.GetInsertItem() +211
Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +2910
Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +134
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +38
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +115
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
