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

Discussion in 'ASP .Net' started by PhilTheGap, Jun 12, 2008.

  1. PhilTheGap

    PhilTheGap Guest

    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
    PhilTheGap, Jun 12, 2008
    #1
    1. Advertising

  2. PhilTheGap

    marss Guest

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

    On Jun 12, 12:06 pm, "PhilTheGap" <> wrote:
    > 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
    marss, Jun 12, 2008
    #2
    1. Advertising

  3. PhilTheGap

    Hans Kesting Guest

    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
    Hans Kesting, Jun 12, 2008
    #3
  4. Re: How to pass a C# parameter as an argument of a javascript method?

    On Jun 12, 11:06 am, "PhilTheGap" <> wrote:
    > 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%>;
    ...
    Alexey Smirnov, Jun 12, 2008
    #4
  5. 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:
    .

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






    --------------------
    >From: "PhilTheGap" <>
    >Subject: How to pass a C# parameter as an argument of a javascript method ?
    >Date: Thu, 12 Jun 2008 11:06:23 +0200


    >
    >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
    >
    >
    >
    Steven Cheng [MSFT], Jun 13, 2008
    #5
  6. PhilTheGap

    PhilTheGap Guest

    Thanks a lot Steven !
    PhilTheGap, Jun 13, 2008
    #6
  7. 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:
    .

    ==================================================
    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" <>
    >References: <>

    <>
    >In-Reply-To: <>
    >Subject: Re: How to pass a C# parameter as an argument of a javascript

    method ?
    >Date: Fri, 13 Jun 2008 09:30:11 +0200


    >
    >Thanks a lot Steven !
    >
    Steven Cheng [MSFT], Jun 13, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Novice
    Replies:
    1
    Views:
    1,124
    Mike Schilling
    Jul 19, 2003
  2. Ben Kial
    Replies:
    1
    Views:
    623
    Eric Enright
    Nov 15, 2004
  3. junk.rail
    Replies:
    1
    Views:
    646
    junk.rail
    Jun 30, 2010
  4. AikidoGuy
    Replies:
    11
    Views:
    539
    Seebs
    Nov 21, 2011
  5. AzamSharp
    Replies:
    2
    Views:
    157
Loading...

Share This Page