Tuesday, 25 October 2016

How to call c# web Page_Load() in javascript function

How to call c# web Page_Load() in javascript function

Description:

In this example we explain that how to call Page_Load post back from JavaScript or how to call C# web Page_Load () in JavaScript function or raising post back using _doPostBack () function from JavaScript. Need to call Page_Load () method in JavaScript without clearing values in the pages. If we use JavaScript window.location.reload () then it will call page load () but clears all the value.so to overcome this problem here we create one hidden button in the web page and call its click event from the JavaScript so the page load will automatically call.

Aspx Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>how to call page_load postback from javascript</title>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        function ReloadPageLoad(ID) {

            document.getElementsByName('hdntemp')[0].value = ID;
            document.getElementById('btnHidden').click();

        }


    </script>
</head>
<body>
    <form id="frm" runat="server">
     <asp:Button ID="btnHidden" runat="server" Style="visibility: hidden" OnClick="btnHidden_Click" />
      <asp:FileUpload ID="FileUpload1" runat="server" webkitdirectory OnChange="ReloadPageLoad(this.id)" />
     <asp:HiddenField ID="hdntemp" runat="server" />
    </form>
</body>
</html>



Aspx.cs Page: 

  protected void Page_Load(object sender, EventArgs e)
        {
            // do something....
        }

        protected void btnHidden_Click(object sender, EventArgs e)
        {
            Page_Load(null, null);
        }



0 comments:

Post a Comment