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

How to access a combobox within a user control with a grid?

6 Answers 74 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Chris @ Intrinsic
Top achievements
Rank 1
Chris @ Intrinsic asked on 19 Jan 2011, 10:30 PM

I have a user control that I use for editing / inserting records for the radgrid.  Within that user control there is a radcombobox.  But, when I try to retrieve the selectedvalue like so:    locationTypeCode = rcmLocationType.SelectedValue, it returns null. Why is that?  the next line below is retrieving the value from a radtextbox like locationName = rtbLocationName.Text,  which works fine.  so, is there a bug with the combobox?  I CAN retrieve the selected TEXT like this:  rcmLocationType.Text, but I do not want the text, I want the selected value.  So what is the workaround or code to access the selected value in the combobox?

thanks.

6 Answers, 1 is accepted

Sort by
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 19 Jan 2011, 10:31 PM
Also, all the code above is within the codebehind of the user control.   There must be some search method to find the values or something like that?
0
Simon
Telerik team
answered on 20 Jan 2011, 10:28 AM
Hi C,

Can you please paste here the markup of the RadComboBox and any code that uses the control in the code-behind?

Best wishes,
Simon
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 20 Jan 2011, 06:58 PM
<td>
<telerik:RadComboBox ID="rcmLocationType" runat="server" EmptyMessage="Choose a location Type"/>
<asp:RequiredFieldValidator runat="server" ID="rfvLocationType" ControlToValidate="rcmLocationType" ErrorMessage="*" CssClass="validator" />                
</td>
protected void Page_Load(object sender, EventArgs e)
       {
           PopulateDropDownLists();
             
       }
       private void PopulateDropDownLists()
       {
           // populate the Location type
           rcmLocationType.DataSource = ApplicationState.Current.LocationType;
           rcmLocationType.DataValueField = "LOCATION_TYP_CD";
           rcmLocationType.DataTextField = "DESC_TXT";
           rcmLocationType.DataBind();
           rcmLocationType.Items.Insert(0, new RadComboBoxItem("", "")); //Add empty line at top of list
       }
protected void btnInsert_Click(object sender, EventArgs e)
       {
          Location location = new Location();
          //Telerik combobox does NOT work in user control, so hardcoding for now.
          location.locationTypCd = "TEST"; // rcmLocationType.SelectedValue; 
             location.locationName = rtbLocationName.Text; //This WORKS
     }

Retrieving the value of the combobox does not work, but retrieving the value of the textbox works fine.  And, retrieving the .Text property of rcmLocationType works fine as in rcmLocationType.Text, but I want the value not the text property.

Thanks.
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 20 Jan 2011, 11:41 PM
I fixed it already.   Just added the populatedropdownlists method as an event handler.  but, now,  how exactly do I set the initial value of the dropdown based on the selected value from grid when I go into edit mode????

This code from your example does not work: DataBinder.Eval(DataItem, "MyFieldName");

(DataItem does not exist in code behind, but it exists in the .ascx page.)
So, how exactly would I read values from the grid in the codebehind??





0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 20 Jan 2011, 11:55 PM
Wait,  I fixed that too.  But, I had to use the Page_PreRender 
method.  Is that the best way to  do it?  As I use the eval.(.... in the .ascx page to get the values that were sent from the grid to the user control, then put that value in the hidden field.  Then, only in the prerender method could I access the hidenfield.  What is the better way to do it?


Also, in the same grid ( that contains this user control),  I want to have a button in the grid to display another grid if I click it (so i can edit a linked table that is linked to the table that is bound to the grid.)  Do you have sample code for that?

thanks.

0
Veli
Telerik team
answered on 27 Jan 2011, 07:00 PM
Hello C,

To set the SelectedValue of the combo to an value from the currently edited grid item, try that:

GridEditableItem editedItem = (GridEditableItem)this.NamingContainer;
rcmLocationType.SelectedValue = DataBinder.Eval(editedItem.DataItem, "MyFieldName").ToString();

The NamingContainer of the user control the combo is instantiated in is the very same edited item in RadGrid. Thus, you can cast it to a general GridEditableItem and use DataBinder.Eval() on that.

As for the requirement for linked tables, check the RadGrid Master/Detail Grids online demo as a suggestion.

Greetings,
Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
ComboBox
Asked by
Chris @ Intrinsic
Top achievements
Rank 1
Answers by
Chris @ Intrinsic
Top achievements
Rank 1
Simon
Telerik team
Veli
Telerik team
Share this question
or