Solved

Form Validation, The Enter Key and MultiView

Posted on 2009-04-07
4
1,472 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error on the asp.net page 1 33
Not showing page correctly 3 31
Syntax Error 2 47
Help with Syntax 9 28
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

13 Experts available now in Live!

Get 1:1 Help Now