THis is my aspx code its is a RadCombobox in a Radgrid form template:
<
EditFormSettings EditFormType="Template">
<EditColumn CancelImageUrl="../App_Themes/Expert/Grid/Cancel.gif" EditImageUrl="../App_Themes/Expert/Grid/Edit.gif" InsertImageUrl="../App_Themes/Expert/Grid/Update.gif" UpdateImageUrl="../App_Themes/Expert/Grid/Update.gif" ButtonType="ImageButton" UniqueName="EditCommandColumn1" ></EditColumn>
<FormTemplate>
<div>
<label class="gridlabel">Completed By:</label>
<telerik:RadComboBox ID="txtCompleteBy" runat="server" AccessKey="m" EmptyMessage="Key Worker" EnableEmbeddedSkins="False" Skin="Expert" Width="225px">
<CollapseAnimation Duration="200" Type="OutQuint" />
</telerik:RadComboBox>
</div>
</FormTemplate>
</EditFormSettings>
How do I fill this from code behind I have the data classes already:
Dim Keyworker_lkp As New clsExpert
Dim Keyworker_dt As DataTable = Keyworker_lkp.PractitionerLookup(Membership.GetUser().ProviderUserKey)
I've searched the forums but nothing seems to fit.
Andy
10 Answers, 1 is accepted
You can access the combobox and then set its datasource from the code behind as shown below:
c#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridEditFormItem && e.Item.IsInEditMode) |
{ |
GridEditFormItem editFormItem = (GridEditFormItem)e.Item; |
RadComboBox txtCompleteBy = (RadComboBox)editFormItem.FindControl("txtCompleteBy"); |
txtCompleteBy.DataSource = Keyworker_dt; |
} |
} |
Thanks
Princy.
One last thing, how would I set the selected value to the id in my database
Andy
In the mark-up for your RadComboBox, just add:
SelectedValue='<%#Bind("YourDataField") %>' |
I hope this helps.
Best Regards,
Tsvetoslav
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
It errors with a message that says that you can only use eval, bind for databound controls, I'm doing my databinding in code behind.
Andy
Try setting the AppendDataBoundItems property of the RadComboBox to true. Also set the SelectedValue of the ComboBox from the code behind. Here I have set the required DataField as the DataKeyName and set the SelectedValue of the ComboBox with key value.
Give a try with the following approach and see if it helps.
ASPX:
<MasterTableView DataKeyNames="ProductName" > |
<EditFormSettings EditFormType="Template"> |
<FormTemplate> |
<telerik:RadComboBox ID="RadComboBox2" AppendDataBoundItems="true" runat="server"> |
</telerik:RadComboBox> |
</FormTemplate> |
</EditFormSettings> |
CS:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
{ |
if((e.Item is GridEditFormItem)&&(e.Item.IsInEditMode)) |
{ |
GridEditFormItem editform = (GridEditFormItem)e.Item; |
RadComboBox combo = (RadComboBox)editform.FindControl("RadComboBox2"); |
//bind the combobox here |
// combo.DataSource= |
//set the selected value with key name |
combo.SelectedValue = editform.GetDataKeyValue("ProductName").ToString(); |
} |
} |
Thanks
Shinu
Even if you are binding your combo in code behind, the binding expression in the mark-up should work as expected. The only thing to take into consideration is that you should use the ItemCreated event for the assignment of the combo's datasource property.
For your convenience, I am sending you a small sample demonstrating how to do that.
Please, review it for comparison with your implementation and let us know if we can help you further.
Sincerely yours,
Tsvetoslav
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
I Still cant get this working.
This is the markup fo the radcombo:
<label class="gridlabel">Observed By:</label>
<telerik:RadComboBox ID="txtObservedBy" runat="server" EmptyMessage="Key Worker" EnableEmbeddedSkins="False" Skin="Expert" Width="225px" SelectedValue='<%#Bind("Practitioner_ID") %>'>
<CollapseAnimation Duration="200" Type="OutQuint" />
</telerik:RadComboBox>
This is my code behind: I also get the same error if I use the ItemDataBound event
Protected Sub rgProfileMain_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgProfileMain.ItemCreated
'fill edit form dropdown list
If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
Dim whichGrid As String = e.Item.OwnerTableView.Name
Select Case whichGrid
Case "Profile"
Dim editFormItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
Dim txtCompleteBy As RadComboBox = DirectCast(editFormItem.FindControl("txtCompleteBy"), RadComboBox)
Dim Keyworker_lkp As New clsExpert
Dim Keyworker_dt As DataTable = Keyworker_lkp.GetPractitionerByProvider(Membership.GetUser().ProviderUserKey)
If Keyworker_dt.Rows.Count > 0 Then
txtCompleteBy.AllowCustomText = True
txtCompleteBy.DataSource = Keyworker_dt
txtCompleteBy.DataValueField = "Practitioner_ID"
txtCompleteBy.DataTextField = "Practitioner_Name"
txtCompleteBy.DataBind()
End If
Case "Observations"
Dim editFormItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
Dim txtObservedBy As RadComboBox = DirectCast(editFormItem.FindControl("txtObservedBy"), RadComboBox)
Dim Keyworker_lkp As New clsExpert
Dim Keyworker_dt As DataTable = Keyworker_lkp.GetPractitionerByProvider(Membership.GetUser().ProviderUserKey)
If Keyworker_dt.Rows.Count > 0 Then
txtObservedBy.AllowCustomText = True
txtObservedBy.DataSource = Keyworker_dt
txtObservedBy.DataValueField = "Practitioner_ID"
txtObservedBy.DataTextField = "Practitioner_Name"
txtObservedBy.DataBind()
End If
End Select
End If
This is the error I get:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Should I use a sqldatasource like you and not try and do it from code behind.
Appreciating your help
Andy
Please, note that you should not call the combo's DataBind() method in the ItemCreated event, just remove: txtCompleteBy.DataBind() from your code and everything should work fine. The combo will be automatically bound.
Do let us know if the problem persists.
Best Regards,
Tsvetoslav
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Andy
Image 1
Open the "Open Property Builder" and the select the master table... and Select the "columns" from the below list and then in the rigth side add a new dropdown. And then put a nice header and then goto the Data Section and now put as shown in the image.
image 2 for RadCombo Data Properties in RadGrid / DataFields