【问题标题】:Display message without reloading the page in html显示消息而不用 html 重新加载页面
【发布时间】:2015-01-29 10:24:15
【问题描述】:

我有一个带有提交按钮的 html 表单,其中包含姓名、电子邮件、联系方式和反馈字段。

我的html表单代码是

<div class="feedback">
    <a id="feedback_button">Feedback</a>
    <form method="post" action="http://localhost:12459/WebsiteForm/WebSiteFeedbackProcessing.aspx"
    name="new_post">
    <div class="form">
        <div>
            <div>
                <input type="text" class="text_field" id="fname" name="fname" placeholder="First Name"
                    style="margin-bottom: 3px; width: 50%;" maxlength="20" />
            </div>
            <div>
                <input type="text" class="text_field" id="email" name="email" placeholder="Email"
                    style="margin-bottom: 3px; width: 50%;" maxlength="30" />
            </div>
            <div>
                <input type="text" class="text_field" id="contact" name="contact" placeholder="Contact No"
                    style="margin-bottom: 3px; width: 50%;" maxlength="15" />
            </div>
            <textarea id="feedback" name="feedback" placeholder="feedback" style="min-height: 41px;
                width: 48%;" maxlength="100" rows="" cols=""></textarea>
        </div>
        <input type="submit" value="Send" class="sendfeedback" />
    </div>
    </form>
</div>

我正在将这四个值保存到 sql server 数据库中。

点击提交按钮后,表单进入另一个asp.net c#项目(代码如下)进行数据保存。 这里通过函数SaveFeedback,它以整数值的形式返回结果,1表示成功,0表示失败。

我的代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using CorporateOnlineTest.BAL;
using System.Text;

namespace CorporateOnlineTest.Web.WebsiteForm {
  public partial class WebSiteFeedbackProcessing: System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
      string fname = "";
      string contact = "";
      string email = "";
      string feedback = "";

      for (int i = 0; i < Request.Form.AllKeys.Length; i++) {
        string key = Request.Form.AllKeys[i] + "";
        string value = Request.Form[i];

        if (key + "" == "fname") {
          fname = value;
        } else if (key + "" == "contact") {
          contact = value;
        } else if (key + "" == "email") {
          email = value;
        } else if (key + "" == "feedback") {
          feedback = value;
        }
      }

      //Insert in database and send mail.
      if (Request.Form.AllKeys.Length > 0) {
        int Submit;

        SubAdminBAL objSubadmin = new SubAdminBAL();
        Submit = SaveFeedback(fname, contact, email, feedback);

        if (Submit == 1) {
          ScriptManager.RegisterStartupScript(this, this.GetType(),
            "alert",
            "alert('Your Feedback Has Been Sent Successfully!');window.location ='http://localhost:2421/Psychometricachat_ByKaustubh/p/online-managerial-skill-test.html';",
            true);
        } else {
          ScriptManager.RegisterStartupScript(this, this.GetType(),
            "alert",
            "alert('Please Try Again!');window.location ='http://localhost:2421/Psychometricachat_ByKaustubh/p/online-managerial-skill-test.html';",
            true);
        }
      }
    }

    [WebMethod]
    public int SaveFeedback(string fname, string contact, string email, string feedback) {
      int Result = 0;
      SubAdminBAL bl = new SubAdminBAL();

      Result = Convert.ToInt32(bl.SaveWebsiteFeedbackBAL(fname, contact, email, feedback));

      return Result;

    }
  }
}

我想在我的 html 页面上显示这些消息而不重新加载该页面。

我尝试了很多选项,但每次我的页面在消息显示之前或之后重新加载。

有没有人有解决办法。请尽早恢复。

提前致谢

【问题讨论】:

  • 我认为您正在寻找 AJAX。
  • 请添加你的js代码。为此,您需要使用 ajax 调用。
  • 用javascript提交表单并通过ajax将表单数据发送到服务器
  • 我是所有这些平台的新手,所以请尽可能提供代码 sn-ps。

标签: javascript c# jquery html asp.net


【解决方案1】:

您可以使用 jQuery .ajax 保存反馈,类似于以下代码:

jQuery.ajax() 执行异步 HTTP (Ajax) 请求。 More

$('.btnSaveFeedback').on('click', function () {
    var fname = $('#fname').val();
    var contact = $('#contact').val();
    var email = $('#email').val();
    var feedback = $('#feedback').val();

    $.ajax({
        url: "/WebSiteFeedbackProcessing.aspx/SaveFeedback", // your Page, WenMethod, Handler,...
        type: 'post',
        data: {
            fname: fname,
            contact: contact,
            email: email,
            feedback: feedback
        },
        success: function (result) {
            alert('Successfully!!');
        },
        error: function (result) {
            alert('Failed!!');
        }
    });
});

【讨论】:

  • 感谢您的回复。我在我的项目中使用了上面的 ajax 脚本,但它向我显示了成功和失败的警报消息。此外,在调试代码时,我的页面也没有重定向到 /WebSiteFeedbackProcessing.aspx/SaveFeedback 函数。当然我的数据不会保存在数据库中。
  • 你是对的,complete 事件在任何successerror 状态之后运行。 success 成功的 ajax 请求。 error ajax 请求失败。
【解决方案2】:

你可以使用ajax方法(jquery ajax也可以)

参考-

Basic of jquery ajax

Jquery ajax tutorial

【讨论】:

  • 为了更好的答案,你可以放一些代码和描述。
  • @Mohamadshiralizadeh Dude.. 从一开始就很难向那些曾经使用过 ajax 方法的人解释。这就是为什么我发送链接仅供参考n试一试。
【解决方案3】:

您是否使用 Scriptmanager 启用了页面方法?

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

【讨论】:

    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2015-12-26
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多