calling javascript function from asp.net

K

kimberly.walker

I have a piece of code that calls the clientside it works ok when no
values are passed ie window.alert ('this is a test'). But I have a
value from a textbox example:
function openemail()
{
var email = document.getElementbyID("txtEmailAddress").value;
window.alert(email);
}
when I call this function
string script = "<script language='javascript'>openemail();</script>";
Page.RegisterClientScriptBlock("email", script);
Its gives me an error saying missing object.
Any help will be greatly appreciated
 
S

Serge/XX

I have a piece of code that calls the clientside it works ok when no
values are passed ie window.alert ('this is a test'). But I have a
value from a textbox example:
function openemail()
{
var email = document.getElementbyID("txtEmailAddress").value;
window.alert(email);
}
when I call this function
string script = "<script language='javascript'>openemail();</script>";
Page.RegisterClientScriptBlock("email", script);
Its gives me an error saying missing object.
Any help will be greatly appreciated

It is due to the fact that when the aspx page is rendered, your control
txtEmailAddress will have a more complicated id based on page id and
container control (if any).

To solve your issue, just do this

function openemail(theEmailAddressControl)
{
var email = document.getElementbyID(theEmailAddressControl).value;
window.alert(email);
}

Then

string script = "<script language='javascript'>" _
"openemail(" & txtEmailAddress.ClientID & ");" _
"</script>";
Page.RegisterClientScriptBlock("email", script);

Regards
 
B

bruce barker

you are calling the function before the text control is parsed by the
browser. try using RegisterStartupScript("openemail()")

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top