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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

İ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..

Method 4

use a label(visible=false) and a hyperlink from the toolbox .when u upload an image u must be inserting the url of savefile location to a database.so when that insert query is fired that will return an integer value which is d no of lines inserted in db.compare it like if that value > 0 then set visibily of label to true and label.text=”success” finally set the navigate url of the hyperlink to d url of saved image which can be used to creat a view link of the image


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x