jrmcdona
Guest
|
Posted:
Wed Jan 26, 2005 4:27 am Post subject:
JavaScript in a web part |
|
|
I have a Datagrid in my web part. One of the columns is a checkbox.
I have a 'select all' button as well, that will check all the checkboxes.
However, I have a basic javacsript function for doing this that normally
works. I registering the script on the page and i have tested to make sure
that it is getting called(it is).
However, in sharepoint it is not working. I noticed that sharepoint is
changing my checkbox names.
Am I missing something here, does sharepoint behave differently in some
manner.
Thanks for any samples or feedback! |
|
Tarquin Simpson-Daniel
Guest
|
Posted:
Wed Jan 26, 2005 9:15 pm Post subject:
Re: JavaScript in a web part |
|
|
I don't think Sharepoint is changing the control name, but rather ASP.Net is
prefixing your control names to ensure unique naming within the controls
collection for the page. This is fine for codebehind etc, but as you have
found out it causes a few issues when trying to correctly refer to a control
client side in JavaScript.
What I have found is that the control's name prefix changes depending on
where in the web part page your web part is placed. Therefore a web part
placed into the bottom zone, it's child controls will have a different
prefix to the same web part being placed in the top zone. Therefore it makes
it difficult to reference the controls in JavaScript.
I have found two options:
- If the webpart is static - then you can simply view the source rendered to
the client, get the full control name from the source and reference it using
this full name in JavaScript.
- If the webpart is dynamic - then you can include a unique attribute in the
child control and use a JavaScript function to obtain a reference to control
required from the client side. (example code below) Though this only works
if there is one instance of the webpart on your web part page, you could
adjust it to ensure it references to the individual instance of the web
part.
I would consider this more of a hack than a solution, so I'd welcome other
ideas from people.
Cheers
===========================
// In Code Behind
myTextBox.Attributes.Add("myAttribute", "SomeValue");
// Javascript
<script language='javascript'>
<!--
// get a reference to the textbox with the attribute - myAttribute
var myTextBox = FindControl('myAttribute');
myTextBox.value = "new user input";
function FindControl(controlToFind)
{
// iterate through the forms in the document (page)
for (var i = 0; i < document.forms.length; i++)");
{
var elementsCol = document.forms[i].elements;
// iterate through the controls collection of the given form
for (var j = 0; j < elementsCol.length; j++)
{
// check if the current element has the given attribute
if (elementsCol[j].getAttribute(controlToFind) != null)
{
return elementsCol[j];
}
}
}
}
-->
</script>
"jrmcdona" <jmc_1776@hotmail.com> wrote in message
news:318329F9-0CA3-4785-BFDF-5E850B6C2FB6@microsoft.com...
| Quote: | I have a Datagrid in my web part. One of the columns is a checkbox.
I have a 'select all' button as well, that will check all the checkboxes.
However, I have a basic javacsript function for doing this that normally
works. I registering the script on the page and i have tested to make sure
that it is getting called(it is).
However, in sharepoint it is not working. I noticed that sharepoint is
changing my checkbox names.
Am I missing something here, does sharepoint behave differently in some
manner.
Thanks for any samples or feedback! |
|
|