[RESOLVED] Get the clientId for a button in a javascript
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: [RESOLVED] Get the clientId for a button in a javascript

  1. #1
    Join Date
    May 2006
    Posts
    170

    [RESOLVED] Get the clientId for a button in a javascript

    I have a code looking like this

    Code:
    function a()
    {
    ...
    displayNextButton( checkBoxesArray );			
    ...
    }
    
    function displayNextButton( checkBoxesArray )
    {
    	for( i = 0; i < checkBoxesArray.length; i++ )
    	{
    		alert("inne i loopen");
    		elem =  checkBoxesArray[i];
    				
    		if( !elem.checked )
    		{
    			alert("inne i if checked och visible");
    			document.getElementById('bNextPage').disabled = true;		
    			return;
    		}
    				
    		else
    			document.getElementById('bNextPage').disabled = false;		
    	}
    		
    }
    
    ....
    
    <asp:Button ID="bNextPage" OnClick="bNextPage_OnClick" Text="Vidare" Runat="server" />
    But it doesn't work because the buttoncontrol is a runat server control so the name isn't bNextPage. But maybe I can use the clientId istead? How can I get the clientId name of the button?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Get the clientId for a button in a javascript

    You will have to use the Code Behind file to render the JavaScript on the final Page. The advantage of using Code Behind is that you get access to all server side controls and you can use the ClientID of all the controls. You can use Page.RegisterClientScriptblock method to register the script. You put the script into a string and then use the above method to render the JavaScript on the final page.

  3. #3
    Join Date
    May 2006
    Posts
    170

    Arrow Re: Get the clientId for a button in a javascript

    Oh I think that my problem is that the script runs after the control has been rendered? Can it be like that in the show source it looks like this

    Code:
    ...
    
    <input type="submit" name="bNextPage" value="Vidare" id="bNextPage" />
    
    ...
    
    <script>changeSegmentDisplay( document.getElementById('repeaterDTH:_ctl1:cbMultiRoom') );</script>
    
    <script>filterProductDependencies();</script>
    
    </form>
      </body>
    </html>
    So I shouldn't have to get the clientid? I mean it says bNextPage here too?
    Last edited by Visslan; May 11th, 2007 at 07:24 AM.

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Get the clientId for a button in a javascript

    I don't think that is the right way to do it. It is better to use the Code Behind for this purpose. have you taken a look at RegisterClientScriptBlock method.

  5. #5
    Join Date
    May 2006
    Posts
    170

    Re: Get the clientId for a button in a javascript

    A little bit.....

  6. #6
    Join Date
    May 2006
    Posts
    170

    Re: Get the clientId for a button in a javascript

    Yeah but the problem is that the first script that starts the otherone (that i want to start at the top ) has to be at the bottom. And the array that the first script makes is the data i need for the second one.....dilemma?!

  7. #7
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Get the clientId for a button in a javascript

    Quote Originally Posted by Visslan
    Yeah but the problem is that the first script that starts the otherone (that i want to start at the top ) has to be at the bottom. And the array that the first script makes is the data i need for the second one.....dilemma?!


    You need to make it simple for me to understand. Or else you can wait for PeejAvery to help you out. He is our scripting Guru.

  8. #8
    Join Date
    May 2006
    Posts
    170

    Talking Re: Get the clientId for a button in a javascript

    I have to think a bit first and test =). I'll be back =)!

  9. #9
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,839

    Re: Get the clientId for a button in a javascript

    Quote Originally Posted by Shuja Ali
    Or else you can wait for PeejAvery to help you out. He is our scripting Guru.
    Haha. Thanks but I just help as much as I can. I don't think I am any bit of a guru.

    Covering multiple posts...No, you can't have colons within IDs nor names. They are invalid characters. And the script should run after the control is rendered since the rendering happens server-side and the script is JavaScript which is a client-side script.

    In your first post you mention name and ID, the ID will be the same as the server's output so getElementById() is sufficient for JavaScript. If you have to have the name, you can use getElementById().name to return that.

    I would suggest using Firefox's Error Console to debug your scripting part of it. After that, post back with more results.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    May 2006
    Posts
    170

    Question Re: Get the clientId for a button in a javascript

    I don't know what's wron with the script

    codebehind (ASP.NET)
    Code:
    string buttonClientId = this.bNextPage.ClientID;
    string scriptTest = "<script>test( buttonClientId );</script>";
    RegisterStartupScript( "key", scriptTest );
    codeinfront(C#)
    Code:
    function test( clientId )
    {
    	document.getElementById( clientId ).disabled = true;	
    }
    
    ...
    
    <asp:Button ID="bNextPage" OnClick="bNextPage_OnClick" Text="Vidare" Runat="server" />
    In mozilla it says:

    buttonClientId is not defined?

    and sender has no properties?

  11. #11
    Join Date
    May 2006
    Posts
    170

    Talking Re: Get the clientId for a button in a javascript

    oh found it

    string scriptTest = "<script>test( '" + buttonClientId + "' );</script>";

  12. #12
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,839

    Re: [RESOLVED] Get the clientId for a button in a javascript

    Debuggers help, don't they? Glad you solved it.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center