How to work with more than one dropdownlist to mount a C#URL?

Asked

Viewed 58 times

1

I have the following dropdownlist where in the first person chooses whether to rent or sell immovable.

inserir a descrição da imagem aqui

The consultation in the bank is being done as follows:

public List<Imovel> ObterCasaOperacao(string codigoOperacao)
{
   try
   {
      string sql = "SELECT i.CodImovel, i.Titulo, i.Foto, o.IdOp FROM Imovel i inner join Operacao o on o.IdOp = i.IdOp";
                List<string> parametros = new List<string>();
            if (!string.IsNullOrEmpty(codigoOperacao))
                parametros.Add("o.IdOp = " + codigoOperacao);


            if (parametros.Count > 0)
                sql += " where " + string.Join(" and ", parametros.ToArray());

            var command = new SqlCommand(sql, Conexao.connection);
            Conexao.connection.Open();
            var reader = command.ExecuteReader();
            var imoveis = new List<Imovel>();

            while (reader.Read())
            {
                var imovel = new Imovel();

                imovel.CodImovel = Convert.ToInt32(reader["CodImovel"]);
                imovel.IdOp = Convert.ToInt32(reader["IdOp"]);
                imovel.Titulo = reader["Titulo"].ToString();

                string[] fotos = reader["Foto"].ToString().Split(',');

                if (fotos.Length > 0)
                {
                    imovel.Foto = string.Format("/ImageHandler.ashx?h=300&w=700&code={0}&file={1}", reader["CodImovel"], fotos.First());
                }

                imoveis.Add(imovel);
            }

            return imoveis;
        }

        catch (Exception)
        {
            throw;
        }
        finally
        {
            Conexao.connection.Close();
        }
    }

It passes to this function at BLL:

public List<Imovel> ObterCasaOperacao(string codigoOperacao)
{
    _ImovelDao = new ImovelDao();
    return _ImovelDao.ObterCasaOperacao(codigoOperacao);
}

And load the result on this repeater, on the search result page.

private ImovelBo _imovelBo;

protected void Page_Load(object sender, EventArgs e)
{
    CarregarImoveisNoRepeater();
}
private void CarregarImoveisNoRepeater()
{
    string codigoOperacao = Request.QueryString["codigoOperacao"];

    _imovelBo = new ImovelBo();
    rptResult.DataSource = _imovelBo.ObterCasaOperacao(codigoOperacao);
    rptResult.DataBind();
}

So far so good, I know I have to make a query for each dropdownlist, the problem is only being in separating it in the search button

protected void BntPesquisar_Click(object sender, EventArgs e)
{
    Response.Redirect(string.Format("~/Painel/ResultadoBusca.aspx?codigoOperacao={0}", ddlOperacao.SelectedValue));
} 

I would like to know how I could put a validation,(for example; if else ou switch case ou foreach), so you can pick up each item.

For example: if I selected Operation and nothing else, it only brings the operation if to select city and anything else it brings the city that I selected and if to select several it brings also.

1 answer

1

User Uribuilder

public static String EditUrl()
{
    UrlBuilder URL = new UrlBuilder("~/Painel/ResultadoBusca.aspx");
    if(ddlOperacao.SelectedValue > 0)
    URL.AppendArgument("codigoOperacao", ddlOperacao.SelectedValue);
    return URL.ToString();
}

// Uribuilder

public class UrlBuilder
{
    private String m_ArgPref;
    private StringBuilder m_URL;

    public UrlBuilder(String pageName)
    {
        m_URL = new StringBuilder();
        m_ArgPref = "?";
        m_URL.Append(pageName);
    }

    public void AppendArgument(String name, String value)
    {
        m_URL.Append(m_ArgPref);
        m_URL.Append(name);
        m_URL.Append("=");
        m_URL.Append(HttpUtility.UrlEncode(value));

        m_ArgPref = "&";
    }

    public override String ToString()
    {
        return m_URL.ToString();
    }
}
  • Thanks man, you helped a lot.

Browser other questions tagged

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