Search code examples
c#jqueryasp.netpageload

pass data from client side to server before page_load executes


A button on html page redirect to aspx page with window.open() command. There are certain data on the html page which i want on server-side of the aspx page before page_load executes.

following is my code on the html page which redirects to the aspx page

var win = window.open('mypage.aspx','_self');
win.args=this.Args;

following is my code on aspx page which tries to catch the data passed from the html page

<script type='text/javascript'>

 var readyStateCheckInterval = setInterval(function () {
            if (document.readyState === "loading") {
                $('#hdnArgs').val(window.args);
                clearInterval(readyStateCheckInterval);
            }
        }, 100);

</script>

<input type='hidden' id='hdnArgs' runat='server'/>

Following is the code on the aspx.cs file which tries to read the hidden variable's value which has been set from the data of the html page

protected void Page_Load(object sender, eventargs e)
{
    string data = hdnArgs.value; //this is always null
}

But what I get is always 'null'.

The readyStateCheckInterval sets the hidden variable value after the page_load event is completed.

I want the value of hidden variable before page_load. How can I get that?


Solution

  • Its not possible to set value of any control before page life cycle finish.

    let me explain you.. You try to set value on hdnArgs but In Page lifecycle control only generate and send it to browser only after finish Page_init,Page_Load,Page_Load_Complete All those methods..

    So,When you try set args value to hdnArgs using $('#hdnArgs').val(window.args); Page_load event already completed..

    Solution

    I think you need to pass value as a QueryString to get Value in Page_load

    var win = window.open('mypage.aspx?Args='+this.Args,'_self');
    

    and In ASPX page

    protected void Page_Load(object sender, eventargs e)
    {
    string data = Request.QueryString["Args"];
    }
    

    Also you can send data using Page Postback if you have large data.