I am pretty new to asp.net and would require a very small help.
I have a drop down list as ddlCampus(intention is to get the Campus where the buildings are located). I would like to add new buildings. So i have created a form which has BuildingCode( input from Text Box), Building Description( input from Text Box) and
then Campus(drop down list values automatically populated from table named Campus).I have done required field validator for BldgCode Textbox and for BldgDescription Text box.I would like to add a required field validator for the DropDownlist for Campus(ddlCampus).
Below is what i have done.
ForeColor="Red"InitialValue="Select
a Campus">Required</asp:RequiredFieldValidator>
2. In the Code Behind after i establish the connection to the database
ddlCampus.DataBind()
ddlCampus.Items.Insert("0",
"Select
a Campus")
I also have a submit button and when i click i just want to make sure that there are values in the text box and in the drop down list(other than Select a Campus).But when put data in the text boxes and for the drop down i select some other value, other than
default value, i am still getting the error "Required" as it says the selected value from the drop down list is "Select a Campus". How could i resolve this.
I Changed to the code as said, but still same . When i click the submit button it still identifies that my selected item is "Select a Campus" even though i have selected a different item in the drop down list and it throws out the error "Required"
Whenever you have any push button or image button or any link button on your page they validate the page in asp.net by default. Because they have a property " CausesValidation " set it to true or false according to your requirements this will validate your
controls and force validators to run if it sets to true.
or you can do this thing also group your validations. It will be using this property of controls and validations property's name is ValidationGroup. Assign the same group whom you want to get validated on button's click assign it on button's click too example ValidationGroup="submit".
lijusacc
Member
6 Points
5 Posts
Drop down list with Required Field Validator
Oct 06, 2011 06:47 AM|LINK
HI
I am pretty new to asp.net and would require a very small help.
I have a drop down list as ddlCampus(intention is to get the Campus where the buildings are located). I would like to add new buildings. So i have created a form which has BuildingCode( input from Text Box), Building Description( input from Text Box) and then Campus(drop down list values automatically populated from table named Campus).I have done required field validator for BldgCode Textbox and for BldgDescription Text box.I would like to add a required field validator for the DropDownlist for Campus(ddlCampus). Below is what i have done.
1. IN the aspx page
<asp:DropDownList ID="ddlCampus" runat="server" DataTextField="CampusDescription" DataValueField="Campusid">
</asp:DropDownList>
<asp:SqlDataSource ID="dsCampus" runat="server" ConnectionString="<%$ ConnectionStrings:keydbConnectionString1 %>"
SelectCommand="SELECT * FROM [CampusMaster]"></asp:SqlDataSource>
<asp:RequiredFieldValidator ID="rfv3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ddlCampus" Display="Dynamic "
ForeColor="Red" InitialValue="Select a Campus" >Required</asp:RequiredFieldValidator>
2. In the Code Behind after i establish the connection to the database
ddlCampus.DataBind()
ddlCampus.Items.Insert("0", "Select a Campus")
I also have a submit button and when i click i just want to make sure that there are values in the text box and in the drop down list(other than Select a Campus).But when put data in the text boxes and for the drop down i select some other value, other than default value, i am still getting the error "Required" as it says the selected value from the drop down list is "Select a Campus". How could i resolve this.
thanks
M_3_Sharp
Member
192 Points
46 Posts
Re: Drop down list with Required Field Validator
Oct 06, 2011 06:57 AM|LINK
try to change ddlCampus.Items.Insert("0", "Select a Campus") to this
ddlCampus.Items.Insert(0, (new ListItem("Select a Campus", "0")));
then in your required field validator change the initial value to "0"
hope that works for you...
God bless....
lijusacc
Member
6 Points
5 Posts
Re: Drop down list with Required Field Validator
Oct 06, 2011 07:19 AM|LINK
I Changed to the code as said, but still same . When i click the submit button it still identifies that my selected item is "Select a Campus" even though i have selected a different item in the drop down list and it throws out the error "Required"
hans_v
All-Star
35986 Points
6550 Posts
Re: Drop down list with Required Field Validator
Oct 06, 2011 07:33 AM|LINK
There's no need for any code behind:
<asp:DropDownList ID="ddlCampus" runat="server" DataTextField="CampusDescription" DataValueField="Campusid" AppendDataBoundItems="true"> <asp:ListItem Value="" Text="Select a campus" /> </asp:DropDownList> <asp:SqlDataSource ID="dsCampus" runat="server" ConnectionString="<%$ ConnectionStrings:keydbConnectionString1 %>" SelectCommand="SELECT * FROM [CampusMaster]"></asp:SqlDataSource> <asp:RequiredFieldValidator ID="rfv3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ddlCampus" Display="Dynamic" ForeColor="Red">Required</asp:RequiredFieldValidator>abiruban
All-Star
16002 Points
2731 Posts
Re: Drop down list with Required Field Validator
Oct 06, 2011 07:43 AM|LINK
Hi
pls fllow the steps
ddlCampus.Items.Insert("-1", "Select a Campus") <asp:RequiredFieldValidator ID="rfv3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="ddlCampus" Display="Dynamic " ForeColor="Red" InitialValue="-1" >Required</asp:RequiredFieldValidator>***DON'T FORGET TO CLICK “MARK AS ANSWER” ON THE POST IF HELPED YOU.
mayankpathak...
Contributor
3643 Points
844 Posts
Re: Drop down list with Required Field Validator
Oct 06, 2011 07:52 AM|LINK
Hey there,
Hope this will help you.
Whenever you have any push button or image button or any link button on your page they validate the page in asp.net by default. Because they have a property " CausesValidation " set it to true or false according to your requirements this will validate your controls and force validators to run if it sets to true.
or you can do this thing also group your validations. It will be using this property of controls and validations property's name is ValidationGroup. Assign the same group whom you want to get validated on button's click assign it on button's click too example ValidationGroup="submit".
Hope this will Help you.
Please mark as answer if it helps you
lijusacc
Member
6 Points
5 Posts
Re: Drop down list with Required Field Validator
Oct 09, 2011 05:06 AM|LINK
Hi Hans
Thanks for the reply. that should do it. I still have more queries and will come back.Appreciate all the help.
thanks
Liju