Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi there

i have tree dropdownlist in one page also (ok) button click event.
all dropdown data comes from database.

1 dropdown for standard
2 dropdown for subject
3 dropdown for chapter

when i click on ok button then 3rd dropdown always automatically select first value from list.
i also have fileupload control in same page.


Thanks.


C#
//on page load event
if (!Page.IsPostBack)
       {
           FillClass();
       }

public void FillClass()
    {
        cn.Close();
        cn.Open();
        string com = "Select * from Class";
        SqlDataAdapter adpt = new SqlDataAdapter(com, cn);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "classname";
        DropDownList1.DataValueField = "srno";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
        cn.Close();
    }

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {       
            cn.Close();
            cn.Open();
            string com = "Select * from SubjectName where std='" + DropDownList1.SelectedItem.Text + "'";
            SqlDataAdapter adpt = new SqlDataAdapter(com, cn);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList2.DataSource = dt;
            DropDownList2.DataTextField = "subject";
            DropDownList2.DataValueField = "srno";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
            cn.Close();        
    }
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

       
            cn.Close();
            cn.Open();
            string com = "Select * from Chapter where std='" + DropDownList1.SelectedItem.Text + "' and subject='" + DropDownList2.SelectedItem.Text + "'";
            SqlDataAdapter adpt = new SqlDataAdapter(com, cn);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            DropDownList3.DataSource = dt;
            DropDownList3.DataTextField = "chapter";
            DropDownList3.DataValueField = "srno";
            DropDownList3.DataBind();
            DropDownList3.Items.Insert(0, new ListItem("--Select--", "0"));
            cn.Close();
        }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DropDownList3.Text;       
    }



ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/AdminMasterPage.master" AutoEventWireup="true" CodeFile="papergenr.aspx.cs" Inherits="admin_papergenr" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">        
    </asp:DropDownList>
    

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
    </asp:DropDownList>


    <asp:DropDownList ID="DropDownList3" runat="server">
    </asp:DropDownList>



    <br />
   
 
 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

</asp:Content>
Posted
Updated 25-Jun-15 0:46am
v8
Comments
sasanka sekhar panda 25-Jun-15 0:39am    
Please post the code inside the button click event...
Member 11422010 25-Jun-15 4:29am    
posted.
please try.
sasanka sekhar panda 25-Jun-15 4:38am    
page load event is their any more lines of code that u did not post..
Member 11422010 25-Jun-15 4:51am    
no only one line.
Member 11422010 25-Jun-15 4:58am    
if you dont have any problem then. i can give teamviewer access.

1 solution

Make sure that-
1. If you have written the logic to bind dropdownlist list in the page load event then you must write the check for post back. Following code block may help
C#
if(!Page.IsPostback)
{
   // logic for binding dropdownlist 
}

2. Make sure that you haven't written any logic for binding dropdownlist in the OK button click event unless it is really required.

If this doesn't solve you problem, please update your question with the respective code so that actual solution can be suggested.

Hope, it helps :)
 
Share this answer
 
v2
Comments
Member 11422010 24-Jun-15 23:47pm    
solution not work.
i have updated question with code.
Suvendu Shekhar Giri 25-Jun-15 1:03am    
Code for Button click event is also required to give the correct solution. Please update your answer.
Member 11422010 25-Jun-15 1:14am    
updated

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