Question on ASP .NET page Javascript

S

stephen

Hi,

I am binding the button.text dynamicall depending on the selection value of
the dropdownlist, at the same time I am trying to use a javascript to alert
users, but it does not fire. This is how I am doing it

In the page_load event I do this:
btnView.Attributes.Add("onclick", "return
TestValidator(btnView.Text);");

and in the page I do this:
function TestValidator(buttonValue)
{
if (buttonValue == "Get Name Values")
{
if (document.forms[0].FirstName.value != '' &&
document.forms[0].LastName.value != '')
{
var x = window.confirm("The Results will be according to LastName, do you
wish to proceed?")
return (x);
}
}
else if (buttonValue == "Get All Values")
{
if (document.forms[0].EmpID.value == '' || document.forms[0].Address.value
== '' || document.forms[0].Department.value == '')
{
alert("Requires EmpID, Address and Department")
}
}
}

Please Advice.
Stephen

[p.s: I apologize if I have posted in the wrong group]
 
M

Mark Rae

but it does not fire

Are you absolutely sure...? Place an alert at the very beginning of the
JavaScript function and see if it pops up...
In the page_load event I do this:
btnView.Attributes.Add("onclick", "return
TestValidator(btnView.Text);");

Has that actually worked? When the page has loaded, do a View Source and
check that the attribute has been correctly added to the button.
 
B

bruce barker

you have a couple errors.

1) on client side, buttons do not have a .Text property. so the if will
never be true. if is a button, you want the value.

2) in the onclick attribute you are referencing the button by name
instead of using document.getElementById or by form as in the code. but
as a on click handler you can just:

btnView.Attributes.Add("onclick",
"return TestValidator(this.value);");

3. you are using the asp.net control ids, to access elements, which may
not be what is rendered. try:

var v = document.forms[0].<%=EmpID.ClientID%>.value;

-- 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

Top