I am using Ajax validation extender with the ListBox. On submitting the form, the validator must check if atleast one entry of the ListBox has been chosen or not. How can that be done?
You can use the RequiredFieldValidator(the ValidatorCalloutExtender is not responsible for the validation) to validate if the user selected at least one item of the ListBox. Here is an example: MSDN/RequiredFieldValidator.InitialValue:
<asp:ListBox id="list"runat="server">
<asp:ListItem Value="Australia">Australia</asp:ListItem>
<asp:ListItem Selected="True" value="NoCountry">--ChooseCountry--</asp:ListItem>
<asp:ListItem Value="USA">USA</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator id="valList"
ErrorMessage="Selection Invalid!"
ControlToValidate="list"
InitialValue="NoCountry" runat="server"/>
Related
Select multiple value in DropDownList using ASP.NET and C#. I tried it to select single value from drop down but unable to find multiple selection.
In that case you should use ListBox control instead of dropdown and Set the SelectionMode property to Multiple
<asp:ListBox runat="server" SelectionMode="Multiple" >
<asp:ListItem Text="test1"></asp:ListItem>
<asp:ListItem Text="test2"></asp:ListItem>
<asp:ListItem Text="test3"></asp:ListItem>
</asp:ListBox>
Take a look at the ListBox control to allow multi-select.
<asp:ListBox runat="server" ID="lblMultiSelect" SelectionMode="multiple">
<asp:ListItem Text="opt1" Value="opt1" />
<asp:ListItem Text="opt2" Value="opt2" />
<asp:ListItem Text="opt3" Value="opt3" />
</asp:ListBox>
in the code behind
foreach(ListItem listItem in lblMultiSelect.Items)
{
if (listItem.Selected)
{
var val = listItem.Value;
var txt = listItem.Text;
}
}
Dropdown list wont allows multiple item select in dropdown.
If you need , you can use listbox control..
ASP.NET List Box
For multiple selection dropdown list,cannot accomplish it directly using dropdown..Can be done in similar ways..
Either you have to use checkbox list or listbox (ajax inclusive)
http://www.codeproject.com/Articles/55184/MultiSelect-Dropdown-in-ASP-NET
http://social.msdn.microsoft.com/Forums/vstudio/en-US/54374df7-5a54-42bc-83b8-ad5994cb634d/multi-select-dropdownlist
http://www.dotnetfunda.com/articles/article1591-multiselect-dropdownlist-in-aspnet-using-csharp-40-.aspx
I have a GridView which has a TextField column in it .. I've setup validation for this TextField column so that it requires an input (i.e. its not optional), and that the input can be a positive integer only ..
The errors do show up when a text field is either empty or has doesn't have a positive integer value, but there's a server-side button which still executes even when there are errors in the GridView ..
I want the button to NOT do its processing if there are inputs errors .. Currently this doesn't happen, as the button's click event is still called even when there are errors ..
GridView Markup Code:
<asp:GridView ID="EPSAndTSRValuesInputGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="EPS Value">
<ItemTemplate>
<asp:TextBox ID="EPSValue" Text='<%# Eval("EPSValue") %>' runat="server" CausesValidation="True" ValidationGroup="Display"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter a valid value for EPS!"
ValidationExpression="^\d*$" ControlToValidate="EPSValue" ValidationGroup="Display"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="EPSValue" ValidationGroup="Display"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Button which performs the next action:
<asp:Button ID="btnDisplayReport2"
runat="server" CssClass="ButtonStyle"
Text="Display Report" ValidationGroup="Display" OnClick="btnDisplayReport2_Click" CausesValidation="true"/>
This is happening because your Button have validation group Display so on click of it will validate only control with same group i.e. Display.As I can see there no validation group of your Textbox so it will not validate it in button click..,to cause validation on Click of Button Add same validation group in your Textbox,RegularExpressionValidator, and RequiredFieldValidator too.
I think it's because they don't have the same ValidationGroup.
Try adding ValidationGroup="Display" to your validators.
You are saying to execute the validation group Display on your button click.It will validate only control have the validation group (dispaly) as you mentioned.You are not defined any validation group for Textbox validators Try by Add same validation group(displa) in your Textbox,RegularExpressionValidator, and RequiredFieldValidator.
Is there any way do delay the autopostback on a dropdownlist until the list actually loses focus? I want to accept input from the keyboard without the page posting back after every keystroke. For example, take this code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem >Test1</asp:ListItem>
<asp:ListItem >Tst1</asp:ListItem>
<asp:ListItem >twotest</asp:ListItem>
</asp:DropDownList>
Say I want to select twotest. I tab to the ddl and input 'tw' into my keyboard. However, as soon as the 't' is entered, the page automatically posts back, while the 'w' input is lost. Resetting focus doesn't really help any as then the ddl thinks that 'w' is the first input. Any help would be appreciated.
It seems impossible. You must remove AutoPostBack="true" and try another way to perform postback. Consider below solution:
when dropdown ddlCountry changed, the function ddlCountry_changed is called that trigger btnLoad click to perform postback. The button btnLoad is put inside an update panel so the page is not reload and you still have focus on the dropdown.
<script type="text/javascript">
function ddlCountry_changed() {
document.getElementById('<%= btnLoad.ClientID %>').click();
}
</script>
Country:
<asp:DropDownList ID="ddlCountry" runat="server" onchange="ddlCountry_changed();">
<asp:ListItem Text="Vietnam" Value="1"></asp:ListItem>
<asp:ListItem Text="United State" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Button ID="btnLoad" style="display: none" runat="server" OnClick="btnLoad_OnClick" />
</ContentTemplate>
</asp:UpdatePanel>
It's a little more complicated than just handling the blur. You need to remove the onchange attribute (AutoPostBack="false") so keyboard navigation is possible and attach event handlers that submit the form when the value has changed via clicking, blur, or the enter key is pressed.
I ran into this same issue and created a plugin for handling this. Check out https://github.com/bstruthers/AutoPostBack-Fix.
I have a panel, controls in the panel..these controls validated with a validation group.
This panel will show / hide when a dropdown box changes.
Question is...i want to validate all the controls with validation group in the panel
how to write the code
You define validationgroup="Test" on your validators , and you define also this validationgroup="Test" on your Button.
Sample
.....
<asp:requiredfieldvalidator id="NameTextBoxRequiredValidator"
controltovalidate="NameTextBox"
display="Dynamic"
text="Please enter your name."
validationgroup="ForPanel" <----------------
runat="server"/>
<asp:button id="SubmitButton"
text="Submit"
validationgroup="ForPanel" <------------------
runat="server"/>
Nota : Gets or sets the name of the validation group to which this validation control belongs.
I wrote a simple asp.net code with the asp.net required validator control, the problem is that I only have one submit button called GO, and a dropdownlist that looks for the selection :
clear, submit, cancel.
No matter what option is selected, the required field validation is being fired.
Is there a way to code the page so only when the selected value is Submit it validates?
<asp:TextBox runat="server" id="txtName" />
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtName" errormessage="Please enter your name!" />
<br /><br />
<asp:DropDownList ID="dpAction" runat="server">
<asp:ListItem>Submit</asp:ListItem>
<asp:ListItem Value="Reset">Reset</asp:ListItem>
<asp:ListItem>Cancel</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnAction" runat="server" onclick="btnAction_Click" Text="Go"
Width="40px" />
You will probably have to use a custom validator, and you would need to write the client side code for it also if you want client side validation.
I'm assuming you have a text box or something else that is required when "dpAction" is set to "Submit"?
So for example you would do something like this in your markup
<asp:CustomValidator id="CustomValidator1" runat="server"
OnServerValidate="TextValidate"
ControlToValidate="TextBox1"
ErrorMessage="Text must be specified if Submit is selected">
and in your code-behind
protected void TextValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = dpAction.SelectedValue == "Submit" && !String.IsNullOrEmpty(textbox1.Text);
}
using "required" attribute.
its new in html 5
you should set ValidationGroup for each Validator!
for all controls that you want to be validated and the button to fire the validation:
and the button must have same validaton group with your validator!