How to pass a C# parameter as an argument of a javascript method ?

  • Thread starter Thread starter PhilTheGap
  • Start date Start date
P

PhilTheGap

Hi,

I've tried this:

<asp:Button ID="Save" runat="server" Text="OK" OnClick="ServerSave"
OnClientClick="SaveParam (<% Util.MaxTags %>)" />

but if fails... Util is a C# class, MaxTags a static member.

Any idea ?

Philippe
 
Hi,

I've tried this:

<asp:Button ID="Save" runat="server" Text="OK" OnClick="ServerSave"
OnClientClick="SaveParam (<% Util.MaxTags %>)" />

but if fails... Util is a C# class, MaxTags a static member.

Any idea ?

Philippe

Hi Philippe,

Do it in code:
Save.OnClientClick = string.Format("SaveParam ('{0}');",
Util.MaxTags);

Mykola
http://marss.co.ua
 
PhilTheGap used his keyboard to write :
Hi,

I've tried this:

<asp:Button ID="Save" runat="server" Text="OK" OnClick="ServerSave"
OnClientClick="SaveParam (<% Util.MaxTags %>)" />

but if fails... Util is a C# class, MaxTags a static member.

Any idea ?

Philippe

You might provide a bit more info about the way it "fails"
(compile-time, run-time? error message? behaviour you didn't expect?)

But maybe try <%= Util.MaxTags %>

<% ... %> means "execute this"
<%= ... %> means "replace with this value"

Hans Kesting
 
Hi,

I've tried this:

<asp:Button ID="Save" runat="server" Text="OK" OnClick="ServerSave"
OnClientClick="SaveParam (<% Util.MaxTags %>)" />

but if fails... Util is a C# class, MaxTags a static member.

Any idea ?

Philippe

....OnClientClick="SaveParam()" />

void SaveParam()
{
var x = <%=Util.MaxTags%>;
...
 
Hi Phil,

In line express like <%= %> cannot be used in server control's
attribute/property, it can only be embeded in plain html fragment. Here is
a blog article mentioned it:

#From the Suggestion Box: Why can't you use code expressions for
properties?
http://weblogs.asp.net/leftslipper/archive/2007/01/16/Using-code-expressions
-in-properties.aspx

For your scenario, as other member suggested, you can consider define the
javascript function without parameter and manually set the variable in the
javascript function definition like below:

==========
<asp:Control OnClientClick="js_function();" />
.........

<script ....>
function js_function()
{
var param = <%= your server-side code %>
}
</script>
==================

Also, for server control property, it support two kind of inline
expresssion:


1. DataBinding expression <%# %>

You can use it to set attributes such as:

<asp:TextBox ... Text='<%# FunctionName() %>' .../>

the "FunctionName" can be defined in codebehind(public or protected),
however, to make the databind expression get executed, you need to call
"DataBind" method on the control or it s parent control.

Here is former thread I've mentioned something on this:

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/brow
se_thread/thread/356a59a676948d03/2eec23b49e63dd96


2. There is a new feature in ASP.NET 2.0 called custom Expression Builder

#Expression Builders in ASP.NET 2.0
http://www.beansoftware.com/ASP.NET-Tutorials/Expression-Builder.aspx

#The CodeExpressionBuilder
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionB
uilder.aspx

You can use such expression builde to embed code expression into server
control's tag to as to intialize control property with some programmtic
values.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.






--------------------
 
You're welcome Phil,

Have a nice day!

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "PhilTheGap" <[email protected]>
References: <[email protected]>
 
Back
Top