Frank Q.
Frank Q.

Reputation: 6612

How to call a javascript function in ASP.NET based on the QueryString

<%@ Page Language="C#" AutoEventWireup="true" %>
    <%
        string paramString = Request.QueryString["query"];
        if (null != paramString)
        {
            if (paramString.ToLower() == "ValueIsRight".ToLower())
            {
                //Here I want to invoke ABC function below defined in my asp page
            }
        }
    %>

 <script type="text/javascript">
        function ABC {
            }
</script>

I am very new to ASPX and want to know if there is anyway to call this function? I tried using the call keyword but it doesn't come up in my IDE.

Upvotes: 0

Views: 369

Answers (2)

Ratna
Ratna

Reputation: 2319

Well the most basic and dangerous way would be

    <script type="text/javascript">
    function ABC {
        }
     </script>
<%@ Page Language="C#" AutoEventWireup="true" %>
<%
    string paramString = Request.QueryString["query"];
    if (null != paramString)
    {
        if (paramString.ToLower() == "ValueIsRight".ToLower())
        {
             Response.Write("<script>");
             Response.Write("ABC();");
             Response.Write("</script>");
            //Here I want to invoke ABC function below defined in my asp page

            //dont use this method
        }
    }
%>

/////////end///////////// NOTE: javascript Function definition is at the starting.

Please use ScriptManager.RegisterStartupScript

To know in greater detail visit http://www.dotnetcurry.com/showarticle.aspx?ID=200

To get querystring on client

  function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));

}

source-->How can I get query string values in JavaScript?

Upvotes: 0

Brent Echols
Brent Echols

Reputation: 590

So as far as I can recall, you can't really do that. The c# runs on the server before shipping off the html for the client's request, so running that javascript function would require the server to have its own javascript engine running, which ASP does not. What you can do though, is a flag variable to the javascript, so whne the client loads, it can run differently based on the flag you pass in. For example

<script type="text/javascript">ABC(<%: someFlag.toString() %>)</script>

will pass that variable to your script to run!

Upvotes: 1

Related Questions