简体   繁体   中英

How to show “success” message on submit?

Using C# with ASP.NET, how do I show a "success" message when my user submits a form? And at the same time say "The image has successfully saved", with a link, so that the image created can be viewed by clicking the link?

Wrap your form in an <asp:Panel> and create another <asp:Panel> with Visible="False" for your Thank you message. Once the form is submitted, change the visibility of each panel, setting the form to Visible="False" , and the thank you message panel to Visible="True" .

Hope that makes sense, here's an example:

<asp:Panel ID="pnlFormFields" runat="server">
    ... form fields here ...
</asp:Panel>

<asp:Panel ID="pnlThankYouMessage" runat="server" Visible="False">
    ... Thank you message here ...
</asp:Panel>

Then inside your codebehind

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

If you need label to display message. Add a label on the page and set its attribute visible = false in aspx and use the code below:

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;
}

For consistency you can use Transaction in above code so as to prevent save operation if image upload fails. Otherwise its your choice. The new code with Transaction will be , given below:

 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()
}

Here to refer transaction scope, add reference System.Transactions.

İf you want to show message on client side controls, like alert("saccess"); you can use ajax and webmethod in Why doesn't my jQuery code work in Firefox and Chrome? if you want to show message on server side you can use panel, label or div (runat server and have id) and default setting of them, set visiible false, when you show message you can set visible true via code behind..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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