Is there a way to change the value of the radinput after the value is bound in this example:
<EditItemTemplate> |
<telerik:RadTextBox ID="numberTextBox" runat="server" Text='<%# Bind("Number") %>' |
MaxLength="5"> |
</telerik:RadTextBox> |
</EditItemTemplate> |
For instance, the number in the database being bound to this control is in degrees C. There is a session variable I set that indicates if the number should be converted to degrees F before being displayed. How would I convert the value of the number and have that value displayed in the radtextbox?
(obviously, I know the C to F formula. I just don't know how to call that code using the Bind or Eval command)
Thanks!
6 Answers, 1 is accepted
0
Hi harry,
You can use ItemDataBound event handler. The ItemDataBound event is raised when an item is bound to data in the RadGrid control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the item, whenever this event occurs.
Here is an example:
.aspx
.cs
Regards,
Plamen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
You can use ItemDataBound event handler. The ItemDataBound event is raised when an item is bound to data in the RadGrid control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the item, whenever this event occurs.
Here is an example:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title>Untitled Page</title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<asp:ScriptManager ID="ScriptManager1" runat="server" /> |
<div> |
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource"> |
<MasterTableView AutoGenerateColumns="false"> |
<Columns> |
<telerik:GridTemplateColumn HeaderText="Column_0" UniqueName="Column_0"> |
<ItemTemplate> |
<%# Eval("Column_0") %> |
</ItemTemplate> |
<EditItemTemplate> |
<telerik:RadTextBox |
ID="numberTextBox" |
runat="server" |
Text='<%# Bind("Column_0") %>' |
MaxLength="5"> |
</telerik:RadTextBox> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridEditCommandColumn> |
</telerik:GridEditCommandColumn> |
</Columns> |
</MasterTableView> |
</telerik:RadGrid> |
</div> |
</form> |
</body> |
</html> |
.cs
using System; |
using System.Data; |
using System.Configuration; |
using System.Web; |
using System.Web.Security; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using System.Web.UI.WebControls.WebParts; |
using System.Web.UI.HtmlControls; |
using Telerik.Web.UI; |
public partial class _Default : System.Web.UI.Page |
{ |
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
{ |
if ((e.Item is GridEditFormItem) && e.Item.IsInEditMode) |
{ |
GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item; |
RadTextBox radTextBox = (RadTextBox)gridEditFormItem["Column_0"].FindControl("numberTextBox"); |
//C or F formula goes here |
radTextBox.Text = "C or F formula goes here"; |
} |
} |
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
{ |
DataTable dataTable = new DataTable(); |
dataTable.Columns.Add("Column_0"); |
dataTable.Rows.Add(0); |
dataTable.Rows.Add(1); |
dataTable.Rows.Add(2); |
RadGrid1.DataSource = dataTable; |
} |
} |
Regards,
Plamen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Harry
Top achievements
Rank 1
answered on 21 Aug 2008, 03:28 PM
Hmm..the radtextbox.text value is null throughout the CS code below. I see this exception in the watch list:
+ base {"FormColumns are only available when EditFormType is GridEditFormType.AutoGenerated"} System.Exception {Telerik.Web.UI.GridException}
Did I do something wrong?
+ base {"FormColumns are only available when EditFormType is GridEditFormType.AutoGenerated"} System.Exception {Telerik.Web.UI.GridException}
Did I do something wrong?
0
Hello Harry,
Is it possible that you attempt to remove some of the editors from the other grid columns or do not reference properly the RadTextBox instance inside the edit template? Here is the description for the FormColumns property referenced from the server API of RadGrid:
http://www.telerik.com/help/aspnet-ajax/telerik.web.ui-telerik.web.ui.grideditformitem-formcolumns.html
You may also try the following modification inside the ItemDataBound handler (since the GridEditableItem is NamingContainer):
Best regards,
Stephen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Is it possible that you attempt to remove some of the editors from the other grid columns or do not reference properly the RadTextBox instance inside the edit template? Here is the description for the FormColumns property referenced from the server API of RadGrid:
http://www.telerik.com/help/aspnet-ajax/telerik.web.ui-telerik.web.ui.grideditformitem-formcolumns.html
You may also try the following modification inside the ItemDataBound handler (since the GridEditableItem is NamingContainer):
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
{ |
if ((e.Item is GridEditableItem) && e.Item.IsInEditMode) |
{ |
GridEditableItem gridEditFormItem = (GridEditableItem)e.Item; |
RadTextBox radTextBox = (RadTextBox)gridEditFormItem.FindControl("numberTextBox"); |
//C or F formula goes here |
radTextBox.Text = "C or F formula goes here"; |
} |
} |
Best regards,
Stephen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Harry
Top achievements
Rank 1
answered on 25 Aug 2008, 01:40 PM
So, does that mean I have to be using the auto-generated edit form?
0
Harry
Top achievements
Rank 1
answered on 25 Aug 2008, 01:46 PM
Wait, ignore that last post. The new code you sent over worked! I modified it just slightly to work with the RadNumericTextBox and it worked like a charm.
Thank you!
Thank you!
0
Hi Harry,
I suppose that you are using the auto-generated grid edit form since you embedded RadTextBox inside edit template of a template column. Thus the RadTextBox editor will become part of the edit form item.
Have you tried the modification from my previous reply? Can you please specify whether you remove some of the default column editors with your custom code? Let me know what your findings are.
Best regards,
Stephen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
I suppose that you are using the auto-generated grid edit form since you embedded RadTextBox inside edit template of a template column. Thus the RadTextBox editor will become part of the edit form item.
Have you tried the modification from my previous reply? Can you please specify whether you remove some of the default column editors with your custom code? Let me know what your findings are.
Best regards,
Stephen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.