ASP.NET异步回发的ASP:按钮点击 [英] ASP.NET asynchronous post-back on asp:button click

查看:247
本文介绍了ASP.NET异步回发的ASP:按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AJAX的ASP:净,我试图处理的情况下一个asp:按钮同时触发一个JavaScript方法和codebehind C#方法。要做到这一点,我使用的onclick和按钮的OnClientClick属性。我需要一个回传发生在codebehind被称为然而,这回发会导致JavaScript来无法工作,因为变量已经失去了状态。任何人都可以描述了如何处理呢?可能与Ajax和异步回发?它开始推动我有点疯狂!

I am using AJAX in asp:net and am trying to handle a situation where an asp:button triggers both a javascript method and a codebehind c# method. To do this I am using onclick and onClientClick attributes for the button. I need a postback to occur for the codebehind to be called however this postback causes the javascript to not work because variables have lost state. Can anyone describe how to handle this? Possible with ajax and async postbacks? It's starting to drive me a bit crazy!

编辑: JavaScript的实际上是一个谷歌地图(第三版)中,C#code是提交到SQL数据库。

The javascript is actually for a google-map (v3), the c# code is for submitting to an sql db.

该地图是在$ C $初始化C-身后:

The map is initialized in the code-behind with:

protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack) {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "initialize_map();", true);
            submitButton.Command += new CommandEventHandler(this.submitButton_Click);
        }
    }

下面是一些相关的code:

Here is some of the relevant code:

<asp:UpdatePanel runat="server" ID="Form" UpdateMode="Conditional">
<ContentTemplate>

(...form stuff...)

<asp:Button ID="submitButton" runat="server" Text="Submit" ValidationGroup="NewCallFormSubmit" OnClientClick="calcRoute(); return false;" onclick="submitButton_Click" />

</ContentTemplate>
</UpdatePanel>

<asp:UpdatePanel runat="server" ID="DisplayUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>

 <h1>
  Pick-Up and Drop-Off Locations
  </h1>
     <!-- Map Layout -->
     <div id="map_canvas" style="width: 500px; height: 500px;"></div>                          

</ContentTemplate>
</asp:UpdatePanel>

...我认为这是可行的,如果我能得到第一的UpdatePanel回发的按钮点击,第二面板不回发,按钮仍然调用codebehind。到目前为止,没有运气,这一切都发生在同一时间。

...I think it would work if I could get the first updatepanel to postback on the button click, the second panel not to postback, and the button to still call the codebehind. So far no luck with this all happening at the same time.

推荐答案

如果你只是想在你的OnClick(服务器)code运行的OnClientClick(客户端)code,请尝试以下操作:

If you simply want to run your OnClientClick (client) code before your OnClick (server) code, try the following:

ASP.NET

<asp:Button ID="btn_mybutton" runat="server" Text="MyButton" OnClientClick="return JSFunction();" OnClick="CFunction"/>

Javascript的

<script type="text/javascript">
    function JSFunction() {
        alert('Some JavaScript stuff');
        return true; 
    }
</script>

C#

protected void CFunction(object sender, EventArgs e)
{
    // Some server code
}

JavaScript函数首先执行,并从服务器code被执行前返回。

The JavaScript function is executed first and returned from before the server code is executed.

这篇关于ASP.NET异步回发的ASP:按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