ASP.NET 回发 asp:button 单击

发布于 2024-12-27 05:34:03 字数 1451 浏览 2 评论 0原文

我在 asp:net 中使用 AJAX,并尝试处理 asp:button 触发 JavaScript 方法和代码隐藏 C# 方法的情况。为此,我使用按钮的 onclick 和 onClientClick 属性。我需要发生回发才能调用代码隐藏,但是此回发会导致 javascript 无法工作,因为变量已丢失状态。谁能描述如何处理这个问题?可以使用 ajax 和异步回发吗?它开始让我有点疯狂了!

编辑: javascript 实际上用于 google-map (v3),c# 代码用于提交到 sql 数据库。

地图在代码隐藏中初始化为:

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);
        }
    }

这是一些相关代码:

<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 在按钮单击时回发,第二个面板不回发,并且按钮仍然调用隐藏代码。到目前为止,这一切都没有同时发生。

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!

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

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);
        }
    }

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>

...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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2025-01-03 05:34:03

如果您只想在 OnClick(服务器)代码之前运行 OnClientClick(客户端)代码,请尝试以下操作:

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 函数首先执行并在服务器代码执行之前返回。

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
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文