Click to See Complete Forum and Search --> : Using JavaScript in UserControl


Cipher
08-31-2006, 06:41 AM
Hi Developers,
I'm trying to use a JavaScript function in UserControl but it doesnt work, i made a simple example of what i'm trying to do

in the UserControl.ascx:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;
<input id="Button1" type="button" value="button" onclick="addText();" />

i try this function "addText()" in several places, the UserControl.ascx and the Default.aspx in different ways also:
function addText()
{
document.forms[0].TextBox1.value = "Hello World";
//document.form1.TextBox1.value = "Hello World";
// document.getElementById("TextBox1").value = "Hello World";
}

and always give me the same error that "TextBox1" is null or not an object.

Thank you for help.

sirpelidor
08-31-2006, 12:17 PM
if you view source after you loaded ur page, u gonna realize your TextBox1 is gonna call something like: UserControl_TextBox1... (i'm making this up, so view source first to verify what it is calling)

so in ur js...u wanna refer to the correct object:


var note = getElementById("UserControl_TextBox1");
note.value = "Hello World";

Cipher
08-31-2006, 01:46 PM
Thank you alot, i tried other way so close to yours and it worked pretty fine:

document.getElementById('<%=TextBox1.ClientID %>').value = "Hello World";