I want to populate fields within a radgrid with some phony data when the button is clicked.
The GetInsertItem() statement causes exception:
Exception Details: Telerik.Web.UI.GridException: Insert item is available only when grid is in insert mode.
The GetInsertItem() statement causes exception:
Exception Details: Telerik.Web.UI.GridException: Insert item is available only when grid is in insert mode.
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"ContentPlaceHolder1"
Runat
=
"Server"
>
<
div
>
<
telerik:RadButton
runat
=
"server"
ID
=
"RadButton1"
AutoPostBack
=
"true"
Text
=
"fill data"
ButtonType
=
"LinkButton"
onclick
=
"RadButton1_Click"
></
telerik:RadButton
>
</
div
>
<
telerik:RadAjaxPanel
runat
=
server
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
ShowStatusBar
=
"True"
GridLines
=
"None"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnCreateColumnEditor
=
"RadGrid1_CreateColumnEditor"
OnPreRender
=
"RadGrid1_PreRender"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
OnItemCreated
=
"RadGrid1_ItemCreated"
>
<
MasterTableView
DataKeyNames
=
"id"
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridBoundColumn
HeaderText
=
"Emp"
DataField
=
"emp"
UniqueName
=
"emp"
>
protected void RadButton1_Click(object sender, EventArgs e)
{
GridEditFormInsertItem i = (GridEditFormInsertItem)RadGrid1.MasterTableView.GetInsertItem();
RadTextBox r = (RadTextBox)i.FindControl("emp");
r.Text = "Johnny";
}
16 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 17 Jul 2013, 04:47 AM
Hi Lisa,
I was able to replicate the issue.If you are clicking the button without opening the insert form,such an error occurs ,so make sure that the form is open.Then please try the below code snippet to access the textbox of a bound column.
C#:
Thanks,
Princy
I was able to replicate the issue.If you are clicking the button without opening the insert form,such an error occurs ,so make sure that the form is open.Then please try the below code snippet to access the textbox of a bound column.
C#:
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
if
(RadGrid1.MasterTableView.IsItemInserted)
{
GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
TextBox r = (TextBox)i[
"emp"
].Controls[0];
// Access textbox of boundcolumn
r.Text =
"Johnny"
;
}
}
Thanks,
Princy
0
Lisa
Top achievements
Rank 1
answered on 17 Jul 2013, 01:48 PM
Hi Princy, On the page it displays the button and below that is the radgrid. So how can I open the insert form as you suggested? Thanks.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Jul 2013, 02:02 PM
Hello,
Thanks,
Jayesh Goyani
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"Open insert mode in grid"
onclick
=
"Button1_Click"
/>
<
asp:Button
ID
=
"Button2"
runat
=
"server"
Text
=
"Set value in Insert mode"
onclick
=
"Button2_Click"
/>
<
asp:Button
ID
=
"Button3"
runat
=
"server"
Text
=
"Save the value of insert mode"
onclick
=
"Button3_Click"
/>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"ID"
UniqueName
=
"ID"
HeaderText
=
"ID"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
dynamic data =
new
[] {
new
{ ID = 1, Name =
"Name1"
},
new
{ ID = 2, Name =
"Name2"
},
new
{ ID = 3, Name =
"Name3"
},
new
{ ID = 4, Name =
"Name4"
},
new
{ ID = 5, Name =
"Name5"
}
};
RadGrid1.DataSource = data;
}
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
RadGrid1.MasterTableView.IsItemInserted =
true
;
RadGrid1.Rebind();
}
protected
void
Button2_Click(
object
sender, EventArgs e)
{
if
(RadGrid1.MasterTableView.IsItemInserted)
{
GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
TextBox r = (TextBox)i[
"ID"
].Controls[0];
// Access textbox of boundcolumn
r.Text =
"0"
;
}
}
protected
void
Button3_Click(
object
sender, EventArgs e)
{
if
(RadGrid1.MasterTableView.IsItemInserted)
{
GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
TextBox r = (TextBox)i[
"ID"
].Controls[0];
// Access textbox of boundcolumn
// Get this textbox text value and perform your futhur operation
}
RadGrid1.MasterTableView.IsItemInserted =
false
;
RadGrid1.Rebind();
}
Thanks,
Jayesh Goyani
0
Lisa
Top achievements
Rank 1
answered on 17 Jul 2013, 02:22 PM
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"ContentPlaceHolder1"
Runat
=
"Server"
>
<
div
>
<
telerik:RadButton
runat
=
"server"
ID
=
"RadButton1"
AutoPostBack
=
"true"
Text
=
"fill data"
ButtonType
=
"LinkButton"
onclick
=
"RadButton1_Click"
></
telerik:RadButton
>
</
div
>
<
telerik:RadAjaxPanel
runat
=
server
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
ShowStatusBar
=
"True"
GridLines
=
"None"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnCreateColumnEditor
=
"RadGrid1_CreateColumnEditor"
OnPreRender
=
"RadGrid1_PreRender"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
OnItemCreated
=
"RadGrid1_ItemCreated"
>
<
MasterTableView
DataKeyNames
=
"id"
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridBoundColumn
HeaderText
=
"Emp"
DataField
=
"emp"
UniqueName
=
"emp"
>
protected void RadButton1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.IsItemInserted =
true
;
RadGrid1.Rebind();
GridEditFormInsertItem i = (GridEditFormInsertItem)RadGrid1.MasterTableView.GetInsertItem();
RadTextBox r = (RadTextBox)i.FindControl("emp");
r.Text = "Johnny";
}
Thanks for the example. I want only 1 button, so I take the idea from your example and add:
RadGrid1.MasterTableView.IsItemInserted =
true
;
RadGrid1.Rebind();
It inserts another grid to the page. I do not want 2 grids on the page. What I want is: when the button is clicked, simply replace the text for the text box. How can that be done?
If I could find the control via the parameter Eventargs, that'd be simply changing the text from there.
But how can I find the control via Eventargs?
0
Lisa
Top achievements
Rank 1
answered on 17 Jul 2013, 10:17 PM
Any suggestion please?
0
Princy
Top achievements
Rank 2
answered on 18 Jul 2013, 02:58 AM
Hi Lisa,
Please try the below code snippet to achieve the insert form to be opened as well as fill in the textbox with value.
C#:
Thanks,
Princy
Please try the below code snippet to achieve the insert form to be opened as well as fill in the textbox with value.
C#:
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
if
(!RadGrid1.MasterTableView.IsItemInserted)
//To Open insertForm
{
RadGrid1.MasterTableView.IsItemInserted =
true
;
RadGrid1.Rebind();
}
else
if
(RadGrid1.MasterTableView.IsItemInserted)
{
GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
TextBox r = (TextBox)i[
"emp"
].Controls[0];
// Access textbox of boundcolumn
r.Text =
"Johnny"
;
}
}
Thanks,
Princy
0
Lisa
Top achievements
Rank 1
answered on 18 Jul 2013, 03:01 PM
RadGrid1.MasterTableView.IsItemInserted =
true
;
RadGrid1.Rebind();
Again, by setting RadGrid1.MasterTableView.IsItemInserted =
true
;
it inserts another grid to the page. I do not want 2 grids on the page.
All I want is: when the button is clicked, simply replace the text for
the text box. How can that be done? Let's forget about having to open insert form because it may not be needed. If you could tell me how to find the control via the parameter Eventargs, I can simply change the text from there. With regular grid I can set the text simply via textbox1.Text = "hello", but with radgrid I don't know how it works, why do I have to deal with open insert form in order to set the text? Is there documentation describing insert form, edit form... that stuff? Thanks.
0
Princy
Top achievements
Rank 2
answered on 19 Jul 2013, 04:19 AM
Hi Lisa,
I think you want to insert a value into the textbox during insert mode.I guess you want to add the text to a bound column.When in insert mode,the boundcolumn will be a textbox and you can set the value of that textbox in insert mode.By setting
Below is a code which shows how to access the textbox of bound column and Template columns of a grid on a button click in insert and view mode. The BoundColumn will be a TextBox only in edit/insert mode.
ASPX:
C#:
Thanks,
Princy
I think you want to insert a value into the textbox during insert mode.I guess you want to add the text to a bound column.When in insert mode,the boundcolumn will be a textbox and you can set the value of that textbox in insert mode.By setting
RadGrid1.MasterTableView.IsItemInserted =
true
;
this is just to open the insert form of the radgrid,it doesn't add a new grid.Could you please elaborate on which mode you want to access the textbox,insert mode or edit mode.Could you please provide your full code aspx and c#.Below is a code which shows how to access the textbox of bound column and Template columns of a grid on a button click in insert and view mode. The BoundColumn will be a TextBox only in edit/insert mode.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
AutoGenerateEditColumn
=
"true"
>
<
MasterTableView
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridBoundColumn
UniqueName
=
"OrderID"
DataField
=
"OrderID"
HeaderText
=
"OrderID"
/>
<
telerik:GridTemplateColumn
HeaderText
=
"TemplateColumn"
>
<
ItemTemplate
>
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
></
asp:TextBox
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"TextBox2"
runat
=
"server"
></
asp:TextBox
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
<
telerik:RadButton
runat
=
"server"
ID
=
"RadButton1"
AutoPostBack
=
"true"
Text
=
"fill data"
ButtonType
=
"LinkButton"
OnClick
=
"RadButton1_Click"
>
</
telerik:RadButton
>
C#:
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
if
(RadGrid1.MasterTableView.IsItemInserted)
//Checking if in Insert mode
{
GridEditableItem edititem = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
TextBox boundtxt = (TextBox)edititem[
"OrderID"
].Controls[0];
// Access textbox of boundcolumn
boundtxt.Text =
"Text1"
;
TextBox templatetxt = (TextBox)edititem.FindControl(
"TextBox2"
);
//Access textbox of Template column
templatetxt.Text =
"Text2"
;
}
foreach
(GridDataItem item
in
RadGrid1.Items)
{
TextBox txt = (TextBox)item.FindControl(
"TextBox1"
);
//Access template textbox in view mode
txt.Text =
"Text3"
;
}
}
Thanks,
Princy
0
Lisa
Top achievements
Rank 1
answered on 19 Jul 2013, 02:47 PM
Here is all the code related to this problem. I have a radgrid that contains an emp field. When the page is displayed the very first time, emp is blank. User enters data into the emp field and submit it. A record is inserted into a table in the database to save the emp field. Whenever the button is clicked, I want it to override the value in the emp field with "Text3". That is all I want it to do.
In RadButton1_Click() it gives object reference not set to an instance of an object error because it cannot find the control emp.
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"ContentPlaceHolder1"
Runat
=
"Server"
>
<
div
>
<
telerik:RadButton
runat
=
"server"
ID
=
"RadButton1"
AutoPostBack
=
"true"
Text
=
"fill data"
ButtonType
=
"LinkButton"
onclick
=
"RadButton1_Click"
></
telerik:RadButton
>
</
div
>
<
telerik:RadAjaxPanel
runat
=
server
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
ShowStatusBar
=
"True"
GridLines
=
"None"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnCreateColumnEditor
=
"RadGrid1_CreateColumnEditor"
OnPreRender
=
"RadGrid1_PreRender"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
OnItemCreated
=
"RadGrid1_ItemCreated"
>
<
MasterTableView
DataKeyNames
=
"id"
CommandItemDisplay
=
"Top"
>
<
Columns
>
<
telerik:GridBoundColumn
HeaderText
=
"Employee"
DataField
=
"employee"
UniqueName
=
"employee"
>
</Columns>
<FormTemplate>
<telerik:RadTextBox id="emp" runat="server" Text='<%# Bind("emp") %>' />
protected void page_load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (data1.Tables[0].Rows.Count < 1)
{
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid1.Rebind();
}
else
RadGrid1.MasterTableView.IsItemInserted = false;
}
}
protected void RadButton1_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.Items)
{
RadTextBox txt = (RadTextBox)item.FindControl("emp");
txt.Text = "Text3"; //got error: object reference not set to an instance of an object
}
}
0
Princy
Top achievements
Rank 2
answered on 22 Jul 2013, 03:45 AM
Hi Lisa,
I guess you want to change the value of a bound column,in the button click in view mode.In order to access bound columns you can use the table cell to get each column cell by the Unique Name of that column.Please try the below code snippet.Let me know if any concern.
C#:
Thanks,
Princy
I guess you want to change the value of a bound column,in the button click in view mode.In order to access bound columns you can use the table cell to get each column cell by the Unique Name of that column.Please try the below code snippet.Let me know if any concern.
C#:
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.Items)
{
TableCell cell = item[
"emp"
];
//access boundcolumn
cell.Text =
"Text3"
;
}
}
Thanks,
Princy
0
Lisa
Top achievements
Rank 1
answered on 22 Jul 2013, 03:31 PM
Please note that the column is inside a FormTemplate:
<FormTemplate>
<FormTemplate>
<telerik:RadTextBox id="emp" runat="server" Text='<%# Bind("emp") %>' />
So,
So,
TableCell cell = item[
"emp"
]; will give error.
How can I access the "emp" RadTextBox inside the FormTemplate?
0
Princy
Top achievements
Rank 2
answered on 23 Jul 2013, 03:39 AM
Hi Lisa,
Please try the below code snippet to access a textbox inside FormTemplate in edit mode.
ASPX:
C#:
Hope this helps you.Let me know if any concern.
Thanks,
Princy
Please try the below code snippet to access a textbox inside FormTemplate in edit mode.
ASPX:
<
EditFormSettings
EditFormType
=
"Template"
>
<
FormTemplate
>
<
telerik:RadTextBox
ID
=
"emp"
runat
=
"server"
Text='<%# Bind("emp") %>' />
</
FormTemplate
>
</
EditFormSettings
>
. . . . . . . . . . . . . . . . . . . . . .
<
telerik:RadButton
runat
=
"server"
ID
=
"RadButton1"
AutoPostBack
=
"true"
Text
=
"Fill Data"
ButtonType
=
"LinkButton"
OnClick
=
"RadButton1_Click"
>
</
telerik:RadButton
>
C#:
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.EditItems)
{
RadTextBox txt = item.EditFormItem.FindControl(
"emp"
)
as
RadTextBox;
//Accessing RadTextBox in FormTemplate in EditMode
txt.Text =
"Your Text"
;
}
}
Hope this helps you.Let me know if any concern.
Thanks,
Princy
0
Lisa
Top achievements
Rank 1
answered on 23 Jul 2013, 03:27 PM
It can find the control now. But because IsItemInserted was set to true, it displays 2 items: 2 "emp" text boxes, one with old text and the other with new text. I do not want it to display 2 items. I just want it to display only one item and replace the old text with the new text for that one item. How can that be done?
If you review my previous posts, originally I tried the following and it also displayed 2 items:
GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
RadTextBox r = (RadTextBox)i.FindControl("emp");
r.Text = "Johnny";
protected void page_load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (data1.Tables[0].Rows.Count < 1)
{
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid1.Rebind();
}
else
RadGrid1.MasterTableView.IsItemInserted = false;
}
}
If you review my previous posts, originally I tried the following and it also displayed 2 items:
GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
RadTextBox r = (RadTextBox)i.FindControl("emp");
r.Text = "Johnny";
0
Princy
Top achievements
Rank 2
answered on 24 Jul 2013, 09:17 AM
Hi Lisa,
I guess the two Textbox is shown because, the insert and edit mode of the RadGrid is open. Insert form is shown because of the code inside page load [You are populating the RadGrid with a dataset 'data1' in NeedDataSource event. Since the Page_load event fires before NeedDataSource, it will always give the condition 'if(data1.Tables[0].Rows.Count < 1)' as "true" and grid will be in insert mode even though record is there]. Edit form is shown because you are clicking edit button before clicking the RadButton outside Radgrid.
But I can confirm it only after knowing where you are populating the 'data1' dataset. If you are populating in NeedDataSource event check the row count in PreRender event as follows.
C#:
Please explain more about the scenario and attach a screenshot of the issue if it doesn't help.
Thanks,
Princy.
I guess the two Textbox is shown because, the insert and edit mode of the RadGrid is open. Insert form is shown because of the code inside page load [You are populating the RadGrid with a dataset 'data1' in NeedDataSource event. Since the Page_load event fires before NeedDataSource, it will always give the condition 'if(data1.Tables[0].Rows.Count < 1)' as "true" and grid will be in insert mode even though record is there]. Edit form is shown because you are clicking edit button before clicking the RadButton outside Radgrid.
But I can confirm it only after knowing where you are populating the 'data1' dataset. If you are populating in NeedDataSource event check the row count in PreRender event as follows.
C#:
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
if
(RadGrid1.Items.Count < 1) //Checking the row count
{
RadGrid1.MasterTableView.IsItemInserted =
true
;
RadGrid1.Rebind();
}
else
{
RadGrid1.MasterTableView.IsItemInserted =
false
;
}
}
}
Please explain more about the scenario and attach a screenshot of the issue if it doesn't help.
Thanks,
Princy.
0
Lisa
Top achievements
Rank 1
answered on 24 Jul 2013, 04:41 PM
"Edit form is shown because you are clicking edit button before clicking the RadButton outside Radgrid."
I didn't click any edit button. Inside the Radgrid, there is:
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCmd" HeaderText="Edit" Visible="false" />
Is that the edit button you are referring to? It's not visible.
When the page is first loaded, it displays the RadButton outside Radgrid and the RadTextBox inside the Radgrid.
I then click the RadButton and it displays 2 text boxes instead of 1.
Here are the codes for the data1 and RadGrid1_NeedDataSource and RadGrid1_PreRender (which doesn't do anything).
I didn't click any edit button. Inside the Radgrid, there is:
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCmd" HeaderText="Edit" Visible="false" />
Is that the edit button you are referring to? It's not visible.
When the page is first loaded, it displays the RadButton outside Radgrid and the RadTextBox inside the Radgrid.
I then click the RadButton and it displays 2 text boxes instead of 1.
Here are the codes for the data1 and RadGrid1_NeedDataSource and RadGrid1_PreRender (which doesn't do anything).
public DataSet data1
{
get
{
//if there is a session value, return that value.
//otherwise retrieve and return data from database.
...
}
}
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = this.data1;
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
}
protected void page_load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (data1.Tables[0].Rows.Count < 1)
{
RadGrid1.MasterTableView.IsItemInserted = true;
RadGrid1.Rebind();
}
else
RadGrid1.MasterTableView.IsItemInserted = false;
}
}
0
Princy
Top achievements
Rank 2
answered on 25 Jul 2013, 06:52 AM
Hi Lisa,
Unfortunately,I'm not able to replicate the issue.Such a scenario is not expected.I'm not clear about how you are getting two textbox to appear.Can u please provide your complete code,C# and ASPX,along with a screenshot of the issue you are facing,so that I may be clear about your scenario .
Thanks,
Princy
Unfortunately,I'm not able to replicate the issue.Such a scenario is not expected.I'm not clear about how you are getting two textbox to appear.Can u please provide your complete code,C# and ASPX,along with a screenshot of the issue you are facing,so that I may be clear about your scenario .
Thanks,
Princy