如何在提交时显示“成功”消息?

在ASP.NET中使用C#,当用户提交表单时如何显示“成功”消息? 并同时说“图像已成功保存”,带有链接,以便通过单击链接查看创建的图像?

将表单包装在并使用Visible="False"创建另一个以获取感谢信息。 提交表单后,更改每个面板的可见性,将表单设置为Visible="False" ,并将感谢消息面板设置为Visible="True"

希望这是有道理的, 这是一个例子:

  ... form fields here ...   ... Thank you message here ...  

然后在你的代码隐藏中

 protected void btnSubmit_Click(object sender, EventArgs e) { // Hook up uploaded image and assign link to it pnlFormFields.Visible = false; pnlThankYouMessage.Visible = true; } 

如果您需要标签来显示消息。 在页面上添加标签并在aspx中设置其属性visible = false并使用以下代码:

 protected void btnSubmit_Click(object sender, EventArgs e) { if(SaveRecordsToDataDatabase()) { If(UploadImage()) { showMessage("Save successfull",true); } else { showMessage("Save failed",false); } } else { showMessage("Save failed",false); } } private bool UploadImage() { // you upload image code.. } private bool SaveRecordsToDatabase() { // db save code } private void showMessage(string message, bool success) { lblMsg.visible = true; // here lblMsg is asp label control on your aspx page. lblMsg.FontBold = true; if(success) lblMsg.ForeColor = Color.Green; else lblMsg.ForeColor = Color.Green; lblMsg.Text = message; } 

为了保持一致性,您可以在上面的代码中使用Transaction,以便在图像上载失败时阻止保存操作。 否则它是你的选择。 具有Transaction的新代码将在下面给出:

  protected void btnSubmit_Click(object sender, EventArgs e) { using(TransactionScope scope = new TransactionScope()) { if(SaveRecordsToDataDatabase()) { If(UploadImage()) { showMessage("Save successfull",true); } else { showMessage("Save failed",false); } } else { showMessage("Save failed",false); } } scope.complete() } 

这里引用事务范围,添加引用System.Transactions。

如果你想在客户端控件上显示消息,比如alert(“saccess”); 你可以使用ajax和webmethod 为什么我的jQuery代码不能在Firefox和Chrome中运行? 如果你想在服务器端显示消息,你可以使用面板,标签或div(runat服务器并具有id)和默认设置,设置visiible false,当你显示消息时,你可以通过后面的代码设置可见的真实。

使用标签(visible = false)和工具箱中的超链接。当您上传图像时,您必须将保存文件位置的URL插入数据库。因此,当触发插入查询时将返回一个整数值,即d no插入db.compare中的行,如果该值> 0,则将label的visibily设置为true,并且label.text =“success”最后将超链接的导航URL设置为已保存图像的d url,可用于创建查看图像的链接