How to show “success” message on submit?

前端 未结 4 737
再見小時候
再見小時候 2020-12-20 20:27

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 t

4条回答
  •  独厮守ぢ
    2020-12-20 20:33

    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.

提交回复
热议问题