在更新面板中提交表单后运行javascript? [英] Run javascript after form submission in update panel?

查看:84
本文介绍了在更新面板中提交表单后运行javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我抓狂!我已经阅读了至少5个与我的问题密切相关的问题,并且可能只有5个左右的网页来自谷歌搜索。我只是不明白。

This is driving me crazy! I have read at least 5 questions on here closely related to my problem, and probably 5 or so more pages just from googling. I just don't get it.

我试图在用户填写表格提交注册然后重定向到另一个表格后出现一个jqueryui对话框页面,但我不能为我的生活得到任何JavaScript工作,甚至没有一个警报。

I am trying to have a jqueryui dialog come up after a user fills out a form saying 'registration submitted' and then redirecting to another page, but I cannot for the life of me get any javascript to work, not even a single alert.

这是我的更新面板:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upForm" runat="server" 
    UpdateMode="Conditional" ChildrenAsTriggers="False">
    <ContentTemplate>
               'Rest of form'
         <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> 
              <p>Did register Pass? <%= registrationComplete %></p>
    </ContentTemplate>
</asp:UpdatePanel>

我想执行的Jquery :(现在它位于标记的头部, autoOpen设置为false)

The Jquery I want to execute: (Right now this is sitting in the head of the markup, with autoOpen set to false)

<script type="text/javascript"> 
function pageLoad() {

    $('#registerComplete').dialog({
        autoOpen: true,
        width: 270,
        resizable: false,
        modal: true,
        draggable: false,
        buttons: {
          "Ok": function() {
          window.location.href = "someUrl";
        }
        }
    });

}
     </script>

最后我的代码背后:(注释掉我试过的所有东西)

Finally my code behind: ( Commented out all the things I've tried)

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click     
      'Dim sbScript As New StringBuilder()'
           registrationComplete = True
            registrationUpdatePanel.Update()

            'sbScript.Append("<script language='JavaScript' type='text/javascript'>" + ControlChars.Lf)'
            'sbScript.Append("<!--" + ControlChars.Lf)'
            'sbScript.Append("window.location.reload()" + ControlChars.Lf)'
            'sbScript.Append("// -->" + ControlChars.Lf)'
            'sbScript.Append("</")'
            'sbScript.Append("script>" + ControlChars.Lf)'

            'ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "AutoPostBack", sbScript.ToString(), False)'
            'ClientScript.RegisterStartupScript("AutoPostBackScript", sbScript.ToString())'
                            'Response.Write("<script type='text/javascript'>alert('Test')</script>")'
            'Response.Write("<script>windows.location.reload()</script>")'
End Sub

我试过:


  • 从服务器传递变量通过head标记的
    javascript块中的内联<%=%>来
    客户端。

  • Passing variables from server to client via inline <%= %> in the javascript block of the head tag.

将相同的代码放入一个脚本

标记在updatePanel内。

Putting that same code in a script
tag inside the updatePanel.

试图使用

RegisterClientScriptBlock和

RegisterStartUpScript

Tried to use
RegisterClientScriptBlock and
RegisterStartUpScript

只需写入带有
脚本标记的Response.Write。

Just doing a Response.Write with the script tag written in it.

尝试将
整个jquery.dialog代码放在
registerstartup脚本中的各种组合,或只是尝试更改autoOpen的


属性,或者只是在
上调用open。

Tried various combinations of putting the entire jquery.dialog code in the registerstartup script, or just
trying to change the autoOpen
property, or just calling "open" on
it.

我不能甚至得到一个简单的提醒来处理任何这些,所以我做错了但我只是不知道它是什么。

I can't even get a simple alert to work with any of these, so I am doing something wrong but I just don't know what it is.

这就是我所知道的:


  • 在异步回发中,Jquery正确绑定甚至
    ,因为div
    容器就是对话框是
    总是看不见的,我在这里看到一个类似的
    帖子说明造成
    的问题,这不是这里的情况。

  • The Jquery is binding properly even on async postbacks, because the div container that is the dialog box is always invisible, I saw a similiar post on here stating that was causing an issue, this isn't the case here.

使用page_load而不是
document.ready,因为假设
在async和normal上运行,
回发,所以isn'问题。

Using page_load instead of
document.ready since that is supposed to run on both async and normal
postbacks, so that isn't the issue.

更新面板正在更新
,因为< p>注册了
Pass ? <%= registrationComplete
%>< / p>

提交表单后更新为true。

The update panel is updating correctly because <p>Did register Pass? <%= registrationComplete %></p> updates to true after I submit the form.

那么我怎样才能做到这一点?我想要的只是 - >点击更新面板内的提交按钮 - >运行服务器端代码以验证表格并插入到数据库 - >如果一切顺利,请弹出jquery(模态)对话框,说它有效。

So how can I make this work? All I want is -> click submit button inside an update panel -> run server side code to validate form and insert into db -> if everything succeeded, have that jquery (modal) dialog pop up saying hey it worked.

推荐答案

要从Codebehind注册,这里是格式

To register from the Codebehind, here's the format:

ClientScript.RegisterStartupScript(GetType(), "AutoPostBackScript", 
                                   "alert('hi');", True);

最后的 true 告诉它将您的代码包装在< script> 标签中,如果您想自己制作标签,请将其关闭。

The true on the end tells it to wrap your code in <script> tags, leave it off if you want to do the tags yourself.

这篇关于在更新面板中提交表单后运行javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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