I am trying to Add dynamic controls to a ASP table which is inside Editform of Radgrid
It displays ok but when i click on Done button which is also in Edit Form.I am loosing all values of dynamic controls.
Can you let me kno if there is any other way i can do this
below is a little bit of code which i am trying to use
My ASPX Page |
This is in Editform of Radgrid |
<asp:Table ID="tb2" runat="server"> |
<asp:TableHeaderRow> |
<asp:TableHeaderCell> |
<asp:Label ID="Label3" Text="Not found" runat="server"> |
</asp:Label> |
</asp:TableHeaderCell> |
</asp:TableHeaderRow> |
</asp:Table> |
Code Behind in Databound
Dim tb2 As Table = grdEItem.FindControl("tb2") |
Dim no_match_chk As New CheckBox |
Dim str1 As New Label |
str1.Text = "" |
Dim c1 As TableCell = New TableCell |
Dim r As New TableRow |
r.ID = "row1" |
r.Controls.Add(c) |
tb2.Rows.Add(r) |
When i Click Done button |
Tring to get checkbox value which i added to tb2 |
So please do let me know
Thanks
Kavya
10 Answers, 1 is accepted
DataBound event is raised only when RadGrid control is created from the dataSource and not when its control structure is recreated from the ViewState. Therefore your logic will not be executed on every postback and your dynamic controls will not be added to table's controls collection. In order to persist the controls across postback you should try using the ItemCreated event instead DataBound .
Best wishes,
Rosen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

You can also refer the following KB article.
Distinguishing the major differences between the ItemCreated and ItemDataBound server events
Thanks
Shinu.

I did try using Item created for some reasons its not getting me the value from the EditForm
Can you give me an example where i can add new controls to the existing one whic are in EditForm Template.
Thanks
Kavya
Please find attached a simple page which demonstrates a possible implementation of how to add controls (as how to set/get values from them) to RadGrid's FormTemplate on the fly.
All the best,
Rosen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.

Was able to resolve issue by using ItemCreated as you said it worked.Thank you very much
Gr8 support if anymore issues will definitely let you know
thanks
Kavya

I've added some controls dynamically to an edit form just like in the example above, and added two buttons for UpdateCommand and CancelCommand. However on item inserted event the controls are not there anymore and I cannot obtain the values.
Do you have any idea why the controls added on item created event are not persisted?
Thank you,
Victor
Could you please show us the code, you are using for adding the controls and to access them?
Kind regards,
Rosen
the Telerik team

Thanks

I ran into the same problem, let's say I dynamically add RadTextBox , and I can not get the value that I entered into this RadTextBox
<telerik:RadAjaxPanel LoadingPanelID="RadAjaxLoadingPanel_1" runat="server" ID="RadAjaxPanel_RadGrid">
<telerik:RadGrid LocalizationPath="~/DesktopModules/PIS/App_LocalResources/" runat="server" ID="RadGrid_Product" AllowPaging="true" AllowSorting="true"
AutoGenerateColumns="false" OnNeedDataSource="RadGrid_Product_OnNeedDataSource"
OnDeleteCommand="RadGrid_Product_OnDeleteCommand" OnItemCommand="RadGrid1_ItemCommand"
OnUpdateCommand="RadGrid_Product_OnUpdateCommand" OnEditCommand="RadGrid1_EditCommand"
OnInsertCommand="RadGrid_Product_OnInsertCommand" OnItemCreated="RadGrid_OnItemCreated">
<PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
<MasterTableView Width="100%" CommandItemDisplay="Top" CommandItemSettings-ShowRefreshButton="true"
DataKeyNames="ProductId" AllowFilteringByColumn="True" >
<NoRecordsTemplate>
<asp:Label ID="Label_NotRecord" ResourceKey="NotRecord.Text" runat="server"></asp:Label></NoRecordsTemplate>
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderStyle-Width="20">
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn AllowFiltering="false" HeaderStyle-Width="40px">
<ItemTemplate>
<center>
<asp:ImageButton ID="ImageButton1" OnLoad="ImageButton_GetOneCode" OnClick="ImageButton_GetOneCode_OnClick" runat ="server"/>
</center>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn AllowFiltering="false" HeaderStyle-Width="40px">
<ItemTemplate>
<center>
<asp:ImageButton ID="ViewProperties" OnLoad="ImageButton_ViewDinamicProperties" runat ="server"/>
</center>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn SortExpression="Name" DataField="Name" HeaderStyle-Width="120px"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn SortExpression="ProductCategoryName" DataField="ProductCategoryName" HeaderStyle-Width="60px"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn SortExpression="CreatedByUserName" DataField="CreatedByUserName" HeaderStyle-Width="120px"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridDateTimeColumn FilterControlWidth="130" HeaderStyle-Width="100" DataField="CreatedDate" DataFormatString="{0:dd.MM.yyyy}">
</telerik:GridDateTimeColumn>
<telerik:GridButtonColumn ConfirmDialogType="RadWindow" HeaderStyle-Width="10"
ButtonType="ImageButton" ConfirmDialogHeight="50px" ConfirmDialogWidth="350px" CommandName="Delete" ShowInEditForm="True">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings EditFormType="Template" EditColumn-ButtonType="LinkButton" EditColumn-HeaderButtonType="PushButton">
<EditColumn UniqueName="EditCommandColumnTest">
</EditColumn>
<FormTemplate >
<telerik:RadTextBox Visible="false" ID="RadTextBox_ProductId" runat="server" Text='<%# Eval("ProductId") %>' ></telerik:RadTextBox>
<telerik:RadTextBox Visible="false" ID="RadTextBox_ProductCategory" runat="server" Text='<%# Eval("ProductCategory") %>' ></telerik:RadTextBox>
<telerik:RadTextBox Visible="false" ID="RadTextBox_CreatedByUser" runat="server" Text='<%# Eval("CreatedByUser") %>' ></telerik:RadTextBox>
<telerik:RadTextBox Visible="false" ID="RadTextBox_CreatedDate" runat="server" Text='<%# Eval("CreatedDate") %>' ></telerik:RadTextBox>
<telerik:RadComboBox runat="server" Visible="false" ID="RadComboBox_SelectCategory"></telerik:RadComboBox>
<asp:PlaceHolder ID="Panel1" runat="server" >
</asp:PlaceHolder>
<%--CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert": "Update" %>'--%>
<telerik:RadButton OnClick="RadButton_Update" Font-Underline="false" ID="RadButton_InsertUpdate" runat="server" Width="70" Text='<%# (Container is GridEditFormInsertItem) ? Localization.GetString("Add.Text", this.LocalResourceFile): Localization.GetString("Save.Text", this.LocalResourceFile) %>'
Skin="Default" ValidationGroup='<%# (Container is GridEditFormInsertItem) ? "InsertGroup": "UpdateGroup" %>'>
</telerik:RadButton>
<telerik:RadButton Font-Underline="false" ID="RadButton_Cancel" runat="server" Width="50"
CommandName="Cancel" Skin="Default" CausesValidation="false" Text='<%#Localization.GetString("Cancel.Text", this.LocalResourceFile) %>' >
</telerik:RadButton>
</FormTemplate>
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
</telerik:RadAjaxPanel>
protected void RadGrid_OnItemCreated(object sender, GridItemEventArgs e)
{
if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))
{
GridEditFormItem itemEditForm = RadGrid_Product.MasterTableView.GetItems(GridItemType.EditFormItem)[0] as GridEditFormItem;
PlaceHolder item = itemEditForm.FindControl("Panel1") as PlaceHolder;
if ((bool)Session["Edit_Or_Insert"])
{
List<AddEditRadGridInfo> RadGridElementSession = new List<AddEditRadGridInfo>();
Guid IdProduct = new Guid(itemEditForm.GetDataKeyValue("ProductId").ToString());
PISController newController = new PISController();
List<PropertiesProductValueInfo> list = new List<PropertiesProductValueInfo>();
list = newController.PropertiesProductByProductId(IdProduct, ModuleId);
List<RadTextBox> listRadTextBox = new List<RadTextBox>();
List<RadDateTimePicker> listRadDateTimePicker = new List<RadDateTimePicker>();
List<Label> listLabel = new List<Label>();
List<RadAsyncUpload> listRadAsyncUpload = new List<RadAsyncUpload>();
Random newRandom = new Random();
Literal table1 = new Literal();
table1.Text = "<table>";
item.Controls.Add(table1);
for (int i = 0; i < list.Count; i++)
{
if (list[i].TypeProp == "int")
{
listRadTextBox.Add(new RadTextBox()); //создаем новый текстбокс
listLabel.Add(new Label()); //создаем новый лабел
listLabel[listLabel.Count - 1].Text = list[i].PropertiesName;
listRadTextBox[listRadTextBox.Count - 1].ID = "Id" + Convert.ToString(newRandom.Next());
listRadTextBox[listRadTextBox.Count - 1].Text = list[i].ValueProduct;
listRadTextBox[listRadTextBox.Count - 1].EnableViewState = true;
listRadTextBox[listRadTextBox.Count - 1].ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
TableLinesAdd(item, listLabel[listLabel.Count - 1], listRadTextBox[listRadTextBox.Count - 1], 1);
RadGridElementSession.Add(new AddEditRadGridInfo());
RadGridElementSession[i].Id = listRadTextBox[listRadTextBox.Count - 1].ID;
RadGridElementSession[i].PropertiesType = list[i].TypeProp;
RadGridElementSession[i].PropertiesName = list[i].PropertiesName;
RadGridElementSession[i].PropertiesId = list[i].PropertiesId;
}
if (list[i].TypeProp == "string")
{
listRadTextBox.Add(new RadTextBox()); //создаем новый текстбокс
listLabel.Add(new Label()); //создаем новый лабел
listLabel[listLabel.Count - 1].Text = list[i].PropertiesName;
listRadTextBox[listRadTextBox.Count - 1].ID = "Id" + Convert.ToString(newRandom.Next());
listRadTextBox[listRadTextBox.Count - 1].Text = list[i].ValueProduct;
listRadTextBox[listRadTextBox.Count - 1].EnableViewState = true;
listRadTextBox[listRadTextBox.Count - 1].ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
TableLinesAdd(item, listLabel[listLabel.Count - 1], listRadTextBox[listRadTextBox.Count - 1], 2);
RadGridElementSession.Add(new AddEditRadGridInfo());
RadGridElementSession[i].Id = listRadTextBox[listRadTextBox.Count - 1].ID;
RadGridElementSession[i].PropertiesType = list[i].TypeProp;
RadGridElementSession[i].PropertiesName = list[i].PropertiesName;
RadGridElementSession[i].PropertiesId = list[i].PropertiesId;
}
if (list[i].TypeProp == "Image")
{
listRadAsyncUpload.Add(new RadAsyncUpload()); //создаем новый текстбокс
listLabel.Add(new Label()); //создаем новый лабел
listLabel[listLabel.Count - 1].Text = list[i].PropertiesName;
listRadAsyncUpload[listRadAsyncUpload.Count - 1].ID = "Id" + Convert.ToString(newRandom.Next());
TableLinesAdd(item, listLabel[listLabel.Count - 1], listRadAsyncUpload[listRadAsyncUpload.Count - 1], 3);
RadGridElementSession.Add(new AddEditRadGridInfo());
RadGridElementSession[i].Id = listRadAsyncUpload[listRadAsyncUpload.Count - 1].ID;
RadGridElementSession[i].PropertiesType = list[i].TypeProp;
RadGridElementSession[i].PropertiesName = list[i].PropertiesName;
RadGridElementSession[i].PropertiesId = list[i].PropertiesId;
}
if (list[i].TypeProp == "DateTime")
{
listRadDateTimePicker.Add(new RadDateTimePicker()); //создаем новый текстбокс
listLabel.Add(new Label()); //создаем новый лабел
listLabel[listLabel.Count - 1].Text = list[i].PropertiesName;
listRadDateTimePicker[listRadDateTimePicker.Count - 1].ID = "Id" + Convert.ToString(newRandom.Next());
CultureInfo invC = CultureInfo.InvariantCulture;
listRadDateTimePicker[listRadDateTimePicker.Count - 1].SelectedDate = Convert.ToDateTime(list[i].ValueProduct, invC);
TableLinesAdd(item, listLabel[listLabel.Count - 1], listRadDateTimePicker[listRadDateTimePicker.Count - 1], 4);
RadGridElementSession.Add(new AddEditRadGridInfo());
RadGridElementSession[i].Id = listRadDateTimePicker[listRadDateTimePicker.Count - 1].ID;
RadGridElementSession[i].PropertiesType = list[i].TypeProp;
RadGridElementSession[i].PropertiesName = list[i].PropertiesName;
RadGridElementSession[i].PropertiesId = list[i].PropertiesId;
}
}
Literal table2 = new Literal();
table2.Text = "</table>";
item.Controls.Add(table2);
Session.Add("RadGridElementSession", RadGridElementSession);
//RadButton knopka1 = new RadButton();
//knopka1.Text = "Жмииии";
//knopka1.Click += new EventHandler(Editing);
//(sender as Panel).Controls.Add(knopka1);
}
}
}
public void RadButton_Update(object sender,EventArgs e)
{
GridEditFormItem item = ((sender as RadButton).NamingContainer as GridEditFormItem);
Guid ProductId = new Guid((item.FindControl("RadTextBox_ProductId") as RadTextBox).Text);
int CategoryId = Convert.ToInt32((item.FindControl("RadTextBox_ProductCategory") as RadTextBox).Text);
int CreatedByUser = Convert.ToInt32((item.FindControl("RadTextBox_CreatedByUser") as RadTextBox).Text);
DateTime CreatedDate = Convert.ToDateTime((item.FindControl("RadTextBox_CreatedDate") as RadTextBox).Text);
PISController controller = new PISController();
List<AddEditRadGridInfo> list = new List<AddEditRadGridInfo>();
list = Session["RadGridElementSession"] as List<AddEditRadGridInfo>;
for (int i = 0; i < list.Count; i++)
{
if (list[i].PropertiesType == "int")
{
string values = (item.FindControl(list[i].Id) as RadTextBox).Text;
controller.PropertiesProductEdit(list[i].PropertiesId, ModuleId, ProductId, CategoryId, values);
}
if (list[i].PropertiesType == "string")
{
string values = (item.FindControl(list[i].Id) as RadTextBox).Text;
controller.PropertiesProductEdit(list[i].PropertiesId, ModuleId, ProductId, CategoryId, values);
}
if (list[i].PropertiesType == "Image")
{
}
if (list[i].PropertiesType == "DateTime")
{
}
}
}
how do I get the value that I typed in RadTextBox?
Could you please try to get this value on ItemCommand server event using the condition if ( e.CommandName=="Update" ) and the approach demonstrated in the following topic:
( Section Accessing controls in edit/insert mode )
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html
I hope this will prove helpful.
Greetings,
Eyup
the Telerik team