-
May 11th, 2007, 07:07 AM
#1
[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?
-
May 11th, 2007, 07:19 AM
#2
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.
-
May 11th, 2007, 07:20 AM
#3
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.
-
May 11th, 2007, 07:26 AM
#4
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.
-
May 11th, 2007, 07:28 AM
#5
Re: Get the clientId for a button in a javascript
-
May 11th, 2007, 07:31 AM
#6
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?!
-
May 11th, 2007, 07:40 AM
#7
Re: Get the clientId for a button in a javascript
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.
-
May 11th, 2007, 08:06 AM
#8
Re: Get the clientId for a button in a javascript
I have to think a bit first and test =). I'll be back =)!
-
May 11th, 2007, 09:45 AM
#9
Re: Get the clientId for a button in a javascript
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.
-
May 14th, 2007, 08:21 AM
#10
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?
-
May 14th, 2007, 08:46 AM
#11
Re: Get the clientId for a button in a javascript
oh found it
string scriptTest = "<script>test( '" + buttonClientId + "' );</script>";
-
May 14th, 2007, 09:39 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|