I'm doing validation of a textbox with RequiredFieldValidation.
I want it to validate the textbox only when a button is clicked, not when textbox loses focus.
Now, every time when I click anywhere else on a page it shows an error message.
Here is a code:
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtEmail" Text='<%#Eval("CustEmail") %>' />
<asp:RequiredFieldValidator ControlToValidate="txtEmail"
ValidationGroup="grpEmail"
ErrorMessage="Must enter Email Address" runat="server"></asp:RequiredFieldValidator>
<asp:Button Text="Update Email" ButtonType="Button" CommandName="UpdateEmail"
ValidationGroup="grpEmail"
CausesValidation="true"
Visible="true" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
I just need it to be validated when button is clicked. That's it.
What am I doing wrong?
Setting the attribute CausesValidation of the Textbox to false should do the trick.
Related
I have a aspx form in which there are 5 controls below is the design code:
Question
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
Answer
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
<asp:CheckBox ID="CheckBox1" runat="server" Text="If Other" AutoPostBack="True"
OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
A user selects a question from the drop down list and chooses the answer from the answer list.
If he/she doesn't find the answer in drop down list than he/she selects the checkbox and type the answer in textbox and press the submit button afterwards.
Is it possible to show the textbox and at the same time hide the drop down list using a single line code in checkbox OnCheckedChanged event?
Yes (though I'd use some more lines for better readability), you should set the visibility of the TextBox to false, to hide it at first:
<asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox>
In CheckBox1_CheckedChanged, you add the following lines:
DropDownList2.Visible = !CheckBox1.Checked;
TextBox1.Visible = CheckBox1.Checked;
I have a textbox and a button in my code.The button when clicked should validate the value entered in the textbox.The button most of the times does it but at times doesn't do anything at all.Any help would be appreciated.
<asp:Button ValidationGroup="vgpPno" runat="server" ID="btnSubmitPartNumber" OnClick="btnSubmitPartNumber_Click" SkinID="MSWButton" Text="OK" OnClientClick="if(Page_ClientValidate(){startPLI();}meta:resourcekey="btnSubmitPartNumberResource1" />
<asp:TextBox runat="server" ValidationGroup="vgpPno" SkinID="MSWTextBox"
ID="tbPARTNUMBER" MaxLength="11" meta:resourcekey="tbPARTNUMBERResource1" Width="230" Style="display: inline" CssClass="form-control input-sm"></asp:TextBox>
<asp:Button ValidationGroup="vgpPno" runat="server" ID="btnSubmitPartNumber" OnClick="btnSubmitPartNumber_Click" SkinID="MSWButton" Text="OK"
OnClientClick="if(Page_ClientValidate(){startPLI();}meta:resourcekey='btnSubmitPartNumberResource1'" />
Check Your web Form. If you are using form tag more then one time asp.controls not working properly
I would like to know if it is possible to do requiredfieldvalidator purely in javascript.
I do not want my button click which processes the page to perform a postback, as postback completely resets my styling and there is too much to reload on ispostback.
I will however do the postback to the server to update SQL once all the fields have been completed.
<asp:UpdatePanel id="PurGradeUpdate" runat="server" RenderMode="Inline">
<ContentTemplate>
<telerik:RadComboBox ID="PURGrade" runat="server" Width="100" EmptyMessage=" ---" Font-Bold="true"></telerik:RadComboBox>
<asp:RequiredFieldValidator ID="PurGradeValidate" runat="server" Display="Dynamic" ControlToValidate="PURGrade" ErrorMessage="!!!" Font-Italic="true" Font-Bold="true" ForeColor="Red" InitialValue=" ---" EnableClientScript="true"></asp:RequiredFieldValidator>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="PURProduct" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<telerik:RadButton ID="PurCreate" runat="server" AutoPostBack="false" OnClientClicked="PerformValidation"></telerik:RadButton>
I have the above in an update panel as the comboboxes are populated via SQL read, based on other comboboxes selected index changes.
I would now like to call that "PerformValidation" on my process button click and here the validation must happen, and if not valid, display the errormessage.
Thanks for the assistance.
Ok man i checked it on my VS and this works for sure:
<asp:UpdatePanel id="PurGradeUpdate" runat="server" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="PURGrade" runat="server" Width="100" Font-Bold="true"></asp:DropDownList>
<asp:RequiredFieldValidator ID="PurGradeValidate" runat="server" Display="Dynamic" ControlToValidate="PURGrade" ErrorMessage="!!!" Font-Italic="true" Font-Bold="true" ForeColor="Red" InitialValue="---" EnableClientScript="true"></asp:RequiredFieldValidator>
<asp:DropDownList ID="PURProduct" runat="server" Width="100" Font-Bold="true"></asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="PURProduct" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<Asp:Button ID="PurCreate" runat="server" AutoPostBack="true"></Asp:Button>
In aspx.cs file I added default value:
PURGrade.Items.Insert(0, "---");
So as i told you before you don't need to use custom JavaScript commands to validate on client side cuz asp required field validator runs on client side by default. The only think that can cause problems in your code is custom controls you use. Be sure that your combo box empty value is what is really added to the select list as a default value.
i use this code in aspx page:
<asp:TextBox ID="Search" runat="server" ToolTip="Search..." Text="Search..." onblur="if (this.value=='') this.value='Search...';" onfocus="if (this.value=='Search...') this.value='';"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvSearch" runat="server" ControlToValidate="Search" ErrorMessage="please insert text" ValidationGroup="Search" SetFocusOnError="True" Display="None"></asp:RequiredFieldValidator>
but i use Text="Search" in textbox for use Watermark property in textbox. please help me for use property and RequiredFieldValidator.
You could set InitialValue = "Search..."
<asp:RequiredFieldValidator ID="rfvSearch" runat="server"
ControlToValidate="Search" ErrorMessage="please insert text"
ValidationGroup="Search" SetFocusOnError="True" Display="None"
InitialValue="Search...">
</asp:RequiredFieldValidator>
Doing this, error message will be shown unless Search contains something different to Search...
Scenario is i have a date time column in grid view, upon selection on date time column i need to show calculated value in next column's text box in gridview
i am not able to calculate value on basis of date selection, although i am able to select the date through jquery and place it in textbox. but it is not changing the next textbox value upon basis of selection.
code is as under
<asp:TemplateField HeaderText="Issue Date">
<ItemStyle Width="140px" />
<ItemTemplate>
<asp:TextBox ID="txtIssueDate" runat="server" Width="110px" Text='<%#Eval("RequirementIssueDate") %>'
ReadOnly="true" class="Calender" OnTextChanged="txtIssueDate_TextChanged"></asp:TextBox>
<img id="imgIssueDate" src="../KelshawImages/calender.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiration Date">
<ItemStyle Width="140px" />
<ItemTemplate>
<asp:TextBox ID="txtExpiration" runat="server" Text='<%#Eval("RequirementExpirationDate") %>'
ReadOnly="true" class="Calender" Width="110px"></asp:TextBox>
<img src="../KelshawImages/calender.png" />
</ItemTemplate>
</asp:TemplateField>
Not 100% sure but I think that if you put the ReadOnly attribute on the control you will not be able to modify it in the textchanged event. Try removing the ReadOnly=True attribute and simply add the HTML disabled attribute to the control and give it a try.