This is a migrated thread and some comments may be shown as answers.

Databinding in a formView.. programmatically

3 Answers 374 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
mac
Top achievements
Rank 1
mac asked on 15 Jan 2009, 01:30 AM
hi there.. I am a little confused on how/why some of my ctrls are able to use the SelectedValue = '<%# Bind() %>' and some are not.

The one that can bind is using an ObjectDataSource within the ascx:

 

<telerik:RadComboBox ID="ComboDepartment" runat="server"   
DataSourceID="CCentres" DataTextField="CC_desc"   
DataValueField="CC_desc" SelectedValue='<%# Bind("department") %>'>   
<CollapseAnimation Duration="200" Type="OutQuint" />   
</telerik:RadComboBox> 
 
<asp:ObjectDataSource ID="CCentres" runat="server" SelectMethod="GetData" TypeName="CC2TableAdapters.Cost_centresTableAdapter">   
<SelectParameters>   
<asp:SessionParameter DefaultValue="1" Name="Empid" SessionField="empid" Type="String" />   
</SelectParameters>   
</asp:ObjectDataSource>   
 

 

the one that is not working is the following 

 

 

 

 

<telerik:RadComboBox ID="ComboStatus" SelectedValue='<%#Bind("status") %>' runat="server">   
<CollapseAnimation Duration="200" Type="OutQuint" />   
 </telerik:RadComboBox> 
 

 

 

the combo status is being built using the following code from within the formview_onload Event.

 

Protected Sub FormView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.Load  
 
 If FormView1.CurrentMode = FormViewMode.Insert And Not IsPostBack Then  
 
Dim Status As RadComboBox = FormView1.FindControl("ComboStatus")  
 
Status.DataSource = functions.NameValues("status")  
 
Status.Items.Add(New RadComboBoxItem("Select"))  
 
Status.AppendDataBoundItems = True 
 
Status.DataTextField = "Name" 
 
Status.DataValueField = "Value" 
 
Status.DataBind()  
 
end if  
End Sub  
 

My preference is to populate the boxes from code but still them bound to the formview.. is this possible? 
thanks
mac 

 

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Jan 2009, 12:59 PM
Hello Mac,

When you set the DataSource in the code behind, the SelectedValue for the combobox should be also set in the code behind. A suggestion is to bind a hidden textbox with the same datafield as that of the combobox and then set the selected value of the combobox in the code behind as that of the TextBox text as shown below:
cs:
 protected void FormView2_DataBound(object sender, EventArgs e) 
    { 
        RadComboBox Status = (RadComboBox)FormView2.FindControl("ComboBoxID"); 
        Status.DataSourceID = "SqlDataSource5"
        // Status.Items.Add(new RadComboBoxItem("Select")); 
        //Status.AppendDataBoundItems = true; 
        Status.DataTextField = "Project_Name"
        Status.DataValueField = "Project_ID"
        TextBox txt = (TextBox)FormView2.FindControl("TextBoxID"); 
        Status.SelectedValue = txt.Text; 
        Status.DataBind(); 
    } 

Thanks,
Princy.
0
mac
Top achievements
Rank 1
answered on 15 Jan 2009, 04:57 PM
I must not be understanding you on some level. I am attempting to just do a basic record insertion. I want to databind the selected value of the combobox to my InsertParameter. I know I can get the value at the point of insertion but feel that really should not be necessary.
I have moved the combo population commands into the formview_databound as you have but don't see why I would set the selectedValue of the combo to a blank textbox? Using your code as suggested did allow the page to load but just resulted in a null value being inserted to the recordset. Using the below html/code only results in null values being inserted. is it your suggestion to postback the selected value into the textbox instead? I might as well just grab the selected value at time of insert instead and skip the postback. I was hoping there was a more simplistic approach than adding more objects :)
 
    <telerik:RadComboBox ID="ComboStatus"   runat="server">  
            <CollapseAnimation Duration="200" Type="OutQuint" /> 
        </telerik:RadComboBox> 
<asp:TextBox ID="tbstatus" Text='<%#Bind("status") %>' runat="server" /> 
 
    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound  
          
        Dim Status As RadComboBox = FormView1.FindControl("ComboStatus")  
        Status.DataSource = functions.NameValues("status") ' this creates a datatable of values.  
        Status.Items.Add(New RadComboBoxItem("Select"))  
        Status.AppendDataBoundItems = True 
        Status.DataTextField = "Name" 
        Status.DataValueField = "Value" 
Status.SelectedValue = TryCast(FormView1.FindControl("tbstatus"), TextBox).Text  
        Status.DataBind()  
 
    End Sub  
     


0
Veselin Vasilev
Telerik team
answered on 16 Jan 2009, 08:07 AM
Hello mac,

The problem seems to be caused by having DataValueField bound to the "Value" column while SelectedValue bound to the "status" column. They both need to be bound to one and the same column.

Sincerely yours,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
ComboBox
Asked by
mac
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
mac
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or