Calling a javascript method when a button is clicked

4 posts, 0 answers
  1. Deepika
    Deepika avatar
    19 posts
    Member since:
    Sep 2011

    Posted 11 Dec 2011 Link to this post


    I have 2 dropdowns, 2 textboxes and two buttons in my form. OnClick the button does certain things. before that, I need to validate a condition. the condition is that if only the txtbox2 is clicked, and no other parameter is selected / entered, then I should show a pop up saying the "txtBox 2 alone can't be entered, and Please also select item from first dropdown!".. Logic needed is not complicated. I am having a problem in finding the object in my javascript function.

    I ahve called IsTREmpty() on OnClientClick event for the button. I am getting the error saying object required. How can I access the form element in my javascript.?


    IsTREmpty() {  

    var tr = document.getElementById('rdCmbTR').value;  

    var tt = document.getElementById('rdCmbTT').value;  

    var tid = document.getElementById('rdTxtTId').value;  

    var tDesc = document.getElementById('rdTxtTDesc').value; 

    if (tDesc.length > 0) {  

    if (tt == -1 && tid.length == 0 && tr == -1) {  

    alert('Please Select T R'); }



  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Dec 2011 Link to this post


    You can access RadControls by using the following method.
    var menu = $find("<%=rdCmbTR.ClientID %>");

  3. Deepika
    Deepika avatar
    19 posts
    Member since:
    Sep 2011

    Posted 12 Dec 2011 Link to this post

    Hey Princy,

    I am getting some error. I think I shud nt use .value. But I need the value to compare. Ny solution?
  4. Slav
    Slav avatar
    1359 posts

    Posted 13 Dec 2011 Link to this post

    Hi Deepika,

    There are two methods, supplied by the Microsoft AJAX Library, that you can use for referencing HTML elements on your page:
    • $find - provides a shortcut to the Sys.Application.findComponent() method, which returns the specified
      Component object. Expect to use this method every time you reference a RadControl on the client.
    • $get - this method is just for finding generic HTML elements, not RadControls. It provides a shortcut to the getElementById() method.

    As most probably you are using the RadButton control, even if not explicitly mentioned, please note that it doesn't support an OnClientClick client event. Instead there are two events, which provide similar functionality - OnClientClicking and OnClientClicked. In your case OnClientClicking should be used, since the click event of the RadButton needs to be canceled if a certain condition is not met.

    Please find attached a sample project, implementing a possible solution for the described case. You can use it as a reference for your further development.

    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top