Radcombobox OnItemRequested firing page validation - asp.net

I have some really weird stuff going on here.
I have the following RadComboBox and button in the master page:
<telerik:RadComboBox ID="rcbPesquisa" runat="server" CausesValidation="false" ShowToggleImage="false" LoadingMessage="Carregando..." HighlightTemplatedItems="true" OnClientKeyPressing="onKeyPressing" EmptyMessage="Buscar" EnableLoadOnDemand="True" EnableVirtualScrolling="true" OnItemsRequested="rcbPesquisa_ItemsRequested" AllowCustomText="True" AutoPostBack="true" OnSelectedIndexChanged="rcbPesquisa_SelectedIndexChanged" Width="350">
<asp:Button runat="server" ID="btnExcluir" Text="Excluir" OnClick="btnExcluir_Click" CausesValidation="true" ValidationGroup="Excluir" CssClass="btn" OnClientClick="return confirm('Deseja realmente excluir o registro?');" />
In the page, I have this validator:
<asp:CustomValidator ID="cuvExclusaoRelacionamento" runat="server" ValidationGroup="Excluir" OnServerValidate="cuvExclusaoRelacionamento_ServerValidate"></asp:CustomValidator>
What happens is: When i click the combobox, and it tries to load it's items, cuvExclusaoRelacionamento validator is called on the server, and of course things go south.
By the way... __EventTarget on cuvExclusaoRelacionamento_ServerValidate is empty

Unfortunately, and without seeing the full code I'm only guessing, I believe there is no way of getting around this due to the following settings:
ValidationGroup="Excluir" The ValidationGroup property assigned to an input control is the Validation Group which should be triggered when the control posts back; The ValidationGroup property of a validator control is the name of the group to which it is assigned; Finally the ValidationGroup property of a submitting control (which could be a button but can also be the input control) is the name of the group of Validators to validate the page against before posting back to the server. I should point out at this timethat it will not post back (server-side event handlers will not be triggered) if the validation fails. Given that the validation group of the control is the same as the validation group of your Custom Validator the validator will be triggered when the control attempts to post back to the server (e.g. when being clicked), if this validation fails it will not post back.
AutoPostBack="true" When you set the control to auto post back it will post back to the server when updated. Given you have an OnClick event I'm guessing you need to perform some server side action when the state of the control is changed.
CausesValidation="true" When set to true all validators assigned to the same "Validation Group" as the submitting control will be triggered when the control attempts to post back to the server. As I've said above, this will prevent post back (meaning the server-side Event Handlers will not be triggered) if the validation fails.
Given the information provided, I'm guessing that the best action you could take is to set CausesValidation="false" for the control. The validation can still be performed so long as you have a submit button (or other control which triggers a post back) elsewhere which is assigned to the "Excluir" Validation Group.

Related

Why ASP.NET Controls defined in mark up already created on Page_PreInit life cycle event

I read Microsoft's article on ASP.Net Page Life Cycle events. It confused on one thing. When Page_PreInit gets called, all the controls' Init method is not yet called. When I setup a test project, I observed a different behavior. In the mark up, I created asp label and button controls and set certain properties such as Text. I put a break point at the beginning of Page_PreInit. When break point got hit, I checked if the controls got created or not by referring to them by their Ids in the Watch window. They all existed and none returned null. Then I checked the Text property and it was what I set in the mark up. So doesn't this contradict what Microsoft says? If this is the case what do Controls' Init method do if they are already initialized? Is there something I miss?
I created asp label and button controls and set certain properties
such as Text. ... Then I checked the Text property and it was what I set
in the mark up.
If you set property value at Design Time, they are in control tree so properties are available in all events.
However, if you add a TextBox and a user clicks on submit, TextBox Text Property only start available at Page_Load event.
The reason is that Page_Load is the place where properties are loaded with information recovered from view state and control state.
Look at this example
<asp:Label runat="server" ID="Label1" Text="Name" />
<asp:TextBox runat="server" ID="TextBox1" />
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />
<asp:Button runat="server" ID="SubmitButton"
Text="Submit" OnClick="SubmitButton_Click" />
After Postback,
Notice that you can only retrieve TextBox value at Page_Load event.

DropDownList / RadioButtonList - I don't need to validate selected value?

I have used ASP.NET MVC and there when I have for example dropdown list - I must check if selected value is correct because user can change value on dropdown list using for example Firebug.
I am new in ASP.NET Web Forms. I have for example:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
</asp:DropDownList>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="1" Selected="True">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
</asp:RadioButtonList>
I have changed in Firebug selected value in DropDownList1 on 3 and in RadioButtonList1 on 3 and submit form. Then I have error:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for va...
Does it mean that I don't need to validate selected value because user can't change it?
Generally speaking, yes, EnableEventValidation works with that purpose in mind, you can read more here: http://msdn.microsoft.com/en-us/library/system.web.ui.page.enableeventvalidation(v=vs.110).aspx
In particular:
When the EnableEventValidation property is set to true, ASP.NET validates that a control event originated from the user interface that was rendered by that control. A control registers its events during rendering and then validates the events during postback or callback handling. For example, if a list control includes options numbered 1, 2, or 3 when the page is rendered, and if a postback request is received specifying option number 4, ASP.NET raises an exception. All event-driven controls in ASP.NET use this feature by default.
So as a matter of fact you can be safe knowing that people cannot tweak your client HTML with the console and post back malicious data.
This said, you may want to put some guard in the server code just in case for some pages you need to disable event validation (it may happen if you want to submit strings that look like code or if you want to modify elements on the page with jQuery for example).

Disable Validation controls in asp.net pages

My asp.net page has multiple text-boxes and DropDownLists that all have required-field validators.
My site also has multiple pages that you can move between with the click of a button. (All pages have all the buttons) When a page loads and decide I want to go to a different page without entering information into the form, I click a button to move to a separate page and the validation pops up and I can't change the page, it stops me every time?
Any ideas on how to stop this?
I know you already found a solution, but I just wanted to throw in another method for people to see.
When using validators it is common practice to use ValidationGroups. When a validator belongs to a ValidationGroup, it is only triggered by another control in the validation group. For example:
<asp:TextBox ID="NameBox" runat="server"/>
<asp:RequiredFieldValidator ID="NameVal" ControlToValidate="NameBox"
ValidationGroup="ValSubmit" runat="server"/>
//...More input fields and validators...
<asp:Button ID="SubmitButton" ValidationGroup="ValSubmit" runat="server"/>
Using this method any validators with the "ValSubmit" ValidationGroup will be triggered only by the SubmitButton and not by other controls. Now you don't have to put CausesValidation="false" on EVERYTHING else that causes a postback.
As everyone else had mentioned, set the CausesValidation property to false.
<asp:Button ID="MyButton" Text="Go Back" CausesValidation="False" />
This is straight from the Microsoft Help Page:
By default, page validation is performed when a Button control is clicked. Page validation determines whether the input controls associated with a validation control on the page all pass the validation rules specified by the validation control.
You can specify or determine whether validation is performed on both the client and the server when a Button control is clicked by using the CausesValidation property. To prevent validation from being performed, set the CausesValidation property to false.
Note:
You should set the CausesValidation property to false when you are using the PostBackUrl property to post back to a different page. You should explicitly check validation when posting back to a different page. For an example, see the Remarks section of the PostBackUrl property.
This property is commonly set to false for a reset or clear button to prevent validation from being performed when the button is clicked.
When the value of the CausesValidation property is set to true, you can also use the ValidationGroup property to specify the name of the validation group for which the Button control causes validation.
set property CausesValidation = "false" to the buttons where you don't want to trigger validation
CausesValidation="false" on the button ;)
<asp:Button CausesValidation="True|False" />
Gets or sets a value indicating whether validation is performed when the Button control is clicked.
read more about Button.CausesValidation Property

asp.net textarea postback before submit button

I have an ASP.NET form with a few controls and a submit button at the bottom, all inside an update panel:
<asp:UpdatePanel runat="server" ID="upContent">
<ContentTemplate>
<asp:TextBox runat="server" ID="tbxMyTextBox" AutoPostBack="true" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return doStuff()" OnClick="btnSubmit_Click" />
</ContentTemplate>
</asp:UpdatePanel>
If I write something in the TextBox and click 'submit' immediately (without clicking out of the TextBox first), the changes are not recorded (as seen in the server-side event handler). However, if I write something in the TextBox, and then change focus to another control, an AutoPostback happens through the UpdatePanel and then clicking 'submit' does recognize these changes. How can I force this content to update right as I click the submit button, while still running both the clientside and serverside events attached to it? Thanks!
Is is possible that your text-box gets cleared due to some on-submit/on-click event handler? I will suggest you do use some tool such as Fiddler to inspect the POST data in request. Or you can put a break-point at your server side code and inspect the request data. Look for particularly Request.Form[tbxMyTextBox.UniqueID] - i.e. look for presence of value for your text-box's name (name property at client side which corresponds to UniqueID property at server side). If the request contains the value typed in the text-box then something is happening on server side but good news is you can always retrieve the value from Request object. If the value is not present in the Request object then it means that client side code is clearing the value before the submit.
If an onclick method returns false, it cancels the action that the button would normally perform. As your button would normally submit your form, but it appears not to be submitting, your client side javascript code in doStuff is probably returning false.

ASP.NET :: Can a textbox control's onblur/lostFocus event trigger a server-side event?

I am trying to attach a server-side event to lookup the city/state for the user-entered zipcode in a field like the one below.
<asp:TextBox ID="TextZipcode" runat="server" CssClass="inputtext" Columns="10" MaxLength="10"></asp:TextBox>
Since there is no lost focus event to capture, has anyone had any luck getting this to work?
Sure, why not? Just attach a JavaScript handler and have it invoke an AJAX action.

Resources