Dropdownlist selected item validation - c#

Hello i am trying to validate dropdownlist as it should not have selected item as "--- Pick One ---" by using compare validator but it's not working. Please can any one guide me. It will be very helpful for me.
My code is :-
enter code here
<asp:DropDownList ID="drpdwn_ImDiv" CssClass="inner-dropdown1" runat="server" DataSourceID="SqlDataSource6" DataTextField="Division" DataValueField="Division" AppendDataBoundItems="true">
<asp:ListItem>--- Pick One ---</asp:ListItem>
</asp:DropDownList>
<br />
<asp:CompareValidator ID="CompareValidator1" ControlToValidate="drpdwn_ImDiv" Type="String" ValueToCompare="--- Pick One ---" CssClass="validator" Font-Size="10px" Operator="NotEqual" runat="server" ErrorMessage="CompareValidator"></asp:CompareValidator>`

try this one
<asp:DropDownList ID="drpdwn_ImDiv" CssClass="inner-dropdown1" runat="server" DataSourceID="SqlDataSource6" DataTextField="Division" DataValueField="Division" AppendDataBoundItems="true">
<asp:ListItem>--- Pick One ---</asp:ListItem>
</asp:DropDownList>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please select" ControlToValidate="drpdwn_ImDiv"
InitialValue="--- Pick One ---"></asp:RequiredFieldValidator>

Start by using a Text and Value property for a ListItem. Then you can use a RequiredFieldValidator to check the empty value of the first ListItem.
<asp:DropDownList ID="drpdwn_ImDiv" runat="server">
<asp:ListItem Text="--- Pick One ---" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="drpdwn_ImDiv"
runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

Related

Retain selected file after postback with FileUpload

I have spent hours on this and read countless threads but not sure what i may be missing to get this to work the way i would like.
I would like to select a file to upload and when the user clicks the button to save the record if there are any validation errors to retain the value of file that was selected to upload.
Heres my markup
<asp:MultiView runat="server" ID="RegisterMultiView">
<asp:View ID="DefaultView" runat="server">
<asp:UpdatePanel ID="upCountryBrands" runat="server">
<ContentTemplate>
Select parent
<asp:RequiredFieldValidator
SetFocusOnError="true"
runat="server" ID="RequiredFieldValidator3"
ControlToValidate="ddlParent" InitialValue="0"
ValidationGroup="MainForm" ErrorMessage="Please select"
Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlParent" runat="server"
OnSelectedIndexChanged="ddlParent_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
Select Child
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator2"
ControlToValidate="ddlChild" InitialValue="0"
ValidationGroup="MainForm" ErrorMessage="Please select"
Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlChild" runat="server"
OnSelectedIndexChanged="ddlChild_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
Name
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator1"
ControlToValidate="txtName" ValidationGroup="MainForm" ErrorMessage="Please enter your name" Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:TextBox ID="txtName" runat="server" MaxLength="100" />
Enter description of your issue
<asp:TextBox ID="txtIssue" runat="server" TextMode="MultiLine" /> <%--This is a Tiny MCE control--%>
Image 1
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="rfv1" ControlToValidate="FileUpload1" ValidationGroup="MainForm"
ErrorMessage="Please select an image." Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revFileTypes" runat="server" ErrorMessage="Images only." ControlToValidate="FileUpload1" ValidationExpression=".*\.(gif|jpe?g|png)$"></asp:RegularExpressionValidator>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="false" accept="image/*" ViewStateMode="Enabled" EnableViewState="true" />
<asp:Label ID="lblImageOneStatus" runat="server"></asp:Label>
Image 2
<asp:RequiredFieldValidator SetFocusOnError="true" runat="server" ID="RequiredFieldValidator9"
ControlToValidate="FileUpload2" ValidationGroup="MainForm" ErrorMessage="Images only" Display="Dynamic">Required</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Images only." ControlToValidate="FileUpload2" ValidationExpression=".*\.(gif|jpe?g|png)$"></asp:RegularExpressionValidator>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="false" class="form-control-file" accept="image/*" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="MainForm" OnClick="btnSave_Click" OnClientClick="txtIssue.triggerSave(false,true);" />
</asp:View>
<asp:View ID="SuccessView" runat="server">
Thanks
</asp:View>
</asp:MultiView>
Couple of notes:
I have an updatePanel surrounding the dropdown so it looks like theres no postback.
I have tried surrounding the FileUpload controls with an UpdatePanel but this made no difference.
I have a TonyMCE control, i dont think this would add a spanner into the works but i have left out the associated Javascript so i can provide if required.
I have tried adding triggers but again this made no difference so i can only assume i am doing something wrong.
How could i ensure when a file is selected using the FileUpload controls and then press Save it retains the value of the file?

First item of dropdownlist item non selectable asp.net

I want to make first item of dropdownlist non selectable following is my code.
<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Bug</asp:ListItem>
<asp:ListItem>Enhancement</asp:ListItem>
<asp:ListItem>Tasks</asp:ListItem>
<asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>
Please help me how to do this.
You can also make it disabled in code behind on page load event
protected void Page_Load(object sender, EventArgs e)
{
IssueList.Items[0].Attributes["disabled"] = "disabled";
}
also make your first item selected
<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
<asp:ListItem Selected="true" Value="">Select One</asp:ListItem>
<asp:ListItem>Bug</asp:ListItem>
<asp:ListItem>Enhancement</asp:ListItem>
<asp:ListItem>Tasks</asp:ListItem>
<asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>
You can add disabled attribte.try with this:
<asp:DropDownList runat="server" ID="id">
<asp:ListItem Text="Test" Value="value" disabled="disabled" />
</asp:DropDownList>
You can add an 'Empty' value item:
<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Bug</asp:ListItem>
<asp:ListItem>Enhancement</asp:ListItem>
<asp:ListItem>Tasks</asp:ListItem>
<asp:ListItem>New Feature</asp:ListItem>
</asp:DropDownList>
Then you would check for
if (string.IsNullOrEmpty(IssueList.SelectedValue))
You could use attribute as in combination of disabled and selected. may this will help to get resolve your issue.
<asp:ListItem Selected="True" disabled="disabled">Select One</asp:ListItem>
^^^^^^^ ^^^^^^^

Add validation group to textbox when dropdown change to specific value

I have a form with textbox, radiobutton etc. and would like to have a validationgroup attached to some of the input fields when one dropdown changes to a specific value. Is that possible.
lets say i have this form;
<form>
Name:
<asp:textbox runat="server" id="nametextbox" ValidationGroup="myvalgroup"/>
<asp:RequiredFieldValidator id="nametextboxFieldValidator" runat="server" ErrorMessage="Name?" ValidationGroup="myvalgroup" ControlToValidate="nametextbox" />
Gender: <asp:RadioButtonList runat="server" ID="gender" RepeatDirection="Horizontal" ValidationGroup="myvalgroup">
<asp:ListItem Text="Select gender" Value="" Selected="True"/>
<asp:ListItem Text="Male" Value="Male"/>
<asp:ListItem Text="Female" Value="Female"/>
</asp:RadioButtonList>
<asp:RequiredFieldValidator id="genderFieldValidator" runat="server" ErrorMessage="Gender?" ValidationGroup="myvalgroup" ControlToValidate="gender" />
The answer to life the universe and everything:
<asp:textbox runat="server" id="theanswer" />
Are you finished?
<asp:RadioButtonList runat="server" ID="finished" RepeatDirection="Horizontal" onchange="add-validation-group()" >
<asp:ListItem Text="Are you done?" Value="" Selected="True"/>
<asp:ListItem Text="Yes" Value="Yes"/>
<asp:ListItem Text="No" Value="No"/>
<asp:ListItem Text="Absolutly not, i need 600 more years to think!" Value="NOT-IN-600-YEARS"/>
</asp:RadioButtonList>
<asp:button runat="server" id="submitbutton" />
</form>
Codebehind
function add-validation-group()
if (finished.SelectedValue.ToString() == "Yes")
{
theanswer.Attributes.Add("ValidationGroup", "myvalgroup");
}
end
Ok so my codebehind contains a mixup of everything but just to show what i would like to acomplish, sorry for the codemixing!
So can this be done, add validation to inputfields when onchange on a dropdownlist?
Point me in the right direction, please! :)
Finally i found a solution to my question.
Quite simple i might add.
<script type=text/javascript>
$(document).ready(function () {
$("#<%= finished.ClientID %>").change(function () {
if ($('option:selected', this).text() == 'Yes') {
ValidatorEnable(document.getElementById('<%=theanswerValidator.ClientID%>'), true);
}
});
});
</script>
<form>
Name:
<asp:textbox runat="server" id="nametextbox" ValidationGroup="myvalgroup"/>
<asp:RequiredFieldValidator id="nametextboxFieldValidator" runat="server" ErrorMessage="Name?" ValidationGroup="myvalgroup" ControlToValidate="nametextbox" />
Gender: <asp:RadioButtonList runat="server" ID="gender" RepeatDirection="Horizontal" ValidationGroup="myvalgroup">
<asp:ListItem Text="Select gender" Value="" Selected="True"/>
<asp:ListItem Text="Male" Value="Male"/>
<asp:ListItem Text="Female" Value="Female"/>
</asp:RadioButtonList>
<asp:RequiredFieldValidator id="genderFieldValidator" runat="server" ErrorMessage="Gender?" ValidationGroup="myvalgroup" ControlToValidate="gender" />
The answer to life the universe and everything:
<asp:textbox runat="server" id="theanswer" />
<asp:RequiredFieldValidator id="theanswerValidator" Enabled="false" runat="server" ErrorMessage="Your answer?" ValidationGroup="myvalgroup" ControlToValidate="theanswer" />
Are you finished?
<asp:DropDownList runat="server" ID="finished" >
<asp:ListItem Text="Are you done?" Value="" Selected="True"/>
<asp:ListItem Text="Yes" Value="Yes"/>
<asp:ListItem Text="No" Value="No"/>
<asp:ListItem Text="Absolutly not, i need 600 more years to think!" Value="NOT-IN-600-YEARS"/>
</asp:DropDownList>
<asp:button runat="server" id="submitbutton" />
</form>
I added required field validators to theanswer textbox and added Enabled="false" i then track a onchange event with javascript to see if user selects Yes and if they do enable the requiredfieldvalidator.
In my question i had a Radiobuttonlist for the (are you finished?), But changed that to a DropdownList instead for other reasons but i cant think that this is impossible if you have radiobutton list.
OK, it's not in codebehind but it works.
I did try to do it codebehind but it dident feel user friendly to have a Postback on select change.
If someone is intrested in the codebehind method i did it this way, and it worked but (in my opinion not so user friendly)
On my asp:Dropdownlist i added
OnSelectedIndexChanged="finished_SelectedIndexChanged" AutoPostBack="true"
and in my codebehind i added this:
protected void finished_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
if (ddl.SelectedItem.ToString().Equals("Yes"))
{
theanswerValidator.Enabled = true;
}
else // If someone selects yes and then regret it and selects no then disable validator again
{
theanswerValidator.Enabled = false;
}
}

How to display no text on radiobutton but has a value c#

Anyone know who to display NO text in a radiobutton item but have a value for each?
Here is my radio button list:
<asp:RadioButtonList ID="rdoD4_1" runat="server" RepeatDirection="Horizontal" Width="407px">
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
</asp:RadioButtonList>
Problem is that it displays 0,1,2 etc next to each list item - I want to keep the value but show no text - any ideas?
Try explicitly setting the Value and Text attributes.
<asp:RadioButtonList runat="server">
<asp:ListItem Value="1" Text="" runat="server"></asp:ListItem>
<asp:ListItem Value="2" Text="" runat="server"></asp:ListItem>
</asp:RadioButtonList>

Gridview's update event firing even though data is invalid in controls

I have an commandfield and few templatefields. The templatefields have validators attached to it and they show up proper messages when wrong data is selected. However when I click commandfield no error is shown and the event fires even though data is invalid. Morover, I have also checked Page.IsValid on server and all works out well even though data is in invalid state. This the markup:
<asp:TemplateField HeaderText="Exp. Date">
<ItemTemplate>
<asp:Label ID="lblExpiration" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Expiration")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:DropDownList ID="ddlMM" runat="server" ValidationGroup="vgExpDateGrid">
<asp:ListItem Value="-1">MM</asp:ListItem>
<asp:ListItem Value="1" >01</asp:ListItem>
<asp:ListItem Value="2">02</asp:ListItem>
<asp:ListItem Value="3">03</asp:ListItem>
<asp:ListItem Value="4">04</asp:ListItem>
<asp:ListItem Value="5">05</asp:ListItem>
<asp:ListItem Value="6">06</asp:ListItem>
<asp:ListItem Value="7">07</asp:ListItem>
<asp:ListItem Value="8">08</asp:ListItem>
<asp:ListItem Value="9">09</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="11">11</asp:ListItem>
<asp:ListItem Value="12">12</asp:ListItem>
</asp:DropDownList>
<span class="green"></span> /
<asp:DropDownList ID="ddlYY" runat="server" ValidationGroup="vgExpDateGrid">
</asp:DropDownList>
<span class="green"></span>
<asp:RequiredFieldValidator ID="rfvddlMM" ControlToValidate="ddlMM" Display="Dynamic" InitialValue="-1" runat="server" ValidationGroup="vgExpDateGrid">*</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="rfvddlYY" ControlToValidate="ddlYY" Display="Dynamic" InitialValue="-1" runat="server" ValidationGroup="vgExpDateGrid">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField
UpdateText="Update" CausesValidation="true"
HeaderText="Update" ShowEditButton="true"
EditText="Update <br/>Exp. Date" ButtonType="Link" />
Please do not pay attention to the weird control names and styles. Basically the template field has dropdownlists of year and month. Year's drop down list is populated in rowdatabound event. As you can see validators are attached still commandfield works normally. Can anybody tell me what can be the problem?
At first glance I would say you do not have ValidationGroup="vgExpDateGrid" on asp:CommandField. Change your commandfield to:
<asp:CommandField ValidationGroup="vgExpDateGrid"
UpdateText="Update" CausesValidation="true"
HeaderText="Update" ShowEditButton="true"
EditText="Update <br/>Exp. Date" ButtonType="Link" />
or alternatively remove ValidationGroup from asp:RequiredFieldValidator

Categories

Resources