如何在javascript函数中调用c#web Page_Load()

问题描述:

需要在javascript函数中调用当前网页的Page_Load()方法而不清除页面中的值。目前我正在使用window.location.reload();但它会清除当前页面中的值。



Need to call current webpage Page_Load() method in javascript function without clearing values in the pages. Currently i am using window.location.reload(); but it clears the values in my current page.

function AssignPatIDtoHiddenField(PatID) {
      
      document.getElementsByName('HiddenField1')[0].value = PatID;     
 
      window.location.reload();     
 
  }





这里我将值分配给hiddenfield但是一旦页面重新加载,隐藏字段值就变为空。我尝试使用会话变量相同的问题。请帮助我



Here i am assigning value to hiddenfield but once the page is reload the hiddenfield value is becoming empty. i tried using session variable same problem. Pls help me

试试这样



在页面和点击中创建隐藏按钮在服务器上调用该事件,调用页面加载。



Try like this

Create a hidden button in the Page and on the click event on the server, call the page load.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        function ReloadPatientDemo(PatID) {

            document.getElementsByName('HiddenField1')[0].value = PatID;
            document.getElementById('btnHidden').click();

        }


    </script>
</head>
<body>
    <form id="frm" runat="server">
     <asp:Button ID="btnHidden" runat="server" Style="visibility: hidden" OnClick="btnHidden_Click" />
    </form>
</body>
</html>













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

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


调用重新加载只会重新加载页面。你想要的是使用JavaScript进行回发。网上有很多可用的例子,这里有几个例子.b
=http://msdn.microsoft.com/en-us /library/aa720099(v=vs.71).aspx\">生成用于回发的客户端脚本 [ ^ ]

使用JavaScript进行ASP.NET回发 [ ^ ]

回发如何在ASP.NET中运行 [ ^ ]

在真实环境中使用doPostBack [ ^ ]



在Google中可以找到更多的例子。
Calling reload will just reload the page. What you want is to do a Postback using JavaScript. There are plenty of examples on the web available for that, here are a few

Generating Client-Side Script for Postback[^]
ASP.NET postback with JavaScript[^]
How postback works in ASP.NET[^]
Usage of doPostBack in a Real Environment[^]

There are plenty of more examples to find in Google.