Click here to Skip to main content
15,916,379 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using two dropdownlist in my form in. i give If condition in dropdownlist1 for changing the dropdownlist2 values but it is not working ...this is my code


C#
if (DropDownList1.Text == "A")
{
    DropDownList2.Items.Add("1");
    DropDownList2.Items.Add("2");
}
if (DropDownList1.Text == "B")
{
    DropDownList2.Items.Add("3");
    DropDownList2.Items.Add("4");
}


Any one tell how to write the code for this one
Posted
Updated 25-Dec-19 23:01pm
v4
Comments
CodingLover 27-Oct-11 6:21am    
When you are adding items to the other list, is there any other existing items?

try using if condition in DropdownList1.SelectedIndexChanged() and set AutoPostBack=true
C#
if(dropdownlist1.SelectedValue=="A")
{
}

instead of
C#
if(dropdownlist1.Text)
 
Share this answer
 
v4
Comments
Manisha Tambade 27-Oct-11 6:28am    
ya,right answer for question.
member60 31-Oct-11 4:37am    
thank u manisha
Did you set autopostback property of dropdown1 to <code>true or not
 
Share this answer
 
Set AutoPostBack property of DropDownList1 to True.

Also check by changing your code as below.
C#
if (DropDownList1.SelectedItem.Text.Equals("A"))
{
  DropDownList2.Items.Add("1");
  DropDownList2.Items.Add("2");
}

if (DropDownList1.SelectedItem.Text.Equals("B"))
{
  DropDownList2.Items.Add("3");
  DropDownList2.Items.Add("4");
}
 
Share this answer
 
First you have to make the dropdownlist AutoPostback property as true

and the use the
C#
dropdownlist1.SelectedItem.Text or dropdownlist1.SelectedValue
 
Share this answer
 
C#
<pre lang="c#"><pre lang="c#">



For Example This is your Dropdown

 <asp:DropDownList ID="ddl_loginType" runat="server" align="center" AutoPostBack="true">
                <asp:ListItem Value="1" Text="">Student</asp:ListItem>
                <asp:ListItem Value="2" Text="">Teacher</asp:ListItem>
                </asp:DropDownList>

and this is code behind


<pre>if (ddl_loginType.SelectedValue == "1")
            {
                int count = 0;
                count = School.Student.getlogin(tb_username.Text, tb_LgnPassword.Text);
                if (count > 0)
                {
                    Response.Redirect("StudentList.aspx");
                }
                else
                {
                    lb_lgnError.InnerText = ("Incorrect UserName or Password");
                    lb_lgnError.Visible = true;
                }
            }
            else
            {
                int count = 0;
                count = School.Teacher.Teacherslogin(tb_username.Text, tb_LgnPassword.Text);
                if(count>0)
                {
                    Response.Redirect("TeacherInfo.aspx");
                }
                else
                {
                    lb_lgnError.InnerText = ("Incoorect Username Password");
                    lb_lgnError.Visible = true;
                }
            }







i made it by using c# 3 tier architecture please change your code accordingly...
 
Share this answer
 

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