Form submit event problem - asp.net

<form id="form1" runat="server" onsubmit="return CheckForm(this)">
<script type="text/javascript" language="javascript">
function CheckForm(frm)
{
if(CheckEntireForm(frm) == false)
return false;
}
</script>
Hello EveryBody Please help me to get solution
I've used a javascript function on form's onSubmit event like
which validates my page's textboxes etc.
NOTE: i am not using asp.net's validation. i have got my own validation classes
I've also used a dropdownlist with auto post back set to true
in asp.net 2.0 when dropdownlist's selected index is changed it
calls form's onsubmit while this did not use to happen in asp.net 1.1
I've used this on 140 pages in my website the worst case will be to call
CheckForm(this.form) on my save buttons client click on all the pages.
I am looking for a backword compatibility solution which can be applied at a single place
like web.config or some class etc.
i am using asp.net and javascript

You have forget to return the true - your form never submit.

Related

input vs .net texbox: input clears on enter pressed, asp:textbox does not

I use the jquery datepicker. I have the following input in the updatePanel:
<input id="DateMask" type="text" />
and js code:
$(".datepicker").datepicker({
changeMonth: true,
changeYear: true
});
$(document).ready(function () {
SetD();
});
Using jQuery:
<script src="/scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="/scripts/jquery-ui.js" type="text/javascript"></script>
When I press enter on my page it clears my input, and I do not post anything to the server. I've managed to solve the problem by writing <asp:textbox> instead of <input>, but why is it so?
and found the topic here
But once again why does it work this way?
This is related to the difference between 'standard' HTML controls, and .NET server controls.
When you use a standard HTML <input> in .net, you are in charge of everything relating to it - quite simply, population of the initial value and later retrieval through Request.Form.
The advantage that the server controls - including, as you've noticed, <asp:textbox> - bring is that the ASP.Net framework now handles most of the messy parts for you. Thus, any text that you enter is available in the code-behind as a property of the object, and when control returns back to the page the textbox is re-populated with the same value it had beforehand.
I'd suggest having a read of a few background topics that discuss this, primarily the ASP.Net server controls overview at http://support.microsoft.com/kb/306459.

Validate AJAX Toolkit Combobox Regular Expression Validator

I was wondering if it was possible to validate the ajax toolkit combobox with a regular expression validator. I allow the user to enter values, but only want certain values ( regex [0-9]{0,1}[0-9]{1}|-7|-8|-9) to be allowed. I could use the custom validator, but I would need to also create javascript function to validate on the client side. If there is a better way I would love to hear it. Thanks. Here is the combobox code:
<asp:ComboBox CssClass="required" DropDownStyle="Simple"
ID="DaysDeployed" Width="50" runat="server">
<asp:ListItem Selected="True" Text="" Value="" />
<asp:ListItem Text="Refused" Value="-7" />
<asp:ListItem Text="Don't Know" Value="-8" />
<asp:ListItem Text="Missing Data" Value="-9" />
</asp:ComboBox>
Summary: Instead of using an asp.net button that would normally trigger your postback, make one using html. Have the html button run a javascript function that first checks the regex validation, then (if valid) runs the postback function.
First, I would remove the asp.net button that you use to trigger the server-side code, and replace it with a client-side button. You can follow the steps in another answer of mine if you need help creating this button. Here is the link:
https://stackoverflow.com/questions/14062993/input-type-image-onclick-will-trigger-its-event-but-not-act-well-on-funct/14063911#14063911-Stack Overflow
Second, the javascript function should first validate the data using a regex function. Use something like this:
function validateCombobox(myComboboxValue) {
if(myComboboxValue.match(regularExpressionString)===null){
return false
} else {
return true
};
};
***Note: Regex is a weak area for me so you may need to revise this script a little.
Third, if the input is validated using the script above, then call the postback using javascript. To do this, follow these steps:
Create an on the asp page. This is necessary
because without it, the site will not generate event handlers for
the needed buttonclick event.
Set the link-button's css display property to 'none'. Beware that
link-button's "Visible" attribute my be set to true (this is because
asp.net does not even render the code for controls with a false
visible attribute). To illustrate, if your link-button's cssClass
name is myButton, add this to your css file:
.myButton
{
display: none;
}
Now that the button is created and properly hidden, you can add the
postback function to your javascript function. The postback function
has two parameters, the first is the client side ID of the
link-button control that we created. Beware that the client-side
IDs of asp.net controls are not the same as the ID you assign it
during development. Because of this, we use <%=Control.ClientId %>
to get the control's client ID. If your link-button ID is
"myLinkButton", the following should be your postback function:
__doPostBack('<%=myLinkButton.clientid %>','')
Please note that there are two underscore characters in the
beginning of this function.
Here is an example of the regex validation function and the javascript function that should be called by your new button:
function validateCombobox(myComboboxValue) {
if(myComboboxValue.match(regularExpressionString)===null){
return false
} else {
return true
};
};
function comboBoxButton_click(){
var myComboboxValue = $('#<%=myComboBox.clientid %>').val();
if(validateCombobox(myComboboxValue)==true){
__doPostBack('<%=myLinkButton.clientid %>','');
};
};
I have a lot of distractions at the moment and am a little scatter-brained, so forgive me if these instructions are a little confusing. If you need more assistance, feel free to comment and I'll check back soon.

