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

Problem getting and setting the selected value of the rad combo box in the javascript when adding the javascript through code behind.

7 Answers 564 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Mausami
Top achievements
Rank 1
Mausami asked on 20 Jan 2009, 03:09 PM

Hello,

I have a rad combo box , a rad date picker and a required field validator for the rad date picker in a rad grid.

I have added through code behind, a javascript for the rad combo box's client side event 'OnClientSelectedIndexChanged'.

I need the selected value of the rad combo box in the javascript to enable or disable the required field validator present in the corresponding row.

I pass the rad combo box's ClientID and required field validator's ClientID as parameters thru backend to this function.

In order to get the selected value, I have tried using the following piece of code,

   <script type="text/javascript">
 
       function OnClientSelectedIndexChanged(radComboClientID, reqFieldValidatorClientID)
      {
          //getting the selected value
          alert(radComboClientID.GetValue());
       }

    </script>

It doesn't work; giving an error 'Object doesn't support this property or method'

Is there any way to access the selected value in this javascript.

Kindly help solving this issue.

Thanks,
Mausami.

7 Answers, 1 is accepted

Sort by
0
Serrin
Top achievements
Rank 1
answered on 20 Jan 2009, 06:00 PM
Heya Mausami,

You could try something along these lines:

aspx javascript:
function HandleEndChanging(sender, args)  
            {      
                var item = args.get_item();  
                alert(sender.get_text());  
            } 

And in the control definition...
<telerik:RadComboBox ID="RadComboBox1" runat="server" Height="170px" Width="204px" 
                    Skin="Default" ShowMoreResultsBox="True"                        
                    EnableLoadOnDemand="True"   
                    OnClientSelectedIndexChanged="HandleEndChanging"   
</telerik:RadComboBox> 

This should work just fine, and this code was pilfered from the RadComboBox client-side demo and modified slightly. :)

 

This should get you started on getting the value of the text in RadCombo, then you would just use that for whatever operation you are going for.

 

-Serrin

0
Mausami
Top achievements
Rank 1
answered on 21 Jan 2009, 05:44 AM

Hi Serrin,

Thanks for the reply.

Using the above javascript I get the error --  'undefined' is null or not an object.

I am able to get the selected text by calling the javascript in the aspx page as given below.

<rad:RadComboBox ID="radComboStatus" runat="server" Skin="WindowsXP" AutoPostBack="false" DataTextField="EmployeeStatusDescription" DataValueField="EmployeeStatusCode" Width="100px" OnClientSelectedIndexChanged="HandleEndChanging"></rad:RadComboBox>

To get the value :
    <script type="text/javascript">
        function HandleEndChanging(item)  
        {      
            alert(item.ComboBox.GetValue());  
        }
   </script>

To get the text :

    <script type="text/javascript">
        function HandleEndChanging(item)  
        {      
            alert(item.ComboBox.GetText());  
        }
   </script>

But my requirement is different .

 I need the ClientID  of the required field validator also which is present in the corresponding grid row as that of the RadComboBox.
So adding the javascript function in the aspx file as in the above example will not help me.

I want to add the javascript function through code behind in the 'ItemDataBound'  event of the grid as given below.

    protected void radGridEmployeesDetails_ItemDataBound(object sender, GridItemEventArgs e)
    {
         if (e.Item is GridDataItem)
            { 
                 RadComboBox radCmbStatus = (RadComboBox)e.Item.FindControl("radComboStatus");
                 RequiredFieldValidator reqField = (RequiredFieldValidator)e.Item.FindControl("EffectiveDateValidator");

                 if(radCmbStatus != null && reqField!= null)
                 {
                       string functionName = String.Format("OnClientSelectedIndexChanged('{0}', '{1}')", radCmbStatus.ClientID, reqField.ClientID); 
                       radCmbStatus.OnClientSelectedIndexChanged = functionName;                                       
              
                 }
         }
   }

Hope you got my requirement !
Is there any way of getting the selected value in the javascript function using the ClientID of the radComboBox passed as a parameter.


Thanks,
Mausami

0
Veselin Vasilev
Telerik team
answered on 21 Jan 2009, 11:02 AM
Hi Mausami,

The code of Serrin is related to RadCombobox for ASP.NET AJAX.
You are using the "classic" RadComboBox maybe?

Please shed some light on the exact version.

Sincerely yours,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mausami
Top achievements
Rank 1
answered on 21 Jan 2009, 11:09 AM
Hi Veselin,

The Product version and Assembly version of the RadCombobox which I am using is 2.8.5.0.

Thanks,
Mausami
0
Mausami
Top achievements
Rank 1
answered on 23 Jan 2009, 02:29 PM
Hi Veselin, Serrin,

Just wanted to know whether my requirement can be fulfilled using the version of ddl that I have.

Thanks.
Mausami
0
Veselin Vasilev
Telerik team
answered on 26 Jan 2009, 08:32 AM
Hi Mausami,

The OnClientSelectedIndexChanged event handler should accept only one parameter - the newly selected  item.

I assume that you want to access the requred field validator client object when the item is changed.
Here is how you can do it:

<EditFormSettings EditFormType="Template"
    <FormTemplate> 
        <rad:RadComboBox id="RadComboBox1" runat="server"  
            AllowCustomText="True" DataSourceID="AccessDataSource1" 
            SelectedValue='<%# Bind("Name") %>'  
            DataTextField="Name" 
            DataValueField="Name" 
            OnClientSelectedIndexChanged="OnClientSelectedIndexChangedHandler" 
            Width="150px"
        </rad:RadComboBox> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
            ErrorMessage="RequiredFieldValidator"  
            ControlToValidate="RadComboBox1"
         </asp:RequiredFieldValidator> 
        <asp:Button ID="btn1" runat="server" Text="Save" />                             
    </FormTemplate> 
</EditFormSettings> 

<script type="text/javascript"
function OnClientSelectedIndexChangedHandler(item) 
    var combo = item.ComboBox; 
    var validatorID = combo.ClientID.replace("RadComboBox1""RequiredFieldValidator1"); 
    //find the validator 
    var validator = document.getElementById(validatorID); 
</script>  

I hope this helps.

Kind regards,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mausami
Top achievements
Rank 1
answered on 27 Jan 2009, 02:09 PM

Hi Veselin,

The above piece of code given by you has helped me implementing my requirement  :)
Many thanks !!

Regards,
Mausami

Tags
ComboBox
Asked by
Mausami
Top achievements
Rank 1
Answers by
Serrin
Top achievements
Rank 1
Mausami
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or