如何在 javascript 函数中触发 updatepanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6856315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:29:17  来源:igfitidea点击:

how to trigger updatepanel within a javascript function

javascriptasp.netupdatepanel

提问by Fer

i have an updatepanel in my asp.net web page. I want to trigger the updatepanel within a javascript function insead of triggering with a button.
In order to do that, i used __doPostBack('myUpdatePanel', '');function. But i think it causes the whole page postback. My document.ready function is also executed when i call this function. I may be missing some points.
Is there any other way to trigger updatepanel within a javascript function?

我的 asp.net 网页中有一个更新面板。我想在 javascript 函数中触发 updatepanel,而不是用按钮触发。
为了做到这一点,我使用了__doPostBack('myUpdatePanel', '');函数。但我认为它会导致整个页面回发。当我调用这个函数时,我的 document.ready 函数也会被执行。我可能遗漏了一些要点。
有没有其他方法可以在 javascript 函数中触发 updatepanel?

回答by Samir Adel

I think if you put a hidden button inside the update panel and you can use the javascript to fire the click of this button, it will do what you want.

我想如果你在更新面板中放置一个隐藏的按钮,你可以使用 javascript 来触发这个按钮的点击,它会做你想做的。

<script type="text/javascript">
        function Update_UpdatePaanel() {
            document.getElementById('<%= YourButton.ClientID %>').click()
        }
    </script>

The button MUST be inside a hidden div and DON'T set visibile="false" because if you set it to false, the control will not render and the javascript will produce errors.

该按钮必须位于隐藏的 div 内,并且不要设置 visibile="false" 因为如果将其设置为 false,则控件将不会呈现并且 javascript 将产生错误。

<div style="display:none">
        <asp:Button ID="YourButton" runat="server" />
    </div>

回答by rkw

Just create a javascript function and execute the generated postback event:

只需创建一个 javascript 函数并执行生成的回发事件:

<%=ClientScript.GetPostBackEventReference(myUpdatePanel, "")%>

The above statement is put on your aspx page, and it references the exact same code generated from the server to cause a postback for your panel. You can use it by putting it inside a function on the client side:

上面的语句放在您的 aspx 页面上,它引用了从服务器生成的完全相同的代码,以导致您的面板回发。您可以通过将它放在客户端的函数中来使用它:

function fncUpdatePanel () {
    <%=ClientScript.GetPostBackEventReference(myUpdatePanel, "")%>;
}

Then you can attach that function to any event on your page (even a mouseover event). This example uses a server side to attach the event:

然后您可以将该函数附加到页面上的任何事件(甚至是鼠标悬停事件)。此示例使用服务器端附加事件:

myUpdatePanel.attributes('onmouseover', 'fncUpdatePanel()')