http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/templateformupdate/defaultcs.aspx
how can I preload "Title Of Courtesy" combobox from code behind at all and also
considering the fact that in my case it is a web user control ? Actually in my case user control is a grid wrapped in user control
but I did DataSource and DataBind interface for it, so it works like a regular bindable control in that respect ).
The values in my user control are different depending on EmployeeId and that is why I need to bind it based on row.
What I am looking to do is something like:
UserControl.DataSource=Business.Employees.GetContactInfoAsList(EpmployeeId)
but having trouble figuring out which event to use to get a hold of usercontrol on add new row click, also for existing records
I am trying to use RadGrid1_DataBound but item.FindControl (UserControlId) returns NULL when
I do foreach on grid items.
Thank you.
7 Answers, 1 is accepted
ok I got it working. So to load some bindable list control(builtin or user control) located in Form template
with values from business object in code behind when "Add New record" is clicked
(in the telerik example above) :
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item is GridEditFormItem && e.Item.IsInEditMode) { GridEditFormItem editForm = e.Item as GridEditFormItem; RadListBox list = editForm.FindControl("listBoxIndustries") as RadListBox; list.DataSource = BusinessService.LoadIndustriesLookupList(); list.DataBind(); } }My
<EditFormSettings EditFormType="Template">
<FormTemplate>
<table cellspacing="1" class="style1" width="50%">
<tr>
<td width="20%">
<b>User Details</b>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
Network Name:
</td>
<td class="style2">
<asp:TextBox ID="TxtNetworkName" runat="server" Text='<%# Bind("NetworkName") %>' TabIndex="1">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
First Name:
</td>
<td class="style2">
<asp:TextBox ID="TxtFirstName" runat="server" Text='<%# Bind("FirstName") %>' TabIndex="2" >
</asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Last Name:
</td>
<td class="style2">
<asp:TextBox ID="TxtLastName" runat="server" Text='<%# Bind("LastName") %>' TabIndex="3">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Phone:
</td>
<td class="style2">
<telerik:RadMaskedTextBox ID="TxtPhone"
runat="server" SelectionOnFocus="SelectAll"
Text='<%# Bind("Phone") %>' PromptChar="_"
Width="120px" Mask="(###) ###-####"
TabIndex="4" LabelWidth="">
</telerik:RadMaskedTextBox>
</td>
</tr>
<tr>
<td class="style2">
Email:
</td>
<td class="style2">
<asp:TextBox ID="TxtEmail" Text='<%# Bind("Email") %>'
runat="server" Width="180px"></asp:TextBox>
<asp:RegularExpressionValidator ID="emailValidator"
runat="server" Display="Dynamic"
ErrorMessage="Please, enter valid e-mail address." ValidationExpression="^\s*(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*\s*$"
ControlToValidate="TxtEmail" TabIndex="5" ForeColor="Red"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style2">
Active:
</td>
<td class="style2">
<asp:CheckBox ID="chkUserActive" runat="server" Text='<%# Bind("UserActive") %>' TabIndex="6" />
</td>
</tr>
<tr>
<td class="style2">
User Group:
</td>
<td class="style2">
<asp:DropDownList ID="ddlUsergroup" runat="server" SelectedValue='<%# Bind("Usergroup") %>'
DataSource='<%# (New string() { "AdminUser", "MainUser", "ReadonlyUser"}) %>' TabIndex="7"
AppendDataBoundItems="True">
<asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td align="left" class="style2">
<asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'>
</asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
and in my VB page, I use the
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
If (Not Page.IsPostBack) Then
RadGrid1.EditIndexes.Add(0)
RadGrid1.Rebind()
End If
End Sub
But I got error when I run it
'ddlUsergroup' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Please help
Thanks. Your demo works fine in my said. As you see, I just change the table in the FormTemplate. It is not work. Maybe I misss some reference? What references you have? The add new is fine and the edit can't open after I commment the lines inside of
RadGrid1_PreRender. Also how can I put a checkbox in the FormTemplate. There is no checkbox in your demo. I put one in my samp[le, but not works when I try to add a new record.
I'm sorry i couldn't replicate the issue,the references are same as that in the example.
to add a checkbox,you can directly drag and drop a checkbox into the form template,where you want it.
For further reference,I'm attaching my full code where i tried. You can use the same code behind from the demo.
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditFormTemplate.aspx.cs" Inherits="Radgrid2_EditFormTemplate" %><%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080"> </asp:Label> <asp:Label ID="Label2" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000"> </asp:Label> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <telerik:RadGrid ID="RadGrid1" runat="server" CssClass="RadGrid" GridLines="None" AllowPaging="True" PageSize="20" AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="true" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" DataSourceID="SqlDataSource1" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted" OnItemUpdated="RadGrid1_ItemUpdated" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender"> <MasterTableView CommandItemDisplay="TopAndBottom" DataSourceID="SqlDataSource1" DataKeyNames="EmployeeID"> <Columns> <telerik:GridEditCommandColumn> </telerik:GridEditCommandColumn> <telerik:GridBoundColumn UniqueName="EmployeeID" HeaderText="ID" DataField="EmployeeID"> <HeaderStyle ForeColor="Silver" Width="20px"></HeaderStyle> <ItemStyle ForeColor="Gray"></ItemStyle> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="TitleOfCourtesy" HeaderText="TOC" DataField="TitleOfCourtesy"> <HeaderStyle Width="60px"></HeaderStyle> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="FirstName" HeaderText="FirstName" DataField="FirstName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="LastName" HeaderText="LastName" DataField="LastName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="HireDate" HeaderText="Hire Date" DataField="HireDate" DataFormatString="{0:d}"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Title" HeaderText="Title" DataField="Title"> </telerik:GridBoundColumn> <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column"> </telerik:GridButtonColumn> </Columns> <EditFormSettings EditFormType="Template"> <FormTemplate> <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none" style="border-collapse: collapse;"> <tr class="EditFormHeader"> <td colspan="2" style="font-size: small"> <b>Employee Details</b> </td> </tr> <tr> <td colspan="2"> <b>Company Info:</b> </td> </tr> <tr> <td> <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module"> <tr> <td> </td> <td> </td> </tr> <tr> <td> Country: </td> <td> <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Country") %>'> </asp:TextBox> </td> </tr> <tr> <td> City: </td> <td> <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("City") %>' TabIndex="1"> </asp:TextBox> </td> </tr> <tr> <td> Region: </td> <td> <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Region") %>' TabIndex="2"> </asp:TextBox> </td> </tr> <tr> <td> Home Phone: </td> <td> <telerik:RadMaskedTextBox ID="HomePhoneBox" runat="server" SelectionOnFocus="SelectAll" Text='<%# Bind("HomePhone") %>' PromptChar="_" Width="300px" Mask="(###) ###-####" TabIndex="3"> </telerik:RadMaskedTextBox> </td> </tr> <tr> <td> Birth Date: </td> <td> <telerik:RadDatePicker ID="BirthDatePicker" runat="server" MinDate="1/1/1900" DbSelectedDate='<%# Bind("BirthDate") %>' TabIndex="4"> </telerik:RadDatePicker> </td> </tr> <tr> <td> Title Of Courtesy </td> <td> <asp:DropDownList ID="ddlTOC" runat="server" SelectedValue='<%# Bind("TitleOfCourtesy") %>' DataSource='<%# (new string[] { "Dr.", "Mr.", "Mrs.", "Ms." }) %>' TabIndex="7" AppendDataBoundItems="True"> <asp:ListItem Selected="True" Text="Select" Value=""> </asp:ListItem> </asp:DropDownList> </td> </tr> </table> </td> <td> <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0" class="module"> <tr> <td> Notes: </td> </tr> <tr> <td> <asp:TextBox ID="TextBox1" Text='<%# Bind("Notes") %>' runat="server" TextMode="MultiLine" Rows="5" Columns="40" TabIndex="5"> </asp:TextBox> </td> </tr> <tr> <td> Address: </td> </tr> <tr> <td> <asp:TextBox ID="TextBox6" Text='<%# Bind("Address") %>' runat="server" TextMode="MultiLine" Rows="2" Columns="40" TabIndex="6"> </asp:TextBox> </td> </tr> </table> </td> </tr> <tr> <td colspan="2"> <b>Personal Info:</b> </td> </tr> <tr> <td> <table id="Table4" cellspacing="1" cellpadding="1" width="250" border="0" class="module"> <tr> <td> FirstName: </td> <td> <asp:TextBox ID="TextBox2" Text='<%# Bind( "FirstName") %>' runat="server" TabIndex="8"> </asp:TextBox> </td> </tr> <tr> <td> Last Name: </td> <td> <asp:TextBox ID="TextBox3" Text='<%# Bind( "LastName") %>' runat="server" TabIndex="9"> </asp:TextBox> </td> </tr> <tr> <td> Hire Date: </td> <td> <telerik:RadDatePicker ID="HireDatePicker" DbSelectedDate='<%# Bind( "HireDate") %>' runat="server" TabIndex="10"> </telerik:RadDatePicker> </td> </tr> <tr> <td> Title: </td> <td> <asp:TextBox ID="TextBox4" Text='<%# Bind( "Title") %>' runat="server" TabIndex="11"> </asp:TextBox> </td> </tr> <tr> <td>Check:</td> <td> <asp:CheckBox ID="CheckBox1" runat="server" /> </td> </tr> </table> </td> <td> </td> </tr> <tr> <td align="right" colspan="2"> <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'> </asp:Button> <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button> </td> </tr> </table> </FormTemplate> </EditFormSettings> </MasterTableView> <ClientSettings> <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents> </ClientSettings> </telerik:RadGrid> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>" DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @EmployeeID" InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [Country], [HomePhone], [Notes], [ReportsTo]) VALUES (@LastName, @FirstName, @Title, @TitleOfCourtesy, @BirthDate, @HireDate, @Address, @City, @Region, @Country, @HomePhone, @Notes, @ReportsTo)" SelectCommand="SELECT * FROM [Employees]" UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title, [TitleOfCourtesy] = @TitleOfCourtesy, [BirthDate] = @BirthDate, [HireDate] = @HireDate, [Address] = @Address, [City] = @City, [Region] = @Region, [Country] = @Country, [HomePhone] = @HomePhone, [Notes] = @Notes WHERE [EmployeeID] = @EmployeeID"> <DeleteParameters> <asp:Parameter Name="EmployeeID" Type="Int32"></asp:Parameter> </DeleteParameters> <InsertParameters> <asp:Parameter Name="LastName" Type="String"></asp:Parameter> <asp:Parameter Name="FirstName" Type="String"></asp:Parameter> <asp:Parameter Name="Title" Type="String"></asp:Parameter> <asp:Parameter Name="TitleOfCourtesy" Type="String"></asp:Parameter> <asp:Parameter Name="BirthDate" Type="DateTime"></asp:Parameter> <asp:Parameter Name="HireDate" Type="DateTime"></asp:Parameter> <asp:Parameter Name="Address" Type="String"></asp:Parameter> <asp:Parameter Name="City" Type="String"></asp:Parameter> <asp:Parameter Name="Region" Type="String"></asp:Parameter> <asp:Parameter Name="Country" Type="String"></asp:Parameter> <asp:Parameter Name="HomePhone" Type="String"></asp:Parameter> <asp:Parameter Name="Notes" Type="String"></asp:Parameter> <asp:Parameter Name="ReportsTo" Type="Int32" DefaultValue=""></asp:Parameter> </InsertParameters> <UpdateParameters> <asp:Parameter Name="LastName" Type="String"></asp:Parameter> <asp:Parameter Name="FirstName" Type="String"></asp:Parameter> <asp:Parameter Name="Title" Type="String"></asp:Parameter> <asp:Parameter Name="TitleOfCourtesy" Type="String"></asp:Parameter> <asp:Parameter Name="BirthDate" Type="DateTime"></asp:Parameter> <asp:Parameter Name="HireDate" Type="DateTime"></asp:Parameter> <asp:Parameter Name="Address" Type="String"></asp:Parameter> <asp:Parameter Name="City" Type="String"></asp:Parameter> <asp:Parameter Name="Region" Type="String"></asp:Parameter> <asp:Parameter Name="Country" Type="String"></asp:Parameter> <asp:Parameter Name="HomePhone" Type="String"></asp:Parameter> <asp:Parameter Name="Notes" Type="String"></asp:Parameter> <asp:Parameter Name="EmployeeID" Type="Int32"></asp:Parameter> </UpdateParameters> </asp:SqlDataSource> </div> </form></body></html>Thanks,
Princy
I copy your code and just change some items. I also use your
Protected
Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
If (Not Page.IsPostBack) Then
RadGrid1.EditIndexes.Add(0)
RadGrid1.Rebind()
End If
End Sub
I got error when I run.
'ddlUsergroup' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value '
If I comment the lines inside of RadGrid1_PreRender the RadGrid1 works fine. How?
The reason for the error you are seeing is that SelectedValue for the DropDownList ddlUserGroup does not exist in the list of items.
As the DropDownList is in an edit template for the RadGrid and Bind() is used to set the SelectedValue the initial value for the DropDownList will be extracted from the RadGrid's data source. You need to ensure that all distinct values from RadGrid's datasource for the column in which the dropdown control resides matches the values of the items of the DropDownList's DataSource.
You would find a sample project where the approach is illustrated. It is working as expected on my side. Notice the definition for the DropDownList in the EditForm template for the RadGrid.
I hope this information would help you solve the issue.
Regards,
Viktor Tachev
Telerik
