Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1523
  • Last Modified:

Form Validation, The Enter Key and MultiView

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
cdemott33
Asked:
cdemott33
  • 2
  • 2
1 Solution
 
ppittleCommented:
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
 
cdemott33Author Commented:
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
 
ppittleCommented:
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
 
cdemott33Author Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now