Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a dropdownlist in my Application.
What i want is each time when i click on the Reset button it should set to default value(select).

Here is my code for reset.
C#
public void ResetControl()
        {
            DDLContainer.SelectedIndex = 0;
            DDLCountryLoading.SelectedIndex = 0;
            DDLCountryDestination.SelectedIndex = 0;
            //DDLContainer.Items.Insert(0, new ListItem(" --SELECT--", "0"));
           ImageMode.ImageUrl = "~/ShowImageMode.ashx?Mode=A";
        }



Here is my dropdownlist code:
void ddlCountry()
    {
        con.Open();
        cmd = new SqlCommand("select Distinct(mCRY_VCName) from TB_TransCountry", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DDLCountryLoading.DataSource = ds;
        DDLCountryLoading.DataSourceID = String.Empty;
        DDLCountryLoading.DataTextField = "mCRY_VCName";
        DDLCountryLoading.DataValueField = "mCRY_VCName";
        DDLCountryLoading.DataBind();

        DDLCountryLoading.Items.Insert(0, new ListItem(" --SELECT--", "0"));

        DDLCountryDestination.DataSource = ds;
        DDLCountryDestination.DataSourceID = String.Empty;
        DDLCountryDestination.DataTextField = "mCRY_VCName";
        DDLCountryDestination.DataValueField = "mCRY_VCName";
        DDLCountryDestination.DataBind();

        DDLCountryDestination.Items.Insert(0, new ListItem(" --SELECT--", "0"));
        con.Close();


    }
    void ddlContainer()
    {
        con.Open();
        cmd = new SqlCommand("select Distinct(mCNT_VCTypeName) from TB_TransContainerType", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DDLContainer.DataSource = ds;
        DDLContainer.DataSourceID = String.Empty;
        DDLContainer.DataTextField = "mCNT_VCTypeName";
        DDLContainer.DataValueField = "mCNT_VCTypeName";
        DDLContainer.DataBind();

        DDLContainer.Items.Insert(0, new ListItem(" --SELECT--", "0"));


        con.Close();


    }


Can any one please help me to achieve this.
Posted

Try this:
C#
public void ResetControl()
        {
  DDLContainer.Items.Insert(00, "Select");
  DDLCountryLoading.Items.Insert(00, "Select");
  DDLCountryDestination.Items.Insert(00, "Select");

            DDLContainer.SelectedIndex = 0;
            DDLCountryLoading.SelectedIndex = 0;
            DDLCountryDestination.SelectedIndex = 0;
           ImageMode.ImageUrl = "~/ShowImageMode.ashx?Mode=A";
        }
 
Share this answer
 
v2
Comments
[no name] 18-Mar-14 9:29am    
I tried this but It is not working.
Tom Marvolo Riddle 19-Mar-14 0:28am    
place this code inside reset button click event and make sure the inserted values are not clear.
Put breakpoint and check it
[no name] 19-Mar-14 1:32am    
No its not firing instead of that it keeps on inserting Select each time when the button is clicked.
Tom Marvolo Riddle 19-Mar-14 1:54am    
which one is not firing?
it keeps on inserting Select each time when the button is clicked

-Try this:


DDLContainer.Items.Clear();
DDLCountryLoading.Items.Clear();
DDLCountryDestination.Items.Clear();

DDLContainer.Items.Insert(00, "Select");
DDLCountryLoading.Items.Insert(00, "Select");
DDLCountryDestination.Items.Insert(00, "Select");

DDLContainer.SelectedIndex = 0;
DDLCountryLoading.SelectedIndex = 0;
DDLCountryDestination.SelectedIndex = 0;
ImageMode.ImageUrl = "~/ShowImageMode.ashx?Mode=A";
[no name] 19-Mar-14 1:59am    
It is not setting to Default value(select0 instead of that it keep on adding select to the list on button click.

For Eg:

in my DDlist 1st time i have the values like select,aa,bb and by default it showing select as selected item and think that i have changed the value to aa.so if i click the button it should reset (i.e) it should display the value select. But it is not doing as i want instead of that it showing the last selected item(aa) as default and it inserted select additionally.so after button click i have the values as select,aa,bb,select.
Hi
I just Called the DDL functions.It works as what i want.

Here is my working code

C#
public void ResetControl()
        {
            DDLContainer.Items.Clear();
            DDLCountryLoading.Items.Clear();
            DDLCountryDestination.Items.Clear();
            mobjGenlib.PopulateCountry(DDLCountryDestination);
            mobjGenlib.PopulateCountry(DDLCountryLoading);
            mobjGenlib.PopulateContainerType(DDLContainer);
           ImageMode.ImageUrl = "~/ShowImageMode.ashx?Mode=A";
        }


Thanks for all Who helped me.
 
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