ASP.NET C# Dropdownlist doubt

Asked

Viewed 93 times

0

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

Addressee Ausente

In the class that informs this query TipoOcorrencia.cs he just makes a simple query SELECT * FROM OcorrTipoEntr 'cause like I said, I need every load of data that’s on this table.

But I need that ddl enter the occurrence code, followed by the description.Example:

46 - Recipient Absent

My question is, where can I apply this change?

In the jQuery, in the code-behind, in the class? It would just be a concatenation?

  • 1

    Is it Aspnetmvc ??? If you have the code that sends this information to the screen ??? Your database is SQL Server?

1 answer

2


This is a way out for those who use Webforms...

HTML of the Form

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

Class of Occurrences

public class Ocorrencias
{
    public Ocorrencias()
    {

    }

    public String Ocorrencia { get; set; }

    public string CodigoOcorrencia { get; set; }
}

Dropdownlist 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;
        }
    }

Browser other questions tagged

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