validation problem in asp.net - asp.net

I am using Page.Isvalid function in my web page.I have two buttons in my web page namely "save" and "generate".when i click the save button the validation summary will be invoked in which all the validations in this page will be shown.
Now I dont want to show a particular validation message for the "Save" button, but the same validation message should be shown specifically to "generate" button in the same page.
But i am using Page.Isvalid in the "save" button click which is displaying all the validation messages in the page.
Any help would be deeply appreciated.Thanks

You can use ValidationGroup(s) to do this.
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="*"
ValidationGroup="GenerateOnly"
ControlToValidate="TextBox1" />
<asp:Button ID="Generate" runat="server" Text="Generate"
ValidationGroup="GenerateOnly" />
<asp:Button ID="Save" runat="server" Text="Save" />
</div>
</form>
This example triggers the validator if Generate is clicked, but not when Save is clicked, and also works when calling Page.IsValid in the buttons onclick function.

not entirely sure what you mean but here goes.
If you want both the save and generate buttons to generate validation messages then why not move the Page.Isvalid along with the validation code into another method. Call this method from both the save and generate methods.
If I've got the wrong end of the stick please let me know a bit more.
Cheers Tigger

Ya buddy if you have to call the same function for two different buttons it is not possible without the concept of overloading. Otherwise you have to create two methods.
But I'am not sure as i am a begginer

Related

Hide validations on form post back in aspx page

I have put server side validations for each text box using <asp:RequiredFieldValidator/>. I have called ClearFields() method on page load that will clear all fields on the form when the button is clicked. The problem is that when the form gets posted and the fields are cleared, the validation message appears again. How to hide the validation messages on form post back. I am sorry, but its been years I have not coded in aspx and I can't find any solution online.
This is the textbox code:
<asp:TextBox runat="server" CssClass="form-control" placeholder="Your Name *" ID="name"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="name" ErrorMessage="Name seems empty" CssClass="help-block text-danger"></asp:RequiredFieldValidator>
This is the button code:
<asp:Button runat="server" class="btn btn-xl" Text="Send Message" ID="submit" OnClick="submit_Click" CausesValidation="false"/>
I guess you are getting the validation message due to button used for clearing the fields. Set CausesValidation property of button to false:-
<asp:Button ID="ClearButton" runat="server" CausesValidation="false" Text="Clear" />
Also, please note asp:RequiredFieldValidator works on client side i.e on the browser it is not a server side validation.
Update:
Since you are clearing your fields on click of button, you can clear in submit_Click method itself after sending the email instead of page load. Ideally you should have a separate button to clear the form though.

ASP.NET Button submits page and I can't stop it

I have a simple proof of concept web page. It uses one master page and one content page, the default one, default.aspx. I'm doing some client side debugging with alert boxes. I dragged an asp.net button onto the page and set CausesValidation = false and UseSubmitBehavior = false and yet when I click it the page submits.
What am I doing wrong?
Here is a design time code....
<asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="False" CausesValidation="False" />
Here is Runtime render, wth is it putting in a PostBack?
<input type="button" name="ctl00$ContentPlaceHolder1$Button1" value="Button" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Button1','')" id="ContentPlaceHolder1_Button1" />
--Update--
Thanks Volkan Paksoy, that worked. For those who suggested HTML buttons, that worked too and that is what I used, but I was just curious why the ASP.NET button wouldn't work. It's something I should know and probably something I knew and forgot. Thanks for the help
If you need to use asp:Button and disable submit you can add OnclientClick function such as:
<asp:Button ID="Button1" runat="server"
CausesValidation="False"
OnClick="Button1_Click"
Text="Button"
UseSubmitBehavior="False"
OnClientClick="return false;" />
This should stop the postback. But you can simply use an input too of course. This is just one way of doing it.
If you want to create a button that does not submit the page/postback to the server, you could create one using raw HTML like so:
<input type="button" />

No KeyDown event on my VB.NET Webform

I'm new to VB.NET webform development, but an old VB/Access developer.
I've used Keydown, Keypress events before in my normal development but cannot find this event with this new web development project I'm starting.
I'm assuming it's something to do with the fact it's a web-form. However when I search I can't find others with this issue so thought I'd ask it here. Below is a screen-shot of the events I have on the text control on the web-form where I'm trying to put the keypress event.
(I wanted to attach my picture showing you the events in the list but I don't have 10 reputation points so won't let me include it).
Is this event not available for web-form development? Essentially what I want to do is have the page check that there is text is both the txtUsername and txtPassword controls before enabling the "Log In" command button.
All I have in the drop-down list for the control is:
(Declarations)
DataBinding
Disposed
Init
Load
PreRender
TextChanged
Unload
Consider using a RequiredFieldValidator:
<asp:TextBox id="Foo" runat="server"/>
<asp:RequiredFieldValidator id="Bar"
ControlToValidate="Foo"
Display="Static"
ErrorMessage="*"
runat="server"/>
And in your submit button's click handler:
If Page.IsValid Then
...
Else
...
End If
You will probably also want to use the HTML5 required attribute:
<asp:TextBox id="Foo" runat="server" required="required" />
You might also consider using aria-required:
<asp:TextBox id="Foo" runat="server" required="required" aria-required="true" />

RequiredFieldValidator have to click twice

I have run into the same problem as described here.
Only the question is marked as answered with only an explanation as to why you may have to click twice when using a RequiredFieldValidator on input fields - once as the blur of a textbox(for example) will correct the validation and then again to actually post the form.
I don't want to have to click a button twice! Does anyone know a solution or workaround to this?
You could add EnableClientScript=false to the validator.
That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.
Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.
Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.
The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.
I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.
If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.
Here's a way to reserve about one, vertical line of space for a dynamic validation message:
<div style="height:1.5em;overflow:visible;">
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="Name is required" ControlToValidate="TextBoxName"
Display="Dynamic"></asp:RequiredFieldValidator>
</div>
I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.
Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.
I would suggest adding
causesValidation="true"
to your button to see if that works.
I have a better idea.
Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.
Try this:
<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
<asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>
<asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />
Here is code that is working fine for me and helping to get rid of double click.
<asp:TextBox ID="TextBox1" runat="server" autocomplete="off"
Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
CausesValidation="true" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});
Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:
the first click will make Page_BlockSubmit become false.
the second click will check the value of Page_BlockSubmit and return true.
if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.
And, you can use google chrome to trace the value of Page_BlockSubmit.

IE 8 - ASP.NET form not submitting when user presses enter key

I have a simple form written in asp.net/C# and when trying to hit enter while in the form's input box doesn't submit the form for some reason. I had implemented a fix for a previous bug where pressing enter would merely refresh the page without submitting the form data but now pressing enter just does nothing, the fix is below:
<div style="display: none">
<input type="text" name="hiddenText" />
</div>
anybody know about a fix for this or a workaround?
I'm assuming you have a button somewhere on your page, as well as an event handler for it.
Have you tried wrapping your form (with the button) inside a Panel control and setting the default button attribute?
i.e.
<asp:Panel id="pnlMyForm" runat="server" DefaultButton="btnMyButton">
<asp:textbox id="txtInput" runat="server" />
<asp:Button id="btnMyButton" text="Submit" runat="server" />
</asp:Panel>
You can specify a default button for a form, which means hitting enter on any input control will fire that button (i.e. target the submit button). I haven't heard of this not working in any specific browser. This should eliminate your need for a workaround/hack.
<form id="form1" runat="server">
<asp:Panel ID="pnlFormContents" runat="server" DefaultButton="btnSubmit">
<!-- add some input controls as needed -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</asp:Panel>
</form>
Hope this helps...
I don't remember the specifics of the rules, but most browsers have the capability of submitting forms when ENTER is pressed if conditions are met. I think it had to do with whether you had 1 or more-than-one field, or whether or not there was at least one submit button (even if you hide it). I've done it in a site I recently did, but I don't have the code handy, but I can tell you it works without any special scripting. Check this posting for more details:
http://manfred.dschini.org/2007/09/20/submit-form-on-enter-key/

Resources