简体   繁体   中英

Trigger C# method from JavaScript and send parameters

I'm trying to call a C# method from JavaScript and send parameters along with the call. I've done calls to web method but since I can't get to server side controls in static web method, I can't really use it in this case.

I can't do full postback because I need to get Google Map coordinates which in my case I'm accessing them this way, and I'm not able to access them differently:

        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var bounds = new google.maps.LatLngBounds();
        bounds = map.getBounds();    
        var latLng = map.getCenter();                
        var latSW = bounds.getSouthWest().lat();
        var lngSW = bounds.getSouthWest().lng();
        var centerLat = latLng.lat();
        var centerLng = latLng.lng();

So now since I want to pass some of these parameters to actual c# function it's ok even if I need to do full server side postback as long as I have the controls in my backend code.

Once on server side I need to populate asp.net repeater control with the dataset, that's the biggest issue why I need to trigger backend method.

public void GetData(string latSW, string lngSW, string latNE, string lngNE)
    {
        DataTable dt = this.GetData(latSW, lngSW, latNE, lngNE);
        rptMarkers.DataSource = dt;
        rptMarkers.DataBind();
        RepDetails.DataSource = dt;
        RepDetails.DataBind();
    }

I'm unsure, from your question, whether you can or can't make a full post back so here's a fun javascript solution

function myawesomejavascriptfunction(latsw,lngsw,latne,lngne)
{
 var myawesomeform=document.createElement('form');
 myawesomeform.setAttribute('method','post");
 myawesomeform.setAttribute('action','/myawesomewebendpoint');

 var myawesomelatsw=document.createElement('input');
 myawesomelatsw.setAttribute('type','hidden');
 myawesomelatsw.setAttribute('name','latsw');
 myawesomelatsw.setAttribute('value',latsw);

 var myawesomelngsw=document.createElement('input');
 myawesomelngsw.setAttribute('type','hidden');
 myawesomelngsw.setAttribute('name','lngsw');
 myawesomelngsw.setAttribute('value',lngsw);

 var myawesomelatne=document.createElement('input');
 myawesomelatne.setAttribute('type','hidden');
 myawesomelatne.setAttribute('name','latne');
 myawesomelatne.setAttribute('value',latne);

 var myawesomelngne=document.createElement('input');
 myawesomelngne.setAttribute('type','hidden');
 myawesomelngne.setAttribute('name','lngne');
 myawesomelngne.setAttribute('value',lngne);

 document.getElementsByTagName('body')[0].appendChild(myawesomeform);
 myawesomeform.submit();
}

The endpoint method at /myawesomewebendpoint will call the class with the GetData() method and pass on the parameters.

It will work, provided I've not got hold of the entirely wrong end of the stick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM