requiredfieldvalidator stops valid event - c#

I have a requiredfieldvalidator for a dropdownlist. Code:
<asp:DropDownList ID="ddlSoortGebeurtenissen" runat="server"
DataSource="<%# SoortGebeurtenissen %>" CssClass="inputtext"
CausesValidation="False" ValidationGroup="valGroupSelectControls"
DataTextField="Title" DataValueField="ID" AutoPostBack="True"
OnSelectedIndexChanged="ddlSoortGebeurtenissen_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredValidatorSoortGebeurtenis" runat="server"
ErrorMessage="Invullen van Soortgebeurtenis is verplicht"
ToolTip="Invullen van Soortgebeurtenis is verplicht"
ControlToValidate="ddlSoortGebeurtenissen"
Display="Dynamic"
CssClass="ui-icon-errormsg"
Text="Invullen van Soortgebeurtenis is verplicht"
ValidationGroup="valGroupSelectControls" />
The selectedIndexChanged event fires when needed, except when the validation has been done before. When I press the submit button having selected an empty value, the validator nicely shows me the error message. Then when I change the selection, and expect the selectedIndexChanged event to fire, it doesn't fire. Nothing happens until I trigger another postback. (lets say the close button). Then the selectedIndexChanged event is fired AND the event of the actual button is fired.
Somehow the selectedIndexChanged event sticks around until the next postback, but does not cause the postback itself. This only happens when the validation shows the error beforehand.

Remove CausesValidation="False" property from dropdownlist

Try using
EnableClientScript="False"

Related

Custom Validator not firing

I realize there are lots of similar posts, however I have not found one that has worked for me unfortunately. Basically, I have an asp:customvalidator that I am trying to add to a validationgroup with other validators so that all error messages appear in the same alert. Here is the customvalidator
<asp:TextBox runat="server" ID="txtVideo1Url" Columns="20" Width="98%" />
<asp:CustomValidator runat="server" ID="valURL1" ControlToValidate="txtVideo1Url" OnServerValidate="txtVideo1Url_ServerValidate" Display="None" ValidationGroup="submission" />
and here is the event
protected void txtVideo1Url_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = false;
valURL1.Text = "FAIL!";
}
The event isn't firing at all and I have no idea why. Once I can get the event firing I can put some actual logic into it, lol
UPDATE: I've noticed that I am now able to get the event firing, however the validationsummary is set to display all errors in a messagebox and this error isn't getting added to the messagebox.
Remember to set this property on the CustomValidator...
ValidateEmptyText="True"
You need to set the CausesValidation property of the TextBox to true, like this:
<asp:TextBox runat="server" ID="txtVideo1Url" Columns="20" Width="98%" CausesValidation="true" />
You will have to add ValidationGroup="submission" to the ASP.NET control that will fire the postback.
CustomValidators don't fire if other Validators in your ASPx are not validating. You may need to force a Page.Validate("something"), with your specific validation group. I suggest look at OnTextChanged event to force a page validate.

Disabled textbox value is getting lost on click of a button

I'm using the following dropdown & mapping its value into a textbox.
<asp:DropDownList ID="ddlCntry" runat="server" Height="24px" Width="160px"
OnChange="Country();">
<asp:ListItem>Select Country</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TxtCntryRisk" runat="server"
ontextchanged="TxtCntryRisk_TextChanged" Width="153px"
EnableTheming="True" Enabled="False"></asp:TextBox>
I have a calculation button, when I click on the button my mapped textbox value gets lost.
I want my textbox to be disabled so that its value cannot be changed.
I tried using Readonly=true property also but in vain. Session, viewstate also not working.
but when I enable the textbox, it's working fine, no loss of value.
any help??
thanx in advance. :-)
Set the ReadOnly attribute server side:
TxtCntryRisk.Attributes.Add("readonly","readonly");

Cannot run event handler in .cs file from ModalPopupExtender

I want to create a simple Yes/No popup window. So I have added a ModalPopupExtender to the .aspx page.
The popup panel's markup is:
<asp:Button ID="btn_yes" runat="server" Text="Yes" onclick="btn_yes_Click" />
<br/>
<br/>
<asp:Button ID="btn_no" runat="server" Text="No" />
For some reason, I cannot get into the function btn_yes_Click, not sure why. Do I have to use Javascript?
I suspect that you are also setting the ModalPopupExtenders OkControlID property to "btn_yes".
As you can see in this related question this will cause the OnClick event not to fire.
There is a chance that the OKControlID Property on ModalPopupExtender tag is specified. Take a look at this topic: The Event Handler of the OK button inside a Modal PopUp is not executing

onselectedindexchanged in a drop down list on the same item is called only client side?

I have an update panel :
<asp:UpdatePanel ID="upAppartiene" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddAppartiene" runat="server" AutoPostBack="true" onselectedindexchanged="ddAppartiene_SelectedIndexChanged">
<asp:ListItem Text="---" Value="0"></asp:ListItem>
<asp:ListItem Text="Value 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value 2" Value="2"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
When I select each time a different value, all is ok : it call the context on server side, page load, and ddAppartiene_SelectedIndexChanged() method.
But, if I click on a value, than I re-click on the same value, it do the call to the server (Page Load is called) but the method ddAppartiene_SelectedIndexChanged() it is not called.
So, onselectedindexchanged works on client side, but on server side not? I don't think so...
Where am I wrong?
I'm just a newbie but doesn't onselectedindexchanged event only fire when the selected index is actually changed? If you re-click the same value there is no change so the event doesn't fire.
Just like the event name says "SelectedIndexChanged", the index needs to change before it is fired. When you click the same item twice, the index is still the same as the previous and therefore has not changed. At this point, I think it is just a clicked and not changed.
What I do to avoid that is to actually provide an OK button. Whenever you click the button, the selected value is always picked, changed or not.

AJAX Update Panel - Action button click on drop down selection

Evening all
I have the following scenarion. I have a range of drop down menus where a clietn can select. The code below is wrapped in an update panel but without this, the button click fires a method to retrieve the number of products. So for example, an item is selected from ddlCategory, the btnValidate is clicked and the label returns the number of products within that category.
I have the following code for an update panel - I'm just not sure how to implement if effectively.
<asp:UpdatePanel ID="UpdatePanel1" runat="Server">
<ContentTemplate>
<asp:Label ID="lblSearchResultsStatus" runat="server" Text="Number of results found: "></asp:Label>
<asp:Label ID="lblSearchResults1" runat="server" Text=""></asp:Label>
<br />
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Search"
OnClick="btnValidate_Click" Width="120px" />
</ContentTemplate>
</asp:UpdatePanel>
How do I go about wiring the update panel so that when a drop down list item is selected, the buttong is effectively clicked?
Do I have to implement something on each of the ddlSelectedIndexChanged event or is there a property within the update panel that does?
Apologies for the noob question.
The point of the UpdatePanel is to update a portion of the page with an AsyncPostBack instead of reloading the whole page, but in order for the drop-down lists to trigger an AsyncPostBack automatically, they must be on an UpdatePanel. In order to update the labels, they must be on the same UpdatePanel with the labels.
A common pattern to implement what you want to accomplish:
Put the DDLs on the UpdatePanel, and set AutoPostBack="true" on each DDL, so they trigger AsyncPostBacks.
Add an event handler for SelectedIndexChanged on each DDL (can be the same event handler).
Move whatever you do in btnValidate_Click to another method.
Call the new method from btnValidate_Click and the SelectedIndexChanged event handler(s) so they all perform the same function.
You can call your btnValidate_Click event from codebehind at any point, i.e. Page_Load
protected void Page_Load(object sender, EventArgs e)
{
btnValidate_Click(btnValidate, new EventArgs());
}

Categories