ASP.NET C # DropDownList doubt

0

I have a module that generates reports in excel , one of these reports being the module called Occurrences. Where by DropDownList it informs me all the records of the OcorrTipoEntr table, which has the columns OctCodigo and OctDesc but in this ddl it just brings me the description of the occurrence:

  

Away Recipient

In the class that informs this query TipoOcorrencia.cs it just makes a simple query SELECT * FROM OcorrTipoEntr because as I said, I need every data load that exists in that table.

However, I need to specify the instance code followed by the description in this ddl . Example:

  

46 - Absent Recipient

My question is, where can I apply this change?

No jQuery , no code-behind , in class? Is it just a concatenation?

    
asked by anonymous 06.06.2017 / 02:15

1 answer

2

This is an output for anyone using WebForms ...

Form HTML

<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="ddlOcorrencias" runat="server" OnDataBound="ddlOcorrencias_DataBound"></asp:DropDownList>
</div>
</form>

Occorrences Class

public class Ocorrencias
{
    public Ocorrencias()
    {

    }

    public String Ocorrencia { get; set; }

    public string CodigoOcorrencia { get; set; }
}

DropDownList's Page_Load to Populate it

    protected void Page_Load(object sender, EventArgs e)
    {
        List<Ocorrencias> Ocorr = new List<Ocorrencias>();

        Ocorrencias Oc1 = new Ocorrencias();
        Oc1.CodigoOcorrencia = "45";
        Oc1.Ocorrencia = "Alguma Ocorrencia Ocorreu !";

        Ocorrencias Oc2 = new Ocorrencias();
        Oc2.CodigoOcorrencia = "46";
        Oc2.Ocorrencia = "Destinatário Ausente";

        Ocorr.Add(Oc1);
        Ocorr.Add(Oc2);

        ddlOcorrencias.DataValueField = "CodigoOcorrencia";
        ddlOcorrencias.DataTextField = "Ocorrencia";
        ddlOcorrencias.DataSource = Ocorr;
        ddlOcorrencias.DataBind();
    }

DropDownList DataBound Concatenation

    protected void ddlOcorrencias_DataBound(object sender, EventArgs e)
    {
        foreach (ListItem li in ddlOcorrencias.Items)
        {
            li.Text = li.Value + " - " + li.Text;
        }
    }
    
06.06.2017 / 21:09