asp:linkbutton (navigating to specific page section on post back)

I have some linkbuttons to update my gridview which is in the middle of the page. Everytime I hit edit or delete etc the window scrolls to the top on the page that gets posted back. I want it to stay focused on the grideview. I have tried a javascript function but for some reason it did not work.
(edit: the following works as far as scrolling is concerned but prevents postback)
here is what I tried
<script type="text/javascript" language="javascript">
function goto() {
window.scrollTo(10, 1100);
}
</script>
<asp:LinkButton ID="lbtnGo" runat="server" OnClientClick="javascript:goto();return false;">GO</asp:LinkButton>
source
How can I do this?
Did you try with <%# Page MaintainScrollPositionOnPostback="true" %> in the page declaration?
Regards
Client-side event fires before server-side. So even if you scroll window to correct position - after postback you will be returned to the top. You can add the following code to your server-side LinkButton click event handler:
if (!this.IsStartupScriptRegistered("ScrollToGrid"))
{
String scriptString = "<script language=\"JavaScript\">";
scriptString += "window.scrollTo(10, 1100);";
scriptString += "</script>";
this.RegisterStartupScript("ScrollToGrid", scriptString);
}
this will add javascript block to your page after postback
There are, depending on the .NET framework properties available that can help one out:
ASP.NET 1.x: use SmartNavigation. ASP.NET 2.0: use MaintainScrollPositionOnPostBack. Use an UpdatePanel control to asynchronously update parts of a page
and the best way for this is UpdatePanel control to asynchronously update parts of a page

How to prevent PostBack on the client side?

I have some validation JS code on client, that must be executed befor PostBack.
If this validation code return 'false', postback is needless.
How it can be disabled?
Remember that the real validation should always happen on the server. Anything you do client-side is merely an optimization to save a few http round trips.
The easiest way to keep your client side and server-side validation in sync with ASP.Net is to use the validation controls. The validation controls will do both client side and server side validation, in such a way that if validation fails on the client it never posts to the server.
If you want to do something that's not covered by the standard validation controls, you should either use a CustomValidator or inherit your own control from BaseValidator.
Set the OnClientClick='YourJSValidationFunction' on your ASP button.
Then have the YourJSValidationFunction return true or false.
False will prevent postback
Example:
http://vijaymodi.wordpress.com/2007/06/08/button-onclick-and-onclientclick-2/
If the postback is being triggered by a button then you can do something like this:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return IsValid();" />
If the IsValid function returns false then the postback will be prevented.
If you want to catch all postbacks regardless of which control triggers it then you can use <form id="form1" runat="server" onsubmit="return IsValid();">
What do you use: some validator or some button with onclick event?
If you have
<input type="button" id="btnID" runat="server" onclick="CheckValid();"/>
function CheckValid()
{
if(!IsValid) return false;//then no post back occer
}
Depending on the validation you're attempting, you may also be able to use the CustomValidator control. This would also allow you to easily implement your validation logic on the server side.

ASP.NET MVC 2 client side validation not working for Html.ValidationMessage()?

I'm trying to get a very simple client side validation example to work in ASP.NET MVC 2. I'm using data annotations to set a required property "Surname". When I use the Html.ValidationMessageFor(x => x.Surname) the correct client side validation script is written to the page. But when I use Html.ValidationMessage("Surname") the client side validation is not rendered out until after the page has been posted. Client side validation only starts working after a form post! I can see that the script is updated in the page after a form post. There appears to be a bug in Html.ValidationMessage()?
Make sure you are referencing the correct scripts in your master page head.
<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcValidation.js") %>" type="text/javascript"></script>
also look at your view to make sure the client validation call is above your form
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
...
<% } %>
and of course your validation message to show the problem.
<span class="editor-label"><%= Html.ValidationMessageFor(u => u.Name)%></span>
That is really all you need. I believe the required attribute will only start its check if you enter text in the field then remove the text and tab out of the field, so try that to see if you get the validation. Others will validate when the attribute needs to. For example the [StringLength(50)] will show an error message when you exceed 50 characters.
i've not tried but the metadata are stored on the proprerty so only the ValidationMessageFor has the ability to check the prop (via static reflection).
The other helper use a string key to access a modelstate dictionary without any reference to the property (and no validation metadata info), so i dot't think the Html.ValidationMessage(string key) has the ability to inject validation script client-side.
As far as I know the validation requires an attempt to validate even if it's client side.
You could try on the GET view creating a new instance of your model, then use TryValidateModel() before sending it to the view. This should cause the validation logic to run and thus to populate the clientside validation, this will result in all required fields showing their error version but depending how you choose to style them this doesn't have to be a big issue.

Resources