html5 required is not allowing button click action to fireup - asp.net

I have an input page where i want to create some required fields and some aren't.
That means that at the first part of the form, you can select the date by pressing a button to show a calender to select the desired date, adding users (by pressing a button) and then you add the required text and finally a submit button.
Right now, when I put the required attribute to the input tag, the first two buttons don't work. They don't fire up and it writes that first I need to insert the required items.
How can I remove the connection between those specific buttons and the required inputs?
Thanks, Arkadi.
Code:
<h3> Create New Project</h3>
Users Added: <asp:TextBox ID="TextBox1" runat="server"
ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblAdded" runat="server" Text="Label" >User Was Already Added</asp:Label>
<asp:Label ID="lblNotFound" runat="server" Text="Label" >User Was Not Found</asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"></asp:CheckBoxList>
Add User<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /><br />
Project Name: <input ID="txtName" runat="server" required="required" />
Hand Over Date: <asp:TextBox ID="txtHandOverDate" runat="server" ReadOnly="true"></asp:TextBox>
<asp:Button ID="btnSelectDate" runat="server" Text="SelectDate"
OnClick="btnSelectDate_Click" />
<asp:Calendar ID="Calendar1" runat="server" Visible="False"
OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
Hand Over Form: <input ID="txtHandOverForm" runat="server" required="required"/>
Subject: <input ID="txtSubjectField" runat="server" required="required" />
Project Porpuse <input ID="txtPorpuse" runat="server" required="required" />
Perimiters<input ID="txtPerimiters" runat="server" required="required" />
Comments <textarea id="txtComments" runat="server"></textarea>
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />

I found what I wanted, so to all those who will see this:
I just needed to change the type of the button from submit to command, that means adding the attribute UseSubmitBehavior to false.
Thanks for the Answers, this site is great!

The OnClick events run at the server, but the required attrib is preventing the code from being submitted. If you want to run client side (ie javascript) code, then you will need to use the OnClientClick event instead.

Related

How to make validator's message appear nearby another control?

I have a validator with a bound text field (it's set via ControlToValidate). How can I make the validator's error message appear nearby another control (a label above this text field)?
Just put the validator control nearby the control where you want to show the message i.e. wherever you want to show the message just put the validator control there.
In the following example I am showing validation message near some other control not near the text box.
<form id="form1" runat="server">
<asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
<asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
<br />
<asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
<asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
<asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="0">Male</asp:ListItem>
<asp:ListItem Value="1">Female</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
Display="Dynamic" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
ValueToCompare="-1"></asp:CompareValidator>
<br />
<asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
Display="Dynamic" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
<br />
</form>
Hope this helps you.

DropDownList Default Value DISABLED

I am using 2 DropDownList in one page. Based on the Selected Value of the 1st DDL the 2nd DDL will get populated with values from the database. Now I want the default value of the 1st DDL to be something like "Please Select a Category". But i cannot set any Value for it, it will remain disabled, the user can only see it, not choose it, as that value is not valid data. I have written the following code
<div>
Categories :
<asp:DropDownList ID="drdcatID" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="FillBrandID"
AppendDataBoundItems="true"></asp:DropDownList><br />
<br />
Brands :
<asp:DropDownList ID="drdbrandID" runat="server"></asp:DropDownList>
<br />
Name:
<asp:TextBox ID="txtItemName" runat="server"></asp:TextBox>
<br />
Item Desc:
<asp:TextBox ID="txtItemDesc" TextMode="MultiLine"
Columns="50" Rows="5" runat="server"></asp:TextBox>
<br />
Price:
<asp:TextBox ID="txtItemPrice" runat="server"></asp:TextBox>
<br />
Availability :
<asp:TextBox ID="txtItemAvl" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="additem"
Text="Insert" />
</div>
Any help would be appreciated..

field validation on publishing the website

On my development PC, I created simple form with 3 text boxs and a submit button. I added validation to the text boxes and time to last text box.
<asp:TextBox
ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Required!" ControlToValidate="TextBox1"
ForeColor="#FF9999"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Required!" ControlToValidate="TextBox2"
ForeColor="#FF9999"></asp:RequiredFieldValidator>
<br />
<asp:TextBox
ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Required!" ControlToValidate="TextBox3"
ForeColor="#FF9999"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
(and added now() to textbox3)
On submitting the form, when running on local host (my development PC), the validation executes fine with no post back (means I don’t see changes on time on text box3). However, when I upload all the files to the server, upon submitting I see validation but it happens after post back which I see a new time on textbox3. Also I get this JavaScript error:
<input type="submit" name="Button1" value="Button"
onclick="javascript:WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(
"Button1",
"",
true,
"", "",
false, false))" id="Button1" />
What am I doing wrong? Should the solution be under release or debug?

Required field in another Panel is causing problems on ASP.NET Panel

i have the following code
<asp:Panel ID="searchPanel" DefaultButton="searchButton" runat="server">
<asp:TextBox ID="searchBox" runat="server" value=" Search..."></asp:TextBox>
<asp:Button ID="searchButton" runat="server" Style="display: none" OnClick="searchInput" />
</asp:Panel>
<asp:Panel ID="loginPanel" DefaultButton="submitButton" runat="server">
<asp:TextBox ID="username" runat="server" required="required"></asp:TextBox>
<asp:TextBox runat="server" ID="password" required="required"></asp:TextBox>
<asp:Button ID="submitButton" runat="server" Text="Log in" OnClick="login" />
</asp:Panel>
whenever i press 'enter' key when searchBox is focused, in the other panel fields are highlighted and says "Please fill the field".
How can I solve this?
I believe you are using HTML 5 "required" attribute. This will cause validation when you try to do a postback.
You can use a form specific for search control and use "novalidate" attribute in the form element.
Or
You can use validation groups to achieve this.
solved the issue by using validation group
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="username"
ValidationGroup="emailValidationGroup"
runat="Server">
</asp:RequiredFieldValidator>
ValidationGroup="emailValidationGroup" //goes to button
also don't forget to add
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
to webconfig

Disable an asp:RequiredFieldValidator if a checkbox is checked

I have 2 textboxes with required field validators, a button and a checkbox. When the check box is checked I want to disable the required field validator on the second textbox and allow the button to be clicked when the first textbox is completed.
This is generally what my aspx page looks like, although this is just a made up example:
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="Name" Display="Dynamic" ErrorMessage="Name Required" runat="server" />
Name: <asp:Textbox ID="Name" runat="server"/>
<asp:RequiredFieldValidator ID="rfvID" ControlToValidate="Name" Display="Dynamic" ErrorMessage="ID Required" runat="server" />
ID: <asp:Textbox ID="ID" runat="server"/>
<asp:Button ID="btn" Text="Query" runat="server"/>
<asp:CheckBox ID="chxNoID" Text="I don't have an ID yet" runat="server" />
use javascript. Get the checkbox change event & put the code below in function
var ctrl1 = document.getElementById('<%=rfvID.ClientID%>');
ValidatorEnable(ctrl1, false);

Resources