Click here to Skip to main content
15,923,845 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
The program below has to select from a drop down list.

I select from the drop which has in addition the element "All'.


However I noticed that it only the "All" element which is passed .

Any other selection is made redundant.


Please peruse the program and correct it.


Thanks







C#
protected void Page_Load(object sender, EventArgs e)
        {

          dfi();

        }

        public void dfi()
        {
            try
            {
                
                {
                   
                    {
                        SqlConnection connect = new SqlConnection();
                        connect.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                        connect.Open();
                        SqlDataAdapter da = new SqlDataAdapter("select INT_NAME from Interest order by Int_name ", connect);

                        DataTable dt = new DataTable();

                        // DataSet ds = new DataSet();
                        da.Fill(dt);
                        ddInterest_Code.DataSource = dt;
                        ddInterest_Code.DataValueField = "INT_NAME";
                        ddInterest_Code.DataTextField = "INT_NAME";
                        ddInterest_Code.DataBind();
                        // connect.Close();

                        //Adding "Please select" option in dropdownlist for validation
                       // ddInterest_Code.Items.Insert(0, new ListItem("      ", "0"));
                        ddInterest_Code.Items.Insert(0, new ListItem("All", "All"));
                    }
                }
            }
            catch
            {

            }

        }

        

        protected void btn_Close_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }

        protected void btn_Create_Click(object sender, EventArgs e)
        {

            ///PROCESS BEGIN
            string str;
            str = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            SqlConnection sqlCon = new SqlConnection(str);


            try
            {
                sqlCon.Open();
                SqlCommand SqlCmd = new SqlCommand("rpt_group_statement", sqlCon);
                SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

                //Create and supply the output parameters

                SqlCmd.Parameters.AddWithValue("@ZGROUP", SqlDbType.VarChar).Value  = ddInterest_Code.Text;
                SqlCmd.Parameters.AddWithValue("@ZPERIOD", SqlDbType.VarChar).Value = ddl_Period.Text;
                SqlCmd.Parameters.AddWithValue("@ZYEAR", SqlDbType.VarChar).Value   = ddl_Year.Text;

              
                SqlCmd.ExecuteNonQuery();

                Session["dfi"] = ddInterest_Code.Text.Trim();

                Response.Redirect("~/statement/rpt_Group_Statement.aspx");
           

            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
            }
            finally
            {

                //*********   ReportViewer1.LocalReport.Refresh();

            }

            ///PROCESS END

        }

        protected void ddInterest_Code_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["dfi"] = ddInterest_Code.Text;
        }
Posted
Updated 27-Dec-14 7:16am
v2

according to ASP.Net Page Life Cycle[^] Page Load method will be excuted on page postback event like button click events etc. when you bind new data again, you will lost previous events, you can simply avoid this by adding validation !IsPostBack like below and it will load data only in the initial page load.
C#
protected void Page_Load(object sender, EventArgs e)
{
  // include IsPostBack check
   if(!IsPostBack)
    {
     dfi();
    }

}
 
Share this answer
 
v2
Comments
King Fisher 27-Dec-14 16:13pm    
5+
DamithSL 27-Dec-14 20:49pm    
Thank you. :)
That is because your DropDownList binds with every Page Load. You need to check for PostBack. If Page is Posting Back, then no need to bind.

So, when you select any option from DropDownList, it goes to Page Load and binds it again. That's the issue.

Now, update the code like below...
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        dfi();
    }
}
 
Share this answer
 
v2
Comments
King Fisher 27-Dec-14 16:12pm    
clear explanation buddy :) 5+
Thanks King 👑 :)

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