HELP! JavaScript not working through c# code

  • Thread starter Thread starter devanoy
  • Start date Start date
D

devanoy

I have a piece of c# code in the Page_Load function.

submitButton.Attributes.Add("onClick", "return checkForm('" + this +
"','" + SomeTextBox.Text + "')");

When I click the submitButton, it calls the javascript function, but
the value is not being passed....

I cannot find a work around.

Thanks in advance
 
"this," in the context of your code, is a reference to the page class.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
Hi,

I don't think you need the single-quotes around -this-, should be (this,
'textvalue');. Good luck! Ken.
 
What value isn't passed?

Are you trying to pass the value in the textbox to this function?
SomeTextBox.Text is being evaluated on the server at the time you are adding
this call. So whatever is in there that moment will be hard coded to get
passed to the function. It's not like VB.NET somehow knows that you want the
client side javascript to evalue the input control with the id of
SomeTextBox and pass its value on to the function.

Unless you were trying to do something else. But if you have to be specific
in your questions if you want a helpful response.
 
I'ts hard to tell what exactly you're adding to the "onclick" attribute of
the submit button, but I would guess that it is adding a client-side script
call for validation to it.

The string you are adding is

"return checkForm{'" + this + "','" + SomeTextBox.Text + "')"

The reference to "this" (without quotes) is a reference to the Page class
itself. Apparently, you're not working in Visual Studio.Net, because it
would give you a compile-time exception, as a Page class is not a string.

As I don't know what it is you want to concatenate into the string by
referencing "this," I can only guess, and I hate to guess.

But, if I were to guess, I would suppose that you really want to put the
string literal "this" (which is often used in JavaScript function calls to
pass a reference to the element that called the function) in your code. If
so, there is no concatenation required:

"return checkForm{'this','" + SomeTextBox.Text + "')"

Of course, this is *still* wrong, as it is not passing a reference to the
element, but the literal string "this" to the function. To pass a reference
to the element, you would need to remove the single quotes:

"return checkForm{this,'" + SomeTextBox.Text + "')"

Of course, this is probably not what you need either. For most client-side
form validation, a reference to the *form* is passed. Passing a reference to
a submit button would be generally pointless.

So, again, presuming that you *do* want to pass a reference to the form
itself to the function, this would have to be modified as follows:

"return checkForm{this.form,'" + SomeTextBox.Text + "')"

ASP.Net is all about HTML, JavaScript, CSS, and client-side technologies.
So, you have 2 skillsets to master:

1.Client-side document technologies (as mentioned above)
2. Server-side .Net programming technology

It is important that you understand both fully, or you will constantly be
plagued with this sort of issue. The Microsoft MSDN Library online is full
of information about both .Net and Internet programming technologies in
general (including HTML, DHTML, JavaScript, CSS, XML, etc). You can find it
at:

http://msdn.microsoft.com

I spend an hour or 2 a day studying this reference on average. And I've been
a developer for about a dozen years.

The nice thing is, you don't have to know it all to do it all. You just have
to know where to find a definitive answer. And if you bookmark the MSDN
Library, you will be well on your way.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
There's a seeker born every minute.
- Dr. "Happy" Harry Cox
 

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

Back
Top