Michael Wu
Michael Wu

Reputation: 13

Calling Code behind Function in JavaScript

I want to call function through javascript, but I get an error message "CS0103: The name 'bidindex' does not exist in the current context" Can you help me? Thanks

JS code

<script>
    function senddata(whatdate, bidindex) {          
        var a = "<%=DatabidGridView1(bidindex,whatdate%>";
    }
</script>

.cs code

public string DatabidGridView1(string sindex, string sdate)
        {
   return "good";
        }

Upvotes: 0

Views: 335

Answers (1)

Anton Rashchenko
Anton Rashchenko

Reputation: 81

Like homungus said the Method in your codebehind has to be a static WebMethod.

It could be something like:

JS Code

$(".clickMe").click(function(){ senddata(data, index) });

function senddata(whatdate, bidindex) {          
   PageMethods.DatabidGridView1(bidindex, whatdate);
}

.CS Code

[WebMethod]
public string DatabidGridView1(string sindex, string sdate)
{return "good";}

Also meby a useful link with useful information: Pagemethods in asp.net

Upvotes: 1

Related Questions