Solved

Show/Hide error message label on masterpage

Posted on 2010-11-11
8
1,252 Views
Last Modified: 2012-05-10
I have a masterpage that is used specifically for logged in users.  In one of my content pages I have many gridviews all within a single wizard control.  I am trying to build in some generic error handling for each of these grids.
I added a label to the master page and then in the rowupdated of the grid I forward to a generic procedure that gets the label and show the exception error.  I haven't tried it, but I assume it works.
Problem is, I want to clear the error from displaying once the user has started editing again.
What is the best way to track clearing the message?
Thanks
Protected Sub grdBrands_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles grdBrands.RowUpdated
        If Not e.Exception Is Nothing Then
            HandleUpdateErrorMessage(e)
        End If
    End Sub

    Public Sub HandleUpdateErrorMessage(ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs)
        Dim lbl As Label = Me.Master.FindControl("lblErrorMsg")
        If Not lbl Is Nothing Then
            If TypeOf (e.Exception) Is ArgumentException Then
                lbl.Text = CType(e.Exception, ArgumentException).Message
            Else
                lbl.Text = "An error occurred while trying to upgrade this record"
            End If

        End If
        e.ExceptionHandled = True
        e.KeepInEditMode = True

    End Sub
    Public Sub HideShowErrorMessage()
        Dim lbl As Label = Me.Master.FindControl("lblErrorMsg")
        If Not lbl Is Nothing Then
            If lbl.Visible Then
                lbl.Visible = False
            Else
                lbl.Visible = True
            End If
        End If

    End Sub

Open in new window

0
Comment
Question by:Sheritlw
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 3

Expert Comment

by:Epitel0920
ID: 34114139
Add an onclick event to make the Control hidden..
0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 34114140
If you can wait until the page posts back to clear the message, you can just set the error message text to String.Empty in the Page_Load event. That's how I typically handle information text labels.
0
 
LVL 3

Expert Comment

by:mortimer452
ID: 34114154
My suggestion would be to set the lbl.Visible = True in the Page_Load event on every page load, that way the first thing that happens on every postback is to clear the previous error message.

If something later on in the page execution causes an error, set the label text and visible = true to display the message.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 13

Expert Comment

by:AngryBinary
ID: 34114259
If you want a dynamic solution, there are a lot of ways to skin the cat. One would be to write a javascript method that sets the 'onchange' event of all the input fields on the page to clear the text label:

Dim lbl As Label = Me.Master.FindControl("lblErrorMsg")
Dim script As String = "function clearErrors() {" _
& "   document.getElementById('" & lbl.ClientId & "').innerHTML = '';'" _
& "}" _
& "var inputs = document.getElementsByTagName('input');" _
& "for (var i = 0; i < inputs.length; i ++) {" _
& "    var element = inputs[i];" _
& "    var old = (element.onchange) ? element.onchange : function () {};" _
& "    element.onchange = function () {old(); clearErrors()};" _
& "}"
Page.RegisterStartupScript(Me.GetType(), "clearErrors", script)

You could attach a similar method to events specific to the control that caused the error if you were to capture that information when the error is reported.
0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34114591
How about adding a method sub to handle the "OnRowEditing" event on each of your grid views.  Then inside the event clear out lbErrorMessage.  This would be exactly what you want.  As soon as the user clicks the edit button on one of the rows, the error is cleared.
0
 

Author Comment

by:Sheritlw
ID: 34114706
Hi P1ST0LPETE,

Could you give me an example of how I would handle that?

Thanks
0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 total points
ID: 34114998
My solution is assuming that you are using the standard editing that is provided by the GridView control.  If you are then every time the user clicks the edit button on a row, then the RowEditing even is fired during the process.  You can tap into this by adding "OnRowEditing" as shown below:

        <asp:GridView ID="GridView1" runat="server" OnRowEditing="GridViewRowEdit">
            <Columns>
                <asp:CommandField ShowEditButton="true" EditText="EditRow" />
                <asp:BoundField Visible="false" DataField="ID" />
            </Columns>
        </asp:GridView>

Then on your code behind you would have a method to handle like this:

    Protected Sub GridViewRowEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
        Dim lbl As Label = Me.Master.FindControl("lblErrorMsg")
        lbl.Text = ""
    End Sub
0
 

Author Closing Comment

by:Sheritlw
ID: 34115018
Quick and simple.
Thank you very much
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to change the view engine in visual studio 2015 enterprises edition 7 48
Regex validation 2 24
ASP.NET MVC identity 6 26
VB.Net Tranfer data between to VB.Net apps 8 25
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question