Isolate a required field validator? - asp.net

I have two search buttons on a page, one linked to a dropdown list and one linked to a dropdown list with a textbox for more search criteria. I have required field validators on all of the aforementioned controls. When I choose something from the first dropdown and click the appropriate search button, the field validator for the textbox fires, disabling the first search button. Is there a way to localize/isolate the validators to only associate with one of the two buttons? Code below:
<asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
<asp:Label ID="Label1" runat="server" Text="Search by status:"></asp:Label>
<asp:DropDownList ID="DdlStatus" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Status" DataValueField="Status" AppendDataBoundItems="true">
<asp:ListItem Text="Choose a status" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="BtnStatusSearch" runat="server" Text="Search" onclick="BtnStatusSearch_Click" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [Status] FROM [Status]"></asp:SqlDataSource>
<asp:Label ID="LblSearch" runat="server" Text="Other search:"></asp:Label>
<asp:DropDownList ID="DdlSearch" runat="server">
<asp:ListItem Selected="True" Value="0">Choose search criteria</asp:ListItem>
<asp:ListItem Value="1">Broker</asp:ListItem>
<asp:ListItem Value="2">Customer</asp:ListItem>
<asp:ListItem Value="3">Customer State</asp:ListItem>
<asp:ListItem Value="4">Broker State</asp:ListItem>
</asp:DropDownList><asp:RequiredFieldValidator ID="RfvDdlSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="DdlSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:TextBox ID="TbSearch" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTbSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="TbSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSearch" runat="server" onclick="BtnSearch_Click" Text="Search" />

Yes, you can use the ValidationGroup property and set that validation group to your button control: ValidationGroup="button1"
<asp:RequiredFieldValidator ID="rfv" runat="server" ValidationGroup="button1"
ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Button ID="btnLogin" runat="server" Text="Login" ValidationGroup="button1" OnClick="btnLogin_Click" />

Related

ASP.NET RequiredFieldValidator : How can i bind value to " InitialValue " property?

I have the following code
<div>
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue="Choose" ControlToValidate="testDropDownList"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
<br />
</div>
in which i validate Dropdownlist by RequiredFieldValidator
If changed the value of initialvalue property to read from static property in stactic classs .. but it always give me emtpy string in runtime unless this property have the value "Choose" ...
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue='<%# Util.ChooseValue %>' ControlToValidate="testDropDownList">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
Could Any one help me to know what's the issue in my code ??
Please call
testRequiredFieldValidator.databind()
in page load event and let me know if this is still an issue.
Set InitialValue of RequiredFieldValidator on page_load event in aspx.cs page.

Validating dropdownlist inside gridview control

for testing i have tried customvalidation
function ClientValidate(sender, args) {
//return false for testing...
args.IsValid = false;
}
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="ddldetail"
Text="Please select" ValidateEmptyText="true"
ClientValidationFunction="ClientValidate"
Display="Dynamic">
</asp:CustomValidator>
edit: here is what i exaclty wants to happen:
how to validate a dropdownlist and i have done this zillion times but what i am doing wrong here? any second pair of eye might spot it? i am trying to validate the dropdownlist if the user have not select any help?
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" CausesValidation="true"/>
<asp:GridView ID="GVInputMapping" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
EnableModelValidation="True" onrowdatabound="GVInputMapping_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" ControlStyle-Width="250px" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddldetail">
<asp:ListItem Selected="True" Value="0">Select me</asp:ListItem>
<asp:ListItem Value="1">abc</asp:ListItem>
<asp:ListItem Value="2">GHt</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredDDL" runat="server"
ControlToValidate="ddldetail" ErrorMessage="Please select" InitialValue="Select me" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have the InitialValue of the validator set to Select me, but the Value of that item is actually 0:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select Me" Value="0" />
<asp:ListItem Text="Foo" Value="1" />
<asp:ListItem Text="Bar" Value="2" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="Please select"
InitialValue="0"
Display="Dynamic">
</asp:RequiredFieldValidator>
You'll also need to assign a unique validation group for each row, otherwise validation will kick off for every row. To assign a unique validation group, you can use the Id column:
ValidationGroup='<%# string.Format("Group_{0}", Eval("Id")) %>'
You would add this to the validator and the button in the row.

