简体   繁体   中英

dropdown list goes back to first value after selected index changes

I have a drop down list with a button event that should send it's value for a textbox.But,even if I choose a value that is not the first one in the DDL,it only sends the value of the first item in the DDL. I was told to add the,IsPostBack in the page load.but it didn't help.

Codes:

protected void Page_Load(object sender, EventArgs e)
{
    string testeddl;
    codProfessor = Request.QueryString["id"];


    if (db.conecta())
    {
        ddlTeste.Items.Clear();
        ddlTesteAltDel.Items.Clear();
        ddlQuestoes.Items.Clear();

        listaX = db.retornaTestes(codProfessor);

        for (int i = 0; i < listaX.Count; i++)
        {
            testeddl = listaX[i].nometeste;
            ddlTesteAltDel.Items.Add(testeddl);
        }

        protected void btnBuscarTeste_Click(object sender, EventArgs e)
        {
            if (db.conecta())
            {
                int posic = ddlTesteAltDel.SelectedIndex;
                txtNomeTeste.Text = listaX[posic].nometeste;
                ddlaltdelTeste.Text = listaX[posic].materiateste;
            }
        }
    }
}

In Page_Load, just need to indicate:

if(!IsPostBack()
{
   // rest of the code.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM