Solved

Form Validation, The Enter Key and MultiView

Posted on 2009-04-07
4
1,449 Views
Last Modified: 2012-05-06
I have a page that uses the MultiView control.  On page load View1 is visiable and View2 is hidden.

View1 of my MultiView has a single textbox with a button used to check a client ID.  The textbox has several validation controls to insure the ID entered is correct.
View 2 has several textboes and a button to update my database.

Both views are wrap around a single form.

My problem has to do with the validation of View1.  If I hit the "Enter" key on my keyboard, nothing happens.  In order for the validation to work I have to click on the button in the browser.

Now I've heard that you can set a Default Button in the form.  GREAT!  But I have TWO button on the page in the same form.  One in View1 and one in View2.

How can I set the default button based on the View that is currently visable?  Do I need to use "Panels" rather than "Views"?  Any help would be appriciated.
0
Comment
Question by:cdemott33
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:ppittle
ID: 24092347
CDemott33,

To make sure I understand your question, you are trying to seperate your validation logic into seperate groups of controls.  In other words, you want to validate all of the controls in View1 seperatley then the controls in View2, based on which View is visible at the time.

To accomplish this, you can set the ValidationGroup property.  This allows you to group your controls, so that certain controls will be validated and others will not.  Take a look at the attached code snippet for a full example.  
<asp:Button ID="SwitchViewIndex" runat="server" Text="Switch View" OnClick="SwitchViewIndex_Click" />

        <br />

        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">            

            <asp:View ID="View1" runat="server">

                <b>View 1:</b>

                <br />

                <asp:TextBox ID="TextBoxInView1" runat="server" ValidationGroup="View1ValidationGroup" />

                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                    Text="TextBox in View 1 can not be empty" ControlToValidate="TextBoxInView1" ValidationGroup="View1ValidationGroup" />

                <asp:Button ID="Button1" ValidationGroup="View1ValidationGroup" Text="Validate View 1 only." runat="server" />

            </asp:View>

            <asp:View ID="View2" runat="server">

                <b>View 2:</b>

                <br />

                <asp:TextBox ID="TextBoxInView2" runat="server" ValidationGroup="View2ValidationGroup" />

                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

                    Text="TextBox in View 2 can not be empty" ControlToValidate="TextBoxInView2" ValidationGroup="View2ValidationGroup" />

                <asp:Button ID="Button2" ValidationGroup="View2ValidationGroup" Text="Validate View 2 only." runat="server" />

            </asp:View>

        </asp:MultiView>
 

<%---- Code Behind ---- %>
 

protected void SwitchViewIndex_Click(object sender, EventArgs e)

        {

            if (this.MultiView1.ActiveViewIndex == 0)

            {

                this.MultiView1.ActiveViewIndex = 1;

            }

            else

            {

                this.MultiView1.ActiveViewIndex = 0;

            }

        }

Open in new window

0
 

Author Comment

by:cdemott33
ID: 24096745
Hi ppittle - Not quite.  I have my controls in View1 within a validation group.  (see code below).  My problem is the enter key on the keyboard.  

When the page loads, it loads View1 (the code below).  Now when I enter a bad value in my textbox on View1 and hit the enter key on the keyboard the ValidationSummary doesn't loaded the error message on the screen.  The ONLY thing it does it put astericks (*) next to the textbox on the screen.  The actually message of "Error: Please correct the following." never appears.  

The only way I can get that message to appear is by "Clicking" on the button.  The enter key does nothing.

Someone suggested that I read this article:

http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx

...which talks about setting the "default button".  However, it refers to the use of "Panels" and not "Views"?  It was my understanding that using a MultiView Control was an "Upgrade" from using a "Panel" control, but I can seam to figure out how to set a default button in a View?

Any thoughts?  Should I just forget about using MultiViews and use Panels instead?
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" 

                HeaderText="Error:  Please correct the following." 

                validationgroup="Group1" />

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                ErrorMessage="Please enter an ID" Text="*" ControlToValidate="customer_id" 

                Display="Dynamic" validationgroup="Group1"></asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 

                ControlToValidate="customer_id" Display="Dynamic" 

                ErrorMessage="Invalid Format - See Above" Text="*" 

                ValidationExpression="^[a-zA-Z]{5}[0-9]{2}$" validationgroup="Group1"></asp:RegularExpressionValidator>

            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 

                ControlToValidate="customer_id" Display="Dynamic" 

                ErrorMessage="ID needs to be 7 characters" Text="*" ValidationExpression="^\w{7}$" 

                validationgroup="Group1"></asp:RegularExpressionValidator>
 

        <asp:TextBox ID="customer_id" runat="server"></asp:TextBox>

  

        <asp:Button ID="check_id" runat="server" Text="Check ID" Height="26px" validationgroup="Group1" />

Open in new window

0
 
LVL 8

Accepted Solution

by:
ppittle earned 500 total points
ID: 24097799
cdemott33,

I was able to get the functionality you are looking for by setting the EnableClientScript to false for the Validator controls.  This prevents validation logic from being run client side via JavaScript and executes all validation logic server side.  Why the ValidationSummary control does not function as expected during client side validation, I am unsure. However, I would infer it's a limitation in the javascript that handles the validation behavior.

PJ
<asp:MultiView ID="MultiView2" runat="server" ActiveViewIndex="0">

                <asp:View ID="View1" runat="server" >

                    <b>View 1:</b>

                    <br />

                    <asp:TextBox ID="TextBoxInView1" runat="server" ValidationGroup="View1ValidationGroup" />

                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                        Text="*" ControlToValidate="TextBoxInView1" ValidationGroup="View1ValidationGroup"

                        ErrorMessage="TextBox in View 1 can not be empty"  EnableClientScript="False"/>

                    <asp:Button ID="Button1" ValidationGroup="View1ValidationGroup" Text="Validate View 1 only." 

                        runat="server" />

                </asp:View>

                <asp:View ID="View2" runat="server" >

                    <b>View 2:</b>

                    <br />

                    <asp:TextBox ID="TextBoxInView2" runat="server" ValidationGroup="View2ValidationGroup"  />

                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

                        Text="*" ControlToValidate="TextBoxInView2" EnableClientScript="False"

                        ValidationGroup="View2ValidationGroup" ErrorMessage="TextBox in View 2 can not be empty" 

                        />

                    <asp:Button ID="Button2" ValidationGroup="View2ValidationGroup" Text="Validate View 2 only." 

                        runat="server" />

                </asp:View>

            </asp:MultiView>        

        <asp:ValidationSummary ID="ValidationSummaryView1" runat="server"

            HeaderText="Please correct the following: " ValidationGroup="View1ValidationGroup" />

        <asp:ValidationSummary ID="ValidationSummaryView2" runat="server"

            HeaderText="Please correct the following: " ValidationGroup="View2ValidationGroup" />

Open in new window

0
 

Author Comment

by:cdemott33
ID: 24099842
By setting the EnableClientScript to false it solves the enter key problem, however, if what I enter into the textbox is valid then it doesn't hide View1 and show View2.  Strange.

I think what I'm going to do is go ahead and use Panels rather than Views.  I think this MultiView thing still has some bugs in it.  

You're solution was correct as far as solving the enter key issue though, so thank you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now