Alejandro
Alejandro

Reputation: 125

How to deal with mutiple forms in ASP .NET

Short description of the page: user searches for an account (first form is to search for an account), account information gets displayed below and you can make changes to that account (second form).

I currently have this feature in only one form, but I am running into problems when I try to validate (since pressing any of the two buttons will try to validate all fields although that is not right, since if you are searching you don't need to fill out the other fields to edit). Also, when I press enter in any of the text boxes the search button is clicked, how can I associate a text box to a particular button.

Upvotes: 0

Views: 152

Answers (2)

Mark Dickinson
Mark Dickinson

Reputation: 6633

What about using the CausesValidation attribute on your search button.

or you could use a custom validator like this

<asp:TextBox ID="TestBox" runat="server"></asp:TextBox>
<asp:CustomValidator ErrorMessage="Not" ID="CustomValidator1" Enabled="true"    
   ControlToValidate="TestBox"
   runat="server"></asp:CustomValidator>
<asp:Button ID="myButton" CausesValidation="true" OnClick="TestClick" runat="server" />

Then have your button have an onclick command like this

public void TestClick(object sender, EventArgs e)
{
    if(this.TestBox.Text == "me") this.CustomValidator1.IsValid = false;
    return;
}

Upvotes: 2

Dan Diplo
Dan Diplo

Reputation: 25339

To only validate certain form fields you can add a common ValidationGroup to all validators you require validating within that group and also the button that submits them. Alternatively, set the CausesValidation property of any button(s) you don't want to trigger validation when pressed.

You can also specify the default button within asp:panel. This controls what button is fired when you hit enter within the panel. So, wrap a panel around your form and specify the button in the panel like so:

<asp:Panel ID="PanelForm" runat="Server" DefaultButton="ButtonSubmit">

    <!-- Form fields go here... -->

    <asp:Button ID="ButtonSubmit" runat="Server" Text="Submit" />

</asp:Panel>

Upvotes: 4

Related Questions