Forms Data Controls :: CustomValidator - ServerValidate Fires Twice?
Nov 24, 2010
I have a CustomValidator and has ServerValidate handler that does custom user input validation against values in database. The key point is the validator is inside of DetailsView. Using Asp.Net 2.0
Issue:
The ServerValidate handler is firing twice when I click a button inside the DetialsView. I am injecting a javascript to show my custom error message in a dialog. So due the event firing twice the dialog appears twice.
Reason for this behavior:
I am sure there isn't anything in my code doing this. It is how the DetailsView and Button control works. Similar issue is listed
here and "DenGn" has given some explanation.
I don't know if anything really exists that can stop this behavior. What I am going to do is use a counter and store it in session, then do the check to see if it is the second time fire, not to inject my script.
Does anyone have any better solution or any other valid reason why this is happening and how to stop it?
I have a fairly complex master/content page that has several GridViews - each one in its own UpdatePanel. I also have four CustomValidators on the page, each one assigned to a different ValidationGroup. Two of the CustomValidators are buried inside a HeaderTemplates of a GridView (inside a distinct UpdatePanel). In the EmptyDataTemplate, HeaderTemplate and FooterTemplate of each GridView I have ImageButtons that postback to the server to perform some operation. All the ImageButtons take this form:
[Code]....
When I click on the ImageButtons in one of my GridView's the ImageButtons cause the ServerValidate event of two CustomValidators to fire. The server-side OnClick event of the ImageButton never fires. I have verified that the ImageButton(s) have CausesValidation="False" - what could be causing the validators to fire?
I have a UserControl which contains a TextBox and a CustomValidator. I would like to set the CustomValidator.ServerValidate to a method in the page that contains the UserControl I found this code which will allow me to dynamically set the custom validators validation function: cusvCustom.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(MethodName);
The problem is that a string value won't work there. It needs to be a reference to the method. Is it possible to use reflection (or some other method) to get a valid reference to the parent controls method using only the string name of it? The reason I want to use the string value of the method name is so I can place the control on the page thusly: <uc1:TextBoxField ID="tbUserName" runat="server" CustomValidationMethod="ValidateUserName" />
WebUserControl.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs" Inherits="WebApplication1.WebUserControl" %> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed" OnServerValidate="CustomValidator1_ServerValidate" /> <asp:TextBox ID="TextBox1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" /> WebUsecControl.ascx.cs using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebUserControl : System.Web.UI.UserControl { public ServerValidateEventHandler Validating; protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e) { if (Validating != null) Validating(sender, e); } } } TestPage.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="WebApplication1.TestPage" %> <%@ Register Src="~/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" /> </div> </form> </body> </html> TestPage.aspx.cs using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class TestPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //WebUserControl1.Validating += WebUserControl1_Validating; } protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e) { e.IsValid = false; } } }
I did some research and I found Type.GetMethod and MethodInfo but I can't get them to work. Primarily because I don't know the type of the parent control and can't figure out how to get it. EDIT: My code for matt-dot-net. As you can see it's almost an exact duplicate of your code. For whatever reason it does not work for me as I have it here. When I click on the button the page reloads and is the same. When I un-comment the one line though and click the button then I see the error message.
I have a web page with a nested grid. I bind the nested grid via the outer Gridviews RowDatabound. The problem I'm having is when a user click on a CommandField in the nested child grid the Page_Load event is fireing twice and reloading the grids, making it appear that the nest grid collapsed. I have tried using "If Not Page.IsPostBack" but because the page posts back twice the IsPostBack is false until the second time the Page_Load event fires.
Here is my Page_Load routine
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim test As Boolean = ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack If Not Me.Page.IsPostBack Then If Not ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack Then
I have editing enabled gridview. I m basically calculating many columns in rowdatabound event of gridview. For this i m using templatefields with labels control inside them.
Here is my c# code-
[Code]....
It doesnot cause any problem. But when i tried editing and click on edit button of my grid. It throws error in rowdatabound event's second line starting with decimal rejp.
Message- Object reference not set to an instance of an object.
Am i missing something or should i have to use any other event to handel editing in this case.
I am using radiobuttonlist and textbox in the gridview. I need to do operations on radiobuttonlist so that it should reflect on textbox . this is to be done after gridview_rowediting event fires i.e when we can see "update" and "cancel" buttons. I tried like this,
[code]...
here default selected value is "0" given in source.
My problem here is when I am changing the selecteditem, the corresponding action is not done.
[URL]Initially if I make a selection it will automatically post back and firecboItemsPerPage_SelectedIndexChanged. If I then post back by clicking one of the other buttons on the page, cboItemsPerPage_SelectedIndexChanged will continue to fire, despite the fact that the value has not been changed.I want it to be postback only when selected index is changed, not in every postback
The problem is that BindHeaders is called multiple times. I was expecting it to fire only once since a very similar code I used to customize the GridView control to use our mapping utility on the headers works this way.
I have one UpdatePanel which contains search criteria controls for a GridView.
The GridView is in another UpdatePanel.
I have a reset button in the search criteria UpdatPanel which resets all the search controls to their default values. However when I click this button for some reason my GridView RowDataBound method fires.
This is slowing the page down when the reset button is clicked. Does anyone know why this is happening and how to stop it. I haven't posted the code and explained it in plain english for ease of reading.
I have a listveiw populated from a List<> in the code behind using the following code:
SubId = Convert.ToInt32(Request.QueryString.Get("SubId")); using (JodyCoryDataContext myDataContext = new JodyCoryDataContext()) { var SelectedRings = from jewlery in myDataContext.Jewleries
[Code]....
The page displays the images from the list<> and button ok but nothing happens when i click it other than postback. Furthermore even when i set up a test button wich changes the ListView.selectedindex the ListView.SelectedIndexChanged event never fires even though i can read the selected index and see that it has changed.
I'm guessing that it's something to do with using a list<> from the code behind to populate the listview but i must confess to being completely clueless.
[Code].... all of a sudden my GridView has stopped firing the row update event. Friday I was working on it and was working flawlessly. Now I haven't made any changes to the code and all of a suddent has stopped working (yes rediculously bizarre and have been banging my head all day thinking what the something is wrong with it, excuse my french).
Everyone of those events fire except the update. I click the update button and nothing happens.
I ask for a date, on OnTextChanged event i bind data with a SqlDataSource to a dropdownlist and also bind data to a repeater passing the dropdownlist SelectedIndex (i use SqlDataSource too and ControlParameter to link the dropdownlist and the repeater) On my repeater during the ItemDataBound event i create dynamic dropdownlist controls, my code is :
[Code]....
[Code]....
On the Page_load event of my page i bind the data & recreate the control :
[Code]....
Everything works fine, datas are bound and SelectedIndexes are restore on every postback.
The probem occurs when user change different dropdownlists, if he choose to change the 1st dropdownlist the event is fire (normal), after if he modifies another dropdownlist the event is fire twice, if he modifies a 3rd time the event is fire 3 time etc...
I have visual 2010 with net 3.5 web app. I was implementing EntityFramework in my project and added a gridview to a entity source control. It worked ok, with select, update, and delete buttons enabled on the grid. But then, i needed to add a template field bound to a Foreign table, so my users select value from a list instead of writing the code. After that, my app fires error when i delete. I tried everything, inclue dakeyNames, write code on my RowDeleting, RowDeleted, RowCommand, but error persists.
Now, i can delete the row, but it then i get the error Object reference not set to an instance of an object If i implemente delete code on RowCommand i get the error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I have a dropdownlist outside the gridview control. My griview control also have dropdownlist in the first column. I want to populate the dropdownlist inside the gridview control for all rows when the selected item is changed in the outside dropdownlist.
why the gridview RowDataBound event fires during a select command. I only expect it to run when the gridview is being populated with data. Is there some way to stop it from running when the select command is triggered?
I have a long form having bunch of textboxes with validation controls. Submit button is on last of the page. for clicking submit button user needs to scroll to down the page. After clicking submit button, If any validation control fires. user is not knowing until he scroll up again.So I kept a lable next to submit button to show, if any validation controls fires. in code behind I wrote code like this
[Code]....
But control is not coming to here after validition control fires. my question is How should I show this lable visible "True" after validation control fires?
I am working with a page that has a series of checkboxes that at least one box must be checked.
The validator fires when I click the submit button and if no checkboxes are checked indicates the validation has failed. However If I check one of the checkboxes the validation message does not go away until I click the submit button again.
If I was using this on a control in which I had specified the ControlToValidate and I fixed the validation issue the error message goes away immediately.
However in this case I am not setting the ControlToValidate due to the neeed to validate serveral independent controls.
So my question is can I cause a re-validation of the custom validator? For instance I would like to add on each checkbox a onclick="revalidate()" that would then force the validation to happen again.
Here is some sample code that I wrote to demonstrate the scenario.
<script type="text/javascript"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> function IsOneCheckboxChecked(sender, args) { if (!$('[id$=checkBox1]').attr('checked') &&
Does anyone know how to make CustomValidator work with controls which are in DetailsView for example ahen updating item
CustomValidator doesnt work on controls which are in detailsView after performin for example update on that detailsView. For example RequiredFiledValidator works with no problem while CustomValidator has problems (Validation doesnt occur):/
I have a very simple CustomValidator control as follows:
[Code]....
If I use this on a basic aspx page as follows:
[Code]....
When I press Button1, I would expect the CustomValidator to fire only if there is a value in TextBox1.Instead, the CustomValidator is ALWAYS fired. Even adding ValidateEmptyText="false" to the validator does nothing to change this behaviour.What am I missing from my control implementation that is causing this?
I have two CustomValidators, both of which function in the same way (call a query and check if a particular value is available in the DB), the only difference is the query itself.
Rather than writing the same function definition in the code-behind, I would like to find a way to execute a particular query depending on which CustomValidator called the function (I guess using the CustomValidator's ID) so if I have the follwing:
[Code]....
How can I check in the code behind , which validator called the function "Check"??
I am clicking one linkbutton to check the validators clientside in the panels. This works well in IE, but not in Firefox. When I click the button, it does not even hit the validateLength function
Sample code below. Either I am doing something wrong or missing something in 4.0.
1: The function CheckText fires upon button click in IE but not in FF. 2: The function CheckText fires upon button click in IE /FF when the site runs under 2.0 or 3.5.
I found this article [URL] on how to create a customvalidator for the checkboxlist. Followed all the way thru, but when i load my page i get the following error, how can it exist in 2 places? How can i resolve this to use the validation on the page?
Compiler Error Message: CS0433: The type 'CustomValidators.RequiredFieldValidatorForCheckBoxLists' exists in both 'c:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files oot294f506e54827e67assemblydl3b4b5a368e15533_8dcfca01RequiredFieldValidatorForCheckBoxLists.DLL'and 'c:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files oot294f506e54827e67App_Code.5yh9snf-.dll'