Asp.Net Drop Down List

Asked

Viewed 120 times

1

How do I pass one dropdownlist by parameter? I tried to pass, for example:

public void preencheDrop(string SQL, WebControl x)
{
    método aqui. 
}

but I couldn’t. This method I just want to make my life easier so I can fill more than one drop and not have to spend all the time copying code.

  • Exchange Webcontrol for System.Web.UI.WebControls.Dropdownlist

1 answer

2

See if this really suits you. Just make the appropriate changes.

public bool preencheTipo_Usuario(DropDownList dl)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select ");
            sb.AppendLine("codtipo, descricao_tipo");
            sb.AppendLine("from tbl_tipo_usuario ");

            SqlConnection conexao = new SqlConnection();
            conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conectDarf"].ConnectionString;
            this.cmd = new SqlCommand(sb.ToString(), conexao);

            try
            {
                conexao.Open();
                cmd.ExecuteNonQuery();

                SqlDataReader dr = cmd.ExecuteReader();

                dl.DataSource = dr;
                dl.DataTextField = "descricao_tipo";
                dl.DataValueField = "codtipo";
                dl.DataBind();

                dl.Items.Insert(0, new ListItem("--- SELECIONE ---", "-1"));
            }
            catch (Exception excecao)
            {
                Erro = excecao.Message;
                return false;
            }
            finally
            {
                conexao.Close();

            }

            return true;
        }
  • Opa , I’ll test and I’ll be back to know the result

Browser other questions tagged

You are not signed in. Login or sign up in order to post.