asp.net radiobutton doesnt recognize jscript function, but html radiobutton will

M

moondaddy

I have an asp radiobutton which calls a jscript function with its
OnCheckedChanged event. However when the page loads it errors saying that
the function doesn't exists. But when I change it from a asp.net
radiobutton to an html radiobutton (Input element and type=radio) the page
loads and all is fine. Below is the asp.net control that can't see the
jscript. I've had this problem in the past with other asp.net control and
would like to know what to do.

<asp:radiobutton id="chkPayByCC" runat="server" Text="Online Credit Card
Transaction: " OnCheckedChanged="PaymentMenthod(this)"
Checked="True"></asp:radiobutton>

Thanks
 
G

Guest

If you use asp:radiobutton and OnCheckedChanged.

PaymentMenthod(this) does not call the js, it calls the method named "PaymentMenthod(object sender, System.EventArgs e)" in your vb/c# source code.

So, if you did not write that method, it would tell you it does not exist.
 
M

MS News \(MS ILM\)

So How do you have an asp:control and force its events to be handles first
by js and then by code behind??
is this possible?
and how?



Tom said:
If you use asp:radiobutton and OnCheckedChanged.

PaymentMenthod(this) does not call the js, it calls the method named
"PaymentMenthod(object sender, System.EventArgs e)" in your vb/c# source
code.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Thanks for posting in this group!!

Based on my understanding, you can not use your jscript function
"PaymentMenthod(this)" work with asp.net server control <asp:radiobutton>

===============================
I think you still do not quite understand the Asp.net web server control
model.
Asp.net server web control encapsulate the client side html, css and
javascript. And it will be execute at web server side. While for
Javascript(or JScript), it is a client side language, it will execute by
the IE, which view this page.

For <asp:radiobutton>, as a web server control, its OnCheckedChanged is a
server side event, you should specify a SERVER SIDE event handler to handle
this event. You specify a CLIENT SIDE JScript function for it, asp.net will
conside this function name as a server side function, but it can not find
one, so a runtime error is generated.

And why your change <asp:radiobutton> to <input type=radio>, then it works?
If you mark a html element with runat=server attribute, it will become a
server side html control.(Will execute at server side, then render to
client side). Please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/cpconaspsyntaxforhtmlcontrols.asp

For Html control, any attribute it does not encapsulate will be added into
its Attribute collection(you can access it through "Attributes" property).
Please refer to:
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconSharedHTMLControl
Properties.asp

For your radio control with "runat=server" attribute, it is mapping to
HtmlInputRadioButton class. HtmlInputRadioButton only encapsulate one
special server event; ServerChange, you did not handle it. So your
OnCheckedChanged will be treated as a client side event(handle by client
side JScript), and will be encapsulte into HtmlInputRadioButton.Attributes
collection. Then after rendering to client side, it will find the JScript
function "PaymentMenthod(this)", and it works.

I hope I have clarified all these things to you. If you want to use the Web
Server raidobutton control and want to add the client side event handler
for client side event OnCheckedChanged, you manually add the event handler,
like this:

<asp:radiobutton id="RadioButton1" runat="server" Text="Online Credit Card
Transaction:" Checked="True"></asp:radiobutton>

private void Page_Load(object sender, System.EventArgs e)
{
RadioButton1.Attributes.Add("OnCheckedChanged", "PaymentMenthod(this)");
}

====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi MS News,

Please refer to my reply to moondaddy. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

moondaddy

Thanks that helps a lot in clearing things up. so I guess if I really want
to execute client side script first and then perhaps execute server side
code, then I need to have an html control fire the initial event which calls
client side jscript and then with in that script call some code that will
force a postback and call server side code. Right? Thanks again.

--
(e-mail address removed)
"Jeffrey Tan[MSFT]" said:
Hi moondaddy,

Thanks for posting in this group!!

Based on my understanding, you can not use your jscript function
"PaymentMenthod(this)" work with asp.net server control <asp:radiobutton>

===============================
I think you still do not quite understand the Asp.net web server control
model.
Asp.net server web control encapsulate the client side html, css and
javascript. And it will be execute at web server side. While for
Javascript(or JScript), it is a client side language, it will execute by
the IE, which view this page.

For <asp:radiobutton>, as a web server control, its OnCheckedChanged is a
server side event, you should specify a SERVER SIDE event handler to handle
this event. You specify a CLIENT SIDE JScript function for it, asp.net will
conside this function name as a server side function, but it can not find
one, so a runtime error is generated.

And why your change <asp:radiobutton> to <input type=radio>, then it works?
If you mark a html element with runat=server attribute, it will become a
server side html control.(Will execute at server side, then render to
client side). Please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/cpconaspsyntaxforhtmlcontrols.asp

For Html control, any attribute it does not encapsulate will be added into
its Attribute collection(you can access it through "Attributes" property).
Please refer to:
http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconSharedHTMLControl
Properties.asp

For your radio control with "runat=server" attribute, it is mapping to
HtmlInputRadioButton class. HtmlInputRadioButton only encapsulate one
special server event; ServerChange, you did not handle it. So your
OnCheckedChanged will be treated as a client side event(handle by client
side JScript), and will be encapsulte into HtmlInputRadioButton.Attributes
collection. Then after rendering to client side, it will find the JScript
function "PaymentMenthod(this)", and it works.

I hope I have clarified all these things to you. If you want to use the Web
Server raidobutton control and want to add the client side event handler
for client side event OnCheckedChanged, you manually add the event handler,
like this:

<asp:radiobutton id="RadioButton1" runat="server" Text="Online Credit Card
Transaction:" Checked="True"></asp:radiobutton>

private void Page_Load(object sender, System.EventArgs e)
{
RadioButton1.Attributes.Add("OnCheckedChanged", "PaymentMenthod(this)");
}

====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Yes, your thought is right. The client side postback is needed for server
side execute.

But you need not to do so much things, because Asp.net webform model has
provided you much help on this(such as postback). You also need not giveup
Web server control.

For example, you may Button web server control to process both client and
server side function, like this:

Suppose there is a client function, like this:

<script language=javascript>
function clientfunc()
{
alert("client side alert");
}
</script>

also, there is a server side function:

protected void ServerFunc(object sender, System.EventArgs e)
{
this.Response.Write("Server side output");
}

Then, you may use the Button web server control like this:

<asp:Button id="Button1" OnClick="ServerFunc" runat="server"
Text="Button"></asp:Button>
private void Page_Load(object sender, System.EventArgs e)
{
this.Button1.Attributes.Add("onclick", "clientfunc()");
}

OnClick="ServerFunc" will register the server side event OnClick, while the
this.Button1.Attributes.Add("onclick", "clientfunc()") will register the
client side onclick event.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

moondaddy

Thanks this sounds great. I have just a few questions on the code below.
It looks like you are adding an onclick attribute from the server. So when
the user clicks on it you will get a client side click event and also a
server side click event. Which one will happen first? Could you please
provide a vb.net version of the sample code below.

Thanks again!
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Thanks very much for your feedback.

Yes, for web server control, you can only manipulate it at server side, as
you can see, I add the onclick attribute in Page_Load event. That is, I add
the onclick attribute when page load. Then the Button web control will
render like this:

<input type="submit" name="Button1" value="Button" id="Button1"
onclick="clientfunc()" />

Actually, in order for the server to execute code, the page must postback
to the server side. In asp.net web form, generally, there are 2 ways to
postback:
1). Through <input type="submit"> html code
2). Through javascript code to explicit postback the entire web form to the
server side

For Button web control, it will use the first way of using <input
type="submit">, so when you click that button, the page will postback to
the server side. But after click and before postback, the client javascript
code hook into the client onclick event(), invoking clientfunc().

Once postback, the asp.net will associate ServerFunc() with Button web
control's server side OnClick event, then it will execute.

As a whole, the client javascript function will execute before the server
side function.

Hope I have clarified for you.

I think you may buy a Asp.net book to learn some internal mechanism of
Asp.net web control model. It will be more clear :)

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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