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

1

I have the following dropdownlist where in the first one the person chooses whether he wants rental or sale.

Thequeryinthedatabaseisbeingdoneasfollows:

publicList<Imovel>ObterCasaOperacao(stringcodigoOperacao){try{stringsql="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 in the BLL:

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

And loads the result into 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 do a query for each dropdownlist, the only problem is being to separate this 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 ), to be able to get each item.

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

    
asked by anonymous 22.08.2016 / 19:16

1 answer

1

User

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();
    }
}
    
22.08.2016 / 21:17