Connecting Tech Pros Worldwide Help | Site Map

HELP! JavaScript not working through c# code

devanoy@hotmail.com
Guest
 
Posts: n/a
#1: Nov 19 '05
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

Kevin Spencer
Guest
 
Posts: n/a
#2: Nov 19 '05

re: HELP! JavaScript not working through c# code


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

<devanoy@hotmail.com> wrote in message
news:1131727774.355688.142520@g14g2000cwa.googlegr oups.com...[color=blue]
>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
>[/color]


Ken Dopierala Jr.
Guest
 
Posts: n/a
#3: Nov 19 '05

re: HELP! JavaScript not working through c# code


Hi,

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

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

<devanoy@hotmail.com> wrote in message
news:1131727774.355688.142520@g14g2000cwa.googlegr oups.com...[color=blue]
> 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
>[/color]


Marina
Guest
 
Posts: n/a
#4: Nov 19 '05

re: HELP! JavaScript not working through c# code


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.

<devanoy@hotmail.com> wrote in message
news:1131727774.355688.142520@g14g2000cwa.googlegr oups.com...[color=blue]
>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
>[/color]


Kevin Spencer
Guest
 
Posts: n/a
#5: Nov 19 '05

re: HELP! JavaScript not working through c# code


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

"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:OBX9bPu5FHA.2956@TK2MSFTNGP12.phx.gbl...[color=blue]
> "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.
>
> <devanoy@hotmail.com> wrote in message
> news:1131727774.355688.142520@g14g2000cwa.googlegr oups.com...[color=green]
>>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
>>[/color]
>
>[/color]


Closed Thread