Handling the submit action of two TextBoxes - c#

I have an ASP.net page.
That has an Ajax Toolkit Tab Control.
That has tabs.
That have custom ascx controls I wrote.
I have a text box that perform a search action. It is declared like this:
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
Nothing fancy. This format has been working for months. There's no submit button. It just posts back when I hit enter. The problem appeared when I added a second custom control using the same type of feature. Now browsers don't postback when I type something in either of these textboxes and press enter.
It seems that browsers have a default way of handling one textbox in one form, but that behavior changes when the number reaches two.
Is there an easy way around this? I guess I can create a hidden submit button but it seems like there is probably a better way to deal with this when the functionality is in two separate custom controls.
Your feedback is appreciated!

Check this out: http://www.allasp.net/enterkey.aspx
The default behavior with no submit button seems to depend on the browser, and the behavior can indeed depend on the number of input controls.
I would add hidden "submit" button (e.g. style="display:none;") which should ensure that it always gets submitted.

The answer was a little different than I expected, but philosophically like my original idea that #jamietre reinforced.
I had to surround the controls with an <asp:Panel> tag with a DefaultButton attribute. A-like-a so:
<asp:Panel ID="ButtonPanel" runat="server" DefaultButton="btnSubmit">
<asp:Label ID="Label1" runat="server" Text="Course:"></asp:Label>
<asp:TextBox ID="txtPrereqSearch" runat="server"
ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="txtPrereq_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtPrereqSearch"
WatermarkCssClass="Watermark" WatermarkText="e.g., MATH201"></asp:TextBoxWatermarkExtender>
<asp:Button ID="btnSubmit" CssClass="InvisibleSubmit" runat="server" Text="Submit" OnClick="txtPrereqSearch_TextChanged"/>
</asp:Panel>

Related

Asp .Net Web Forms removing postback from asp:linkbutton with inner HTML tags

I have created my first fully functional listview with custom action buttons, that worked fine when I used asp:buttons, but I need the buttons to be icons, so I tried inserting HTML tags inside, since it can't be done out of the box, I used as:linkbuttons controls to achieve the desired look.
What it does is that the buttons trigger several actions that populate controls inside update panels, and I have my own Javascript that shows a popup on click, but on the second action it currently sends a postback and page is reloaded, I googled other questions and tried everything they had, what I tried:
Adding a return false on client click
<asp:LinkButton runat="server" OnClientClick="showDialog('#popup'); return false;" CommandName="Detalles" CssClass="toolbar-button" ID="EditButton">
Editar
<span class="mif-pencil"></span>
</asp:LinkButton>
Using an HTML button with runat server, but Command doesn't trigger
<button runat="server" onclick="showDialog('#popup');" CommandName="Detalles" class="toolbar-button" ID="EditButton">
Editar
<span class="mif-pencil"></span>
</button>
And on jQuery, but doesn't do any difference
$('.toolbar-button').removeAttr('href');
What exactly I'm missing, my code works fine with Asp:buttons, but they can't have additional HTML :(, I'm a newbie on ASP, any hints are appreciated.
Working button that doesn't allow inside HTML
<asp:Button runat="server" Text="Detalles" CssClass="toolbar-button fg-darkCyan button-popup" ID="btn_detalles" CommandName="Detalles">
</asp:Button>
NOTE Buttons are inside a ListView control, and assigned for each item.

How to handle two different forms in an asp.net web page?

I have a login page in my asp.net website (using C#) and it has a "login form" which has an email text box, a password text box and a login button.
In addition, I have a "search form" at the top of the web page which has a search text box and a search button.
*All of the controls are in the same form because of the asp.net limit for one runat="server" form.
The problem is that when I type something to search for and click ENTER (and not directly the button) it doesn't do anything, only runs the Page_Load again. Same thing when I click ENTER in the login section instead of directly on the login button.
I have tried different solutions but they were problematic because of the different functionality of the two "forms".
I have no idea how to solve this, any suggestions?
my web forms is a little rusty but I think something like this:
<asp:Panel ID="loginPanel" runat="server" DefaultButton="loginButton">
<%-- Login Stuff--%>
<asp:Button ID="loginButton" runat="server" />
</asp:Panel>
<asp:Panel ID="loginPanel" runat="server" DefaultButton="searchButton">
<%-- Search Stuff --%>
<asp:Button ID="searchButton" runat="server" />
</asp:Panel>
should work
Only one form with runat='server' available on aspx page. So, if you really need another form, you may use its without runat='server' and use for search jQuery ajax call.

Unblock controls if !Page.IsValid

I have ASP.NET WebForms application. One of it's pages is dynamically created table with RegularExpressionValidator. Above of table there are several LinkButtons, which manages navigation of application. But if I put invalid value to textbox in table, Page.IsValid is set to false and all controls on page are blocked.
So, how can I unblock buttons even if validator set Page.IsValid to false? Thnak you.
You could use ValidatorGroups to separate the validations.
Assuming you want to "unblock" the link buttons used for navigation, you can use:
CausesValidation="False"
in the ASPX markup for the link button.
Example:
<asp:LinkButton ID="btnBack" runat="server" data-transition="fade" CausesValidation="false"
data-theme="b" data-icon="" Text="Back" onclick="btnBack_Click" />

use Javascript or C# to validate a webform

Would like to validate more than one control on one button click. I would like something to validate whether a textbox has contents if a checkbox is checked or not but the checkbox doesn't necessarily have to be checked and in that case I don't want to check the textbox. I tried validation group but each button needs to control the different groups and i need this all to be under one button.
I'm open to ideas of how to do this c#,javascript...etc. Heres some code: Button3 is the save which validates whether checkbox 1 is checked and if so textbox10 cant be empty. I have about four other instances of this but are independent of each other.
<asp:Button ID="Button3" runat="server" Height="24px"
Text="Save" Visible="False" Width="67px" Font-Bold="True"
causesvalidation="true"
validationgroup="required"
runat="Server" />
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text=" Breach Letter Sent"
ValidationGroup="required" AutoPostBack="True" Enabled="False" />
You want to use the CustomValidator control which can validate both on the server and the client. There is an example in the docs here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
I would never do form validation in JavaScript. Believe it or not, but some people actually turn off JavaScript! Use validators to validate the field content. Of course this means a round trip to the server in most cases, but you get reliable and well integrated validation.
you can use validation with Ajax (in Ajax postback occures but you will not sense)

Changing a control's style based on validation (ASP.NET)

I've got a very simple form with the following troubled snippet:
<asp:Panel class="normal" ID="Panel1" runat="server">
<strong><asp:Label ID="Panel1Error" class="error" Visible="false" runat="server"/></strong>
<label for="TextBox1"><em>*</em> Don't leave this blank</label>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="TextBox1RFV" runat="server"
ControlToValidate="TextBox1" ErrorMessage="This field cannot be blank."
Display="None" />
<--- other validators --->
</asp:Panel>
There are two things I want to do when the page fails validation:
Change the style of Panel1 (to one which shows different colors to indicate an error). I was able to do this by calling Page.Validate in Page_Load, then iterating over Page.Validators, getting each validator's parent control, casting it to a Panel, then setting .CssClass Doesn't seem like a superb solution, but it got the job done - is there a better way?
I want to take whatever validation error(s) are thrown and put them in the Panel1Error label, as well as set it to visible. This is where I am a bit baffled. I thought at first I could possibly specify the Label in which a validator writes its ErrorMessage, but I had no such luck. If I just toss the validator inside the Label, its formatting messes up the entire layout of the page, regardless of whether I directly assign it the 'error' CSS class or just leave it in the Label.
Just to clarify, in production, I would be doing this process for multiple Panels on a page, each with one form element, preventing me from calling the Panels explicitly and just saying Panel1.CssClass, etc.
I would recommend a javascript solution. ASP.NET injects a global js variable called Page_Validators, which is an array of all of the validator spans on the page. I wrote about this on my blog. It's a different solution, but it should give you enough insight to get started.
Use ValidationSummary controls with a ValidationGroup for each panel.
Seems fine if it worked.
Use a ValidationSummary control. Or you can inherit from the controls and override the render event.

Categories