Calling a RequiredFieldValidator and ValidatorCallExtender inside GridView

I have a GridView with its columns being TextBoxes and after user made modifications to data within GridView I have a "Commit" Button thats located outside the GridView and also located under a different Content within the page... How do I set the Validator to work even though I have the validator set correctly but when I click on the "Commit" button it doesn't check for the validation and I believe its because the button is not inside the GridView or UpdatePanel... Is there a way to get around that? or A better approach?
Thanks for your help in advance.
Please select a Test from the dropdown below.<br />
<asp:DropDownList ID="ddlResult" runat="server"
onselectedindexchanged="ddlResult_SelectedIndexChanged"
AutoPostBack="True" CausesValidation="false">
</asp:DropDownList>
<br />
<asp:UpdatePanel ID="upGrid" runat="server">
<ContentTemplate>
<asp:GridView ID="grdResults" runat="server"
CssClass="gridview"
RowStyle-CssClass="gridview_itm"
AlternatingRowStyle-CssClass="gridview_aitm"
HeaderStyle-CssClass="gridview_hdr"
Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text='<%#Eval("Test")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Result">
<ItemTemplate>
<asp:TextBox ID="tbResult" runat="server" Text='<%#Request.QueryString["t_ID"] == null ? null : Eval("n_Result") %>'></asp:TextBox>
<asp:Label ID="lblResult" runat="server" Text='<%#Eval("Validate")%>' ForeColor="#D50000"></asp:Label>
<asp:FilteredTextBoxExtender ID="ftbe" runat="server" TargetControlID="tbResult" FilterType="Custom, Numbers" ValidChars='<%#Eval("n_Mask")%>' />
<asp:RequiredFieldValidator runat="server" ID="RReq"
ControlToValidate="tbResult"
Display="None"
ErrorMessage="A Result is required." />
<asp:ValidatorCalloutExtender runat="Server" ID="RReqE"
TargetControlID="RReq"
HighlightCssClass="validatorCalloutHighlight" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Completed">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server" Text='<%#Request.QueryString["t_ID"] == null ? null : Eval("d_DateCompleted") %>'></asp:TextBox>
<asp:Image ID="imgCalendar" runat="server" ImageUrl="~/App_Themes/Sugar2006/images/Calendar_scheduleHS.png" ImageAlign="Middle" />
<asp:CalendarExtender ID="ce" runat="server" TargetControlID ="tbDate" PopupButtonID="imgCalendar" />
<asp:MaskedEditExtender ID="mex" runat="server"
TargetControlID="tbDate"
Mask="99/99/9999"
MaskType="Date"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError" />
<asp:MaskedEditValidator ID="mev" runat="server"
ControlToValidate="tbDate"
ControlExtender="mex"
Display="Dynamic"
InvalidValueMessage="This date is invalid!" Font-Bold="True"
ForeColor="#D50000" />
<asp:RequiredFieldValidator runat="server" ID="DReq"
ControlToValidate="tbDate"
Display="None"
ErrorMessage="A Date is required." />
<asp:ValidatorCalloutExtender runat="Server" ID="DReqE"
TargetControlID="DReq"
HighlightCssClass="validatorCalloutHighlight" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="lblSave" runat="server" Text="**After Each Test Entry Please Save." ForeColor="#D50000"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlResult" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:Content ID="Content1" ContentPlaceHolderID="cntSidebar" runat="server">
<asp:Button ID="btnSave" runat="server" Text="Save" Width="80%"
onclick="btnSave_Click"/> <br />
<asp:Button ID="btnClose" runat="server" Text="Close" Width="80%"
onclick="btnClose_Click" CausesValidation="false"/>
</asp:Content>
Have you tried setting the following properties on the Commit button:
CausesValidation="true" ValidationGroup="vgMyGroup"
Also, try setting the validation controls property: ValidationGroup="vgMyGroup"
Another suggestion could be to do a postback on the Commit button's click event and check for
if(Page.IsValid)...
I would be interested to see if setting the ValidationGroup property persists across the ASP content controls..

AJAX ComboBox renders as TextBox

I have page with 2 comboboxes. They both have unrelated sets of data so a cascadingcombobox did not seem to make sense. When the page renders combobox A is fine, B renders as a textbox. If I disable combobox A then B will render properly.
Here are my defintions (each combo box is sitting in its own panel).
<asp:ComboBox ID="cboRequestType" runat="server" CssClass="comboBoxes"
DropDownStyle="DropDownList" ItemInsertLocation="Append" AutoPostBack="True"
AppendDataBoundItems="True" onselectedindexchanged="cboRequestType_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="Select Request Type..." Value="Select Request Type" />
</asp:ComboBox>
<asp:ComboBox ID="cboContactName" runat="server" CssClass="comboBoxes"
DropDownStyle="DropDownList" ItemInsertLocation="Append" AutoPostBack="True"
AppendDataBoundItems="True" onselectedindexchanged="cboContactName_SelectedIndexChanged">
</asp:ComboBox>
Here are the full segments and a better description of test cases.
When page loads with both comboboxes cboContactName renders as a txtbox.
If I sent pnlRequestType.visible = false, then cboContactName renders properly.
Changing cboRequestType to an element allows for cboContactName to render as a combolist.
<asp:Panel ID="pnlRequestType" runat="server">
<asp:Label ID="lblRequestType" Text="Request Type" runat="server"
CssClass="comboBoxLabel" />
<asp:ComboBox ID="cboRequestType" runat="server" CssClass="comboBoxes"
DropDownStyle="DropDownList" ItemInsertLocation="Append" AutoPostBack="True"
AppendDataBoundItems="True" onselectedindexchanged="cboRequestType_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="Select Request Type..." Value="Select Request Type" />
</asp:ComboBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Select A Catagory." CssClass="fieldValidationMessage"
ControlToValidate="cboRequestType" InitialValue="Select Request Type" >
</asp:RequiredFieldValidator>
</asp:Panel>
<asp:Panel ID="pnlContact" runat="server">
<asp:Label ID="lblContactName" Text="Contact" runat="server"
AssociatedControlID="cboContactName" CssClass="comboBoxLabel" />
<asp:ComboBox ID="cboContactName" runat="server" CssClass="comboBoxes"
DropDownStyle="DropDownList" ItemInsertLocation="Append" AutoPostBack="True"
AppendDataBoundItems="True" onselectedindexchanged="cboContactName_SelectedIndexChanged">
</asp:ComboBox>
</asp:Panel>

asp.net Validation on DropDownList box

I have a dropdownlist (cboViewAlbums) which has displays album values. The first item is a
Please select an album.... I am trying to use validation which when lb_create_album linkButton is clicked throws an error if the cboViewAlbums list has the value 0 selected.
Below is the code for the this and my attempt:
<asp:DropDownList ID="cboViewAlbums" runat="server"
DataSourceID="SqlDataSource1" DataTextField="album_name"
DataValueField="album_id" Width="250px" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0">Please select an album...</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lb_create_album" runat="server">Create Album</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpaConnectionString %>"
SelectCommand="SELECT [album_id], [album_name] FROM [fpa_albums] ORDER BY [album_name]">
</asp:SqlDataSource>
<br />
<asp:HyperLink CssClass="example7" ID="hLinkUploadPhotos" NavigateUrl="multiple_upload.aspx" runat="server">Upload Multiple Photos</asp:HyperLink>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="Please select an album..."></asp:RequiredFieldValidator>
Any idea how I can get this working?
Thanks
You have to use Range validator with dropdown list & set mininmum value greater then 0 & maximum value to set any max value , aslo provide type value of min & max and that is integer.
Below is the sample code i have make for you you have to bind your datasource insted of static list items.
<asp:DropDownList runat="server" ID="ddl1" >
<asp:ListItem Value="0" Text="Select value" />
<asp:ListItem Value="1" Text="text1" />
<asp:ListItem Value="2" Text="text2" />
</asp:DropDownList>
<asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
MinimumValue="1" MaximumValue="100000000" Type=Integer />
<asp:Button Text="text" runat="server" />
If this helpful to you please mark as an answer
Thanks
Arun
First, you can't do validation with HyperLink, better use the LinkButton. HyperLink does not do post back, so that's your first error.
Second, on your RequiredFieldValidator, put the initialvalue=0, and that should fix your problem.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="0"></asp:RequiredFieldValidator>

Resources