Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any native way in ASP.NET to do a "success message"?

Say you have something like an ASP.NET ASP:DetailsView to show and edit a single record in a database.

It's simple to record the error cases... you add validation and a validation summary. When your update form fails validation it naturally makes noise: it shows the validation message and/or the validation summary. Not a single code behind is required.

But then, you pass validation, and it makes your update completely silently. There's no sense that anything happened, and there doesn't seem to be any default settings to make a success message without code-behinds.

But, even code-behinds are confusing. What event should show the success message? onItemUpdate, right? Fine, but then let's say you make another change and get a validation error? Your success message stays. I wasn't able to find an event that would reliably turn off an existing success message if there were a validation error.

This should be web development 101! Why is it so hard?

EDIT:

Someone suggested using the ItemCommand event... I tried this and many other events, but that success message just won't disappear. Here's some code.

My message in ASP.NET

<label id="successMessage" class="successMessage" runat="server"></label>

And my DataView tag (simplified):

    <asp:DetailsView 
        Id="EditClient"
        DataKeyNames="LicenseID" 
    DataSourceID="MySource"
    runat="server" 
        OnItemUpdated="SuccessfulClientUpdate"
        OnItemCommand="ClearMessages">

And, my code-behind:

protected void SuccessfulClientUpdate(object sender, DetailsViewUpdatedEventArgs e)
{
    successMessage.InnerText = string.Format("Your changes were saved.");
    successMessage.Visible = true;
}

protected void ClearMessages(object sender, DetailsViewCommandEventArgs e)
{
    successMessage.InnerText = string.Empty;
    successMessage.Visible = false;
}

Once I do a successful update, however, nothing seems to make that message disappear, not even failed validation.

2nd EDIT:

Just want to be clear that I did try putting the ClearMessages code in Page_Load. However, nothing seems to make that successMessage label disappear when I hit update a 2nd time WITH a validation error. Can anyone suggest any other troubleshooting tips?

like image 597
danieltalsky Avatar asked Dec 12 '08 04:12

danieltalsky


People also ask

How do I show messages in view?

we can easily show the tempdata message in view all we need to do is binding the message to tempdata. View Code : In view we need to check whether “Tempdata” is null or not , if it is not null then put in any html tags like p , span ,etc..


3 Answers

As far as I know, there is no native way of doing this. You may rant about it, maybe Microsoft will hear it :).

Resetting the "success message" on Page_Load, or wherever in your code-behind, won't work. This is because ASP.NET validation is usually done both client and server-side. This means for every validation control you put on the page, ASP.NET generates some client-side Javascript that does the validation and renders the error on the client, without going back to the server. So you're stuck with both the success message and the error message at the same time.

What you can do about it:

  • place a <div> control on your page, that would show up the success message (as already suggested by others above). Whenever you update something (in server-side code), show up the control and set a meaningful "Successful!" message text.
  • register a custom Javascript function that would lookup the <div> and hide it on every page submit. Be aware that the function needs to be called before the autogenerated client script that does the validation.

If you look at the client source of an ASP.NET page (with validators on it), here's what you can find:

<form name="aspnetForm" method="post" action="MyPage.aspx" onsubmit="javascript:return WebForm_OnSubmit();id="aspnetForm">

The WebForm_OnSubmit is generated by ASP.NET and calls the javascript that does the validation. Sample:

function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
    return false;
return true;
}

To register your custom code that hides the success message, you should place (in your code-behind) something along these lines:

if (!Page.ClientScript.IsOnSubmitStatementRegistered("ClearMessage"))
{
    string script = @"document.getElementById('" + 
        yourDivControl.ClientID + "').style.display='none'";
    Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "ClearMessage", script);
}

This will turn your page's autogenerated WebForm_OnSubmit into the following:

function WebForm_OnSubmit() {
    document.getElementById('ctl00_yourDivControl').style.display='none';
    if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
        return false;
    return true;
}

The effect: On every postback (e.g. when ItemCommand is triggered, or when some Save button is clicked, or anything else), you will show up the div control with the "success" message. On the next postback, just before submitting the page to the server, this message is cleared. Of course, if this postback also triggers a "success", the message is shown again by the code-behind on the server. And so on and so forth.

I hope the above is helpful. It's not the full-blown solution, but it gives sufficient hints to point you in the right direction.

like image 199
Dan C. Avatar answered Nov 14 '22 15:11

Dan C.


Use the ItemCommand event and perform reset to clean state for every command that is given. Then if validation fails on update, the validation error will be displayed. If it succeeds, it will show the success message. Subsequent, button clicks will invoke the ItemCommand event and you can reset again to start fresh.

like image 41
tvanfosson Avatar answered Nov 14 '22 15:11

tvanfosson


Daniel, I needed something similar so I derived from a Label and bound it directly to the events raised by the various DataSourceControl derivatives (LinqDataSource, ObjectDataSource, SqlDataSource). I also used client-side JavaScript to hide the label during validation and/or form submission (like Dan.C's answer).

The control exposes separate message text properties for successful Deleted, Inserted and Updated events.

I've published the commented source for the solution at http://codehq.net/files/UpdateSuccessMessage.zip

Hope this helps.

like image 40
devstuff Avatar answered Nov 14 '22 14:11

devstuff