简体   繁体   中英

Pass data to server side from Javascript code

I'm trying to send data from client-side to server-side (asp.net c#), if you really want to know, I want to send the window.name property .

First I thought about having a asp:HiddenField and on the OnSubmit event have some JS write the value in the hidden field. The only problem is that I can access the hidden field value (according to this ) only from PreLoad event to PreRenderComplete event. The project that I'm working on has a lot of code in the OnInit event, and unfortunately I cannot move it and I need to use the window.name value here.

The other ideas that I have is to add a custom HTTP Header windowId or on the OnSubmit event have a JS that appends a parameter to the document.location.href .

I managed to write to the header from JS with the XMLHttpRequest setRequestHeader , but, maybe I did something wrong in my implementation , this generates 2 requests, the first one is the normal, expected one(clicking a button/link ...) and the second is from the XMLHttpRequest . I find this behavior very unnatural. Do you have any sugestions? ( see code snippet below ). I do not what to use AJAX.

    var oReq = new window.XMLHttpRequest;
    oReq.open('POST', document.location, false);
    oReq.setRequestHeader("windowId", window.name);
    oReq.send(null);

For the OnSubmit hook idea, i haven't spent to much time on it, but i think I have to append the # character before i append my windowId parameter with it's value, so that the page doesn't reload. I might be wrong about this. Any way, I have to remove this from the URL after I take the value, so that the user doesn't see the nasty URL. Do you have any sugestions?

Ok so what are your ideas?

Thank you for reading all my blabbering, and thank you, in advance, for you answers.

我会推荐<asp:HiddenField /> (例如, <asp:HiddenField ID="hfWindowName" runat="server" /> 。在OnInit您仍然可以使用Request.Form来访问其值:

string windowName = Request.Form(hfWindowName.UniqueID);

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