开发者

ASP.NET get textbox value filled from Javascript in Load or Unload Event

simply, i have an ASP.net Textbox inside a webcontrol it gets filled by a javascript function inside the same markup of the webcontrol

the problem is that i need to read that value from the serverside events of the webcontrol tried page_load, page_unload... but they all fire before the javascript function is executed.

i even tried to move the JS code to a seperate Script file, and added a reference to it. but again its just the same.

when i try to call that function to fill the textbox, using:

Page.ClientScript.RegisterClientScriptBlock //which calls it to early
Page.ClientScript.RegisterStartupScript //which calls it too late ;P

but again it's executed before the Script reference is included in the render of the control.

any suggestions except of Registering all the JS code using registerClientScriptBlock?

im sure im missing something important related to the life cycle开发者_运维技巧 of the web control, so please enlighten me and sorry for the long blablabla. thanks in advance


As Tim implied, this is probably better done on the server, prior to output.

However, to answer your question, you could create a webservice which the client could call to notify the backend of the calculated value. Here's a very rough example:

NewWebService.asmx:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void SaveTextBox(string textValue)
{
    <%-- Save the value here --%>
}

YourPage.html:

// Requires jQuery.  
// Code can be refactored to use any library or none at all, if you like

<script>
$("#textBoxId").change(function() {

    var textValue = this.value;

    $.ajax({
        type: "POST",
        url: "NewWebSerivce.asmx/SaveTextBox",
        data: textValue,
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            // do nothing
        }
    });
});
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