我正在使用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();
}
}
}
答案 0 :(得分:2)
考虑到您的问题与插入新记录或插入重复记录后弹出消息有关,您可以执行以下操作:
方法1:
Response.Write("<script type=\"text/javascript\">alert('Messagebox Pop up Alert!!!');</script>");
方法2:
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#