Call aspx function from javascript

salmanfazal
salmanfazal used Ask the Experts™
on
I want to call an aspx function lets say datareader() from javascript. What is the correct syntax for this? I am using VS 2008 with c#
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Say you want to call this function on button click.
call as:
onclientclick="datareader();"
I dont really understand the question. Javascript runs client side, and asp .net functions run server side. As far as i know, aspx pages are generated on the server dynamically and then send out the web page to the client (including javascript ) and once the page is served, then javascript runs in the client's browser
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Why do you need to access asp.net server side functions from JavaScript?
Hi salmanfazal,

Just have a Button Click event with the functionality and u can call the click event from the javascript as

document.getElementById("Button1").click();

Author

Commented:
well lets say i have a client side click event in my dropdownlist. on this event i want to load the dropdownlist with an aspx function. Lets say aspx function will read the data from database.

I dont want to load the dropdownlist everytime the page or control gets load. I want to load the data when it get clicked only.

Commented:
Call the DropDownFill function which is in aSP.NET/c# from the javascript function which invokes the dropdown click.
just have a button with its style property display:none;

Try with the sample

aspx page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function CallFunction()
    {//debugger
       if(document.getElementById("TestDdl").length == 1)
            document.getElementById("TestButton").click();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="TestDdl" runat="server" onclick="CallFunction()" >
    <asp:ListItem Text="Select"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="TestButton" runat="server" style="display:none;" OnClick="TestButton_Click" />
    </div>
    </form>
</body>
</html>

cs Page

protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TestButton_Click(object sender, EventArgs e)
    {
        TestDdl.Items.Add("Test1");
        TestDdl.Items.Add("Test2");
        TestDdl.Items.Add("Test3");
    }

Author

Commented:
that is my question. How to call the dropdownfill function?
Read this (http://www.javascriptbank.com/chained-selects-index.html) to check if that's what you need.

Commented:
As has been said before: javascript runs on the client, your datareader() function runs on the server. So they run on different machines. Javascript can not call the function on a different machine. It can only do a http-request to an .aspx and then this aspx then calls the function, and returns the results as a webpage. It is not uncommon to have the results returned as xml or json,so that the javascript can easily read the result and present them in the page.
an example for ur requirement

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function CallFunction()
    {//debugger
       if(document.getElementById("TestDdl").length == 1)
            document.getElementById("TestButton").click();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="TestDdl" runat="server" onclick="CallFunction()" >
    <asp:ListItem Text="Select"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="TestButton" runat="server" style="display:none;" OnClick="TestButton_Click" />
    </div>
    </form>
</body>
</html>

cs Page

protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TestButton_Click(object sender, EventArgs e)
    {
        dropdownfill ()
    }

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial