简体   繁体   English

保持html表单在asp.net按钮上回发

[英]Keep html form open on asp.net button postback

I have an html page, where you click a button and an html form with some fields and a button opens up. 我有一个html页面,您在其中单击一个按钮,并打开一个包含一些字段和按钮的html表单。 The button in the form is an asp:Button with a method in the code behind to handle to click. 表单中的按钮是一个asp:Button,后面的代码中有一个方法来处理单击。 When I click the button, the page postsback and the form is gone. 当我单击按钮时,页面回发并且表单消失了。 How can I keep the form from closing on postback? 如何防止表单在回发时关闭? Currently the form is opened through javascript when the html button on the main page is clicked. 当前,当单击主页上的html按钮时,将通过javascript打开该表单。

I tried putting the form in an update panel, this makes the postback much smoother but still closes the html form. 我尝试将表单放在更新面板中,这使回发更加顺畅,但仍然关闭了html表单。

<asp:Button class="btn btn-success btn-sm"  id="letsTalkVerify"  href="#" onclick="SendSms" runat="server" Text="Send SMS"></asp:Button>

Asp:Button Asp:按钮

<form id="letsTalkForm" action="#" runat="server">

html form that needs to stay open on postback 需要在回发时保持打开状态的html表单

As I understand due to postback your page gone references and the existing data on the page is removed. 据我了解,由于回发,您的页面已消失,并且页面上的现有数据也被删除。

If you don't want to lose your data in the same condition I mean postback you've to use UpdatePanel on the page. 如果您不想在相同情况下丢失数据,我的意思是回发,您必须在页面上使用UpdatePanel。

An UpdatePanel is a set of components that you want to be affected by ajaxified updates in your webapplication. UpdatePanel是一组您希望受到Web应用程序中更新更新影响的组件。 The triggers are what causes the panel of components to update. 触发器是导致组件面板更新的原因。 If you want to know more about asp.net ajax and update panel please read How Ajax for ASP.NET updatepanels work. 如果您想了解有关asp.net ajax和更新面板的更多信息,请阅读ASP.NET updatepanels的Ajax工作原理。

See example: 参见示例:

Before add UpdatePanel on the page you have to add ' ScriptManager ' on the page or master page like: 在页面上添加UpdatePanel之前,必须在页面或母版页上添加“ ScriptManager ”,例如:

<asp:ScriptManager ID="ScriptManager" runat="server" />

Then after put UpdatePanel tag on the page like: 然后将UpdatePanel标记放在页面上后,如下所示:

<asp:UpdatePanel ID="updatePanel3" runat="server">
<ContentTemplate>
</ContentTemplate>
<Triggers>                      
     <asp:AsyncPostBackTrigger ControlID="yourbuttonid" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

NOTE: 注意:

1) Your HTML or data which you don't want to lose on button click (postback) but under ContentTemplate with your button. 1)您不想在按钮上丢失的HTML或数据单击(回发),但是在ContentTemplate下使用您的按钮。 2) Set your button id under AsyncPostBackTrigger this will handle your request without referencing your page. 2)在AsyncPostBackTrigger下设置按钮ID,这将处理您的请求而无需引用您的页面。

You can choose to do it via jquery ajax() 您可以选择通过jquery ajax()

$("#letsTalkVerify").click(function(e){
    e.preventDefault();
    //YOUR CODE VIA AJAX
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM