【问题标题】:How to display a message box between the screen when user try to insert a new record/duplicate record in asp.net当用户尝试在 asp.net 中插入新记录/重复记录时如何在屏幕之间显示消息框
【发布时间】:2016-12-16 13:47:58
【问题描述】:

我正在使用 c# 在 asp.net 中创建个人资料网站。我有一个联系页面,其中有 ID、姓名、手机、电子邮件、消息文本框和取消、提交按钮。我想在用户尝试插入重复记录时显示一条消息,并且我想在用户插入新记录时显示一条消息,并且该消息应显示在页面之间。

protected void btnInsert_Click(object sender, EventArgs e)
{
    if (txtId.Text != "" && txtName.Text != "" && txtMobile.Text != "" && txtEmail.Text != "" && txtMessage.Text!="")
    {
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Sp_Insert_ContactInfo";
        cmd.Parameters.AddWithValue("@Id", txtId.Text);
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@Message", txtMessage.Text);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        int ctr = 0;

        try
        {
            con.Open();
            ctr = cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception er)
        {
            lblResult.Text = "error!" + er.Message;
        }
        finally
        {
            if (ctr > 0)
            {
                clear_Textbox();
                //FillEmps();
            }
            con.Close();
        }
    }
}

【问题讨论】:

  • “页面之间”在哪里?
  • Sp_Insert_ContactInfo 是做什么的?
  • 它是一个插入存储过程名称,介于两者之间意味着我想在页面之间显示一个消息框,但这对我来说不是强制性的。

标签: c# asp.net


【解决方案1】:

考虑到您的问题只是与插入新记录或插入重复记录后弹出消息有关,您可以这样做:

方法一:

Response.Write("<script type=\"text/javascript\">alert('Messagebox Pop up Alert!!!');</script>");

方法二:

    lblResult.Text =
        "<script language='javascript'>" + Environment.NewLine +
        "window.alert('" + Message + "')</script>";
    Page.Controls.Add(lblMessageBox);

你也可以参考这些帖子:

How to create a pop-up message box to display error message in .net c# web-applications

How to display an error message box in a web application asp.net c#

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2017-02-14
    • 2023-04-11
    • 2019-08-07
    • 2019-12-14
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多