I'm using Edit Form to insert/edit items. The problem I'm running into is when I try to edit a row, it's using the same form and some of the controls are not set to what data item for that grid item actually has. When in the life cycle can I set selected values for my RadComboBoxes and RadNumericTextBoxes, any code examples?
4 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 13 Mar 2012, 06:09 AM
Hello,
You can access the controls in ItemDataBound event as shown below.
C#:
Also take a look at the following demo.
Grid / Form Template Edit Form
-Shinu.
You can access the controls in ItemDataBound event as shown below.
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem itm = (GridEditableItem)e.Item;
RadNumericTextBox txt = (RadNumericTextBox)itm.FindControl(
"RadNumericTextBox1"
);
}
}
Grid / Form Template Edit Form
-Shinu.
0

Dom
Top achievements
Rank 1
answered on 13 Mar 2012, 05:04 PM
I'm still having troubles accessing control values, this time it's during update.
Here's markup for my edit form
Here's markup for my edit form
<
EditFormSettings
EditFormType
=
"Template"
>
<
FormTemplate
>
<
div
class
=
"EditFormDiv"
>
<
manatron:RadComboBox
runat
=
"server"
ID
=
"NewTag"
Label
=
"TAG:"
Width
=
"50%"
DataSource="<%# Ctl.TaxAuthorityGroups %>"
DataTextField="Descr" DataValueField="Id" />
<
manatron:RadNumericTextBox
runat
=
"server"
ID
=
"NewRate"
Label
=
"Rate:"
DataType
=
"System.decimal"
EmptyMessage
=
"0"
NumberFormat-DecimalDigits
=
"8"
/>
<
div
style
=
"text-align: right; display: inline"
>
<
manatron:RadButton
ID
=
"AddRate"
runat
=
"server"
Text
=
"Apply"
CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsertRate" : "UpdateRate" %>' />
<
manatron:RadButton
ID
=
"CancelRate"
runat
=
"server"
Text
=
"Cancel"
CommandName
=
"Cancel"
/>
</
div
>
</
div
>
</
FormTemplate
>
</
EditFormSettings
>
and server-side code
protected
void
TimberTaxMaintGridItemCommand(
object
sender, GridCommandEventArgs e)
{
switch
(e.CommandName)
{
case
GridCommands.PerformInsertTafCommandName:
PerformInsertTaf( e );
break
;
case
GridCommands.PerformInsertRateCommandName:
PerformInsertRate( e );
break
;
case
GridCommands.CopyRateCommandName:
CopyRate( e );
break
;
case
GridCommands.EditRateCommandName:
//Since we use Form Template to insert/edit we need to know when we're editing to handle user clicking Apply differently
_editingRate =
true
;
EditRate( e );
break
;
case
GridCommands.UpdateRateCommandName:
UpdateRate( e );
break
;
case
GridCommands.DeleteTafCommandName:
break
;
case
GridCommands.DeleteRateCommandName:
break
;
}
}
protected
void
TimberTaxMaintGridItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item.OwnerTableView.Name ==
"TAGRates"
&& e.Item
is
GridEditableItem && e.Item.IsInEditMode && _editingRate)
{
var item = ( GridEditableItem ) e.Item;
var tagControl = ( RadComboBox ) item.FindControl(
"NewTag"
);
var rateControl = ( RadNumericTextBox )item.FindControl(
"NewRate"
);
var applyControl = ( RadButton ) item.FindControl(
"AddRate"
);
if
(tagControl ==
null
|| rateControl ==
null
|| applyControl ==
null
)
{
Ctl.Messages.Add(
"Unexpected error occured while editing TAG rate."
, MessageType.Error );
return
;
}
var dataItem = ( TimberTaxYieldRate ) item.DataItem;
tagControl.SelectedValue = dataItem.TagId.ToString( CultureInfo.InvariantCulture );
rateControl.Value = Convert.ToDouble( dataItem.Rate );
}
}
private
void
EditRate(GridCommandEventArgs e)
{
if
( e.Item.OwnerTableView.ChildSelectedItems.Count != 1 )
{
Ctl.Messages.Add(
"Please select 1 TAG Rate to edit."
, MessageType.Error );
return
;
}
var selectedItem = ( GridDataItem ) e.Item.OwnerTableView.ChildSelectedItems[ 0 ];
selectedItem.FireCommandEvent( RadGrid.EditCommandName,
null
);
}
private
void
UpdateRate(GridCommandEventArgs e)
{
var item = ( GridEditableItem ) e.Item;
var tagControl = ( RadComboBox ) item.FindControl(
"NewTag"
);
var rateControl = ( RadNumericTextBox ) item.FindControl(
"NewRate"
);
if
(tagControl ==
null
|| rateControl ==
null
)
return
;
var rate = rateControl.Value ==
null
? 0m : Convert.ToDecimal(rateControl.Value.Value);
var tagId = Convert.ToInt32(tagControl.SelectedValue);
var tag = Ctl.TaxAuthorityGroups.First(t => t.Id == tagId);
Ctl.DataSource[ item.OwnerTableView.ParentItem.DataSetIndex ].Rates[ item.DataSetIndex ].TagId = tag.Id;
Ctl.DataSource[ item.OwnerTableView.ParentItem.DataSetIndex ].Rates[ item.DataSetIndex ].TagShortDescr = tag.ShortDescr;
Ctl.DataSource[ item.OwnerTableView.ParentItem.DataSetIndex ].Rates[ item.DataSetIndex ].TagDescr = tag.Descr;
Ctl.DataSource[ item.OwnerTableView.ParentItem.DataSetIndex ].Rates[ item.DataSetIndex ].Rate = rate;
item.FireCommandEvent(RadGrid.CancelCommandName,
null
);
item.OwnerTableView.DataBind();
}
The problem I'm having now is when UpdateRate() executes both tagControl and rateControl are null. Why is that?
0

Dom
Top achievements
Rank 1
answered on 13 Mar 2012, 06:13 PM
Also, how come when I click Apply button on the edit form, e.Item passed to TimberTaxMaintGridItemCommand() is GridDataItem type, but when I click cancel it's GridEditFormItem? Shouldn't they be the same for both events?
0
Hello,
You could find both controls by accessing the item EditFormItem value in which the two controls reside as it is shown below.
The item types for the Update and Cancel are different because when updating you are working with the DataItem not the EditFormItem. When closing you are accessing the EditForm and not operating on the DataItem.
Additionally, you could go through the help article for more in depth information on how to access elements in RadGrid.
Kind regards,
Antonio Stoilkov
the Telerik team
You could find both controls by accessing the item EditFormItem value in which the two controls reside as it is shown below.
GridDataItem dataItem = e.Item
as
GridDataItem;
if
(dataItem !=
null
)
{
RadComboBox tagCombo = dataItem.EditFormItem.FindControl(
"NewTag"
)
as
RadComboBox;
RadNumericTextBox rateNumeric = dataItem.EditFormItem.FindControl(
"NewRate"
)
as
RadNumericTextBox;
}
The item types for the Update and Cancel are different because when updating you are working with the DataItem not the EditFormItem. When closing you are accessing the EditForm and not operating on the DataItem.
Additionally, you could go through the help article for more in depth information on how to access elements in RadGrid.
Kind regards,
Antonio Stoilkov
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.