Do i need if IsPostback

Hey,

I have the following code:


protected void Button1_Click(object sender, EventArgs e)
    {
        lbl_dropdowns.Visible = true;

        string connStr = "Database Connection...";
        SqlConnection dbConn = new SqlConnection(connStr);

        dbConn.Open();

        if (DropDownList4.SelectedValue == "Regions")
        {
            string sqlStr = "SELECT * FROM tbl_regions ORDER BY region ASC";

            SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);

            SqlDataReader dbReader = dbCommand.ExecuteReader();

            int i=0;

            while (dbReader.Read())
            {
                lbl_dropdowns.Text += "<div class='dropdownrows"+i+"'>" + dbReader["region"].ToString() + "<span style='color:#018A60'>[<a href='admin-dropdowns.aspx?type=Regions&ID=" + dbReader["region_id"].ToString() + "' style='color:#018A60'>delete</a>]</div><br/>";
            	i++;
			    i = i % 2;
            }
        }
        else if (DropDownList4.SelectedValue == "Nurseries")
        {
            string sqlStr = "SELECT * FROM tbl_locations ORDER BY name ASC";

            SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);

            SqlDataReader dbReader = dbCommand.ExecuteReader();

            int i = 0;

            while (dbReader.Read())
            {
                lbl_dropdowns.Text += "<div class='dropdownrows" + i + "'>" + dbReader["name"].ToString() + "<span style='color:#018A60'>[<a href='admin-dropdowns.aspx?type=Nurseries&ID=" + dbReader["location_id"].ToString() + "' style='color:#018A60'>delete</a>]</div><br/>";
                i++;
                i = i % 2;
            }
        }
        dbConn.Close();
    }

The users selects a value from the Dropdown and it brings up result from 2 different tables. Only problem is that when i click the button to show the first set of results and then select a different item from the dropdown and hit submit, the previous results remain and the new results appear underneath.

I need to have it so when i click the button any previous results are cleared and new results are displayed…

Can i do this? If so, how?

This problem can be removed by using only a simple if condition,just clears the list at postback time. just embed following statements at page_load event. now your code become like following.

protected void page_load()
{
if(!ispostback)
{
DropDownList4.clear();
}
}

protected void Button1_Click(object sender, EventArgs e)

{

    lbl_dropdowns.Visible = true;



    string connStr = "Database Connection...";

    SqlConnection dbConn = new SqlConnection(connStr);



    dbConn.Open();



    if (DropDownList4.SelectedValue == "Regions")

    {

        string sqlStr = "SELECT * FROM tbl_regions ORDER BY region ASC";



        SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);



        SqlDataReader dbReader = dbCommand.ExecuteReader();



        int i=0;



        while (dbReader.Read())

        {

            lbl_dropdowns.Text += "&lt;div class='dropdownrows"+i+"'&gt;" + dbReader["region"].ToString() + "&lt;span style='color:#018A60'&gt;[&lt;a href='admin-dropdowns.aspx?type=Regions&ID=" + dbReader["region_id"].ToString() + "' style='color:#018A60'&gt;delete&lt;/a&gt;]&lt;/div&gt;&lt;br/&gt;";

            i++;

            i = i &#37; 2;

        }

    }

    else if (DropDownList4.SelectedValue == "Nurseries")

    {

        string sqlStr = "SELECT * FROM tbl_locations ORDER BY name ASC";



        SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);



        SqlDataReader dbReader = dbCommand.ExecuteReader();



        int i = 0;



        while (dbReader.Read())

        {

            lbl_dropdowns.Text += "&lt;div class='dropdownrows" + i + "'&gt;" +  dbReader["name"].ToString() + "&lt;span style='color:#018A60'&gt;[&lt;a href='admin-dropdowns.aspx?type=Nurseries&ID=" + dbReader["location_id"].ToString() + "' style='color:#018A60'&gt;delete&lt;/a&gt;]&lt;/div&gt;&lt;br/&gt;";

            i++;

            i = i % 2;

        }

    }

    dbConn.Close();

}

I think you need the statement if(Page.IsPostBack)

Ok i worked it out.

I was using a label to display the data, so i set EnableViewState to false and it now works :slight_smile:

Thanks