Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Iam using dropdownlist in page and setting required field for it..so when I am getting text and binding to dropdownlist the value is showing at select place so if I click on button it is firing validation...
Posted
Comments
Tom Marvolo Riddle 11-Apr-14 2:47am    
Post your code
Shiva nag 11-Apr-14 2:55am    
Aspx Code
<asp:DropDownList ID="ddlMemberType" runat="server" CssClass="product-textbox" Width="420" onchange="changeMember();">
<asp:ListItem Value="Select" Text="Select MemberType" />
<asp:ListItem Value="Family" Text="Family Man" />
<asp:ListItem Value="Employee" Text="Employee" />
<asp:ListItem Value="Student" Text="Student" />

<asp:RequiredFieldValidator ID="rfvMemberType" ErrorMessage="*" Display="Dynamic" ForeColor="Red" ControlToValidate="ddlMemberType" runat="server" />
.CS code
ddlMemberType.SelectedItem.Text = dr["MemberType"].ToString();
Tom Marvolo Riddle 11-Apr-14 3:02am    
Solution posted.Try it
Shiva nag 11-Apr-14 5:17am    
Actully Iam saving data to db in that I have dropdownlist with required filed valiadator
once data saved to db page will be redirected from there I need to click on button to redirect to actual page then there Dropdown value showing value two times I mean value showing in select Place as well as in actual position
Tom Marvolo Riddle 11-Apr-14 5:22am    
Try this;
DropDownList1.Items.Clear();
DropDownList1.Items.Insert(00, "Select");
DropDownList1.Items.Insert(01, yourvalue);
DropDownList1.SelectedIndex = 0;

I hope you are looking for these.try it

1 solution

Try this:

ClientSide:
Javascript:
JavaScript
<script language="javascript" type="text/javascript" >
     function validate() {
         if (document.getElementById('<%=DropDownList1.ClientID %>').value == "Select") {
             alert("Please Select!");
             document.getElementById('<%=DropDownList1.ClientID %>').focus();
             return false;
         }
     }
       
    </script>


Markup:
ASP.NET
<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" onclientclick="return validate()"/>



ServerSide:
In .CS page:
C#
//Bind Dropdown
           DropDownList1.Items.Insert(00, "Select");
           DropDownList1.SelectedIndex = 0;



In button click event:
C#
if (ddl_DropDownList1.SelectedItem.Text != "Select")
{

}
  else
{
  //alert
}
 
Share this answer
 
v3
Comments
Shiva nag 11-Apr-14 5:09am    
Actually it getting in the scenario of edit Iam clicking on edit button which is in another page
load event Iam assiging the value to dropdownlist Even I removed reaquired field validator....
Tom Marvolo Riddle 11-Apr-14 5:11am    
Not clear.Please explain more

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900