Getting Value instead of Text from ListItem in ListBox

Tengo este codigo

 protected void btnPrasaj_Click(object sender, EventArgs e)
{
    List<ListItem> lista = new List<ListItem>();
    string prasanje = null;
    Application.Lock();
    if (Application["prasanja"] == null) // za prvpat se postavuva prasanje
    {
        prasanje = txtNaslov.Text + "\n\n\n" + txtPrasanje.Text;
        lista.Add(new ListItem(prasanje, ddltema.SelectedIndex.ToString()));
        lstProblemPrasanje.DataSource = lista;
        lstProblemPrasanje.DataTextField = "Text";
        lstProblemPrasanje.DataValueField = "Value";
        lstProblemPrasanje.DataBind();
        Application["prasanja"] = lista;
    }
    else
    {
        lista=(List<ListItem>)Application["prasanja"];
        prasanje = txtNaslov.Text + "\n\n\n" + txtPrasanje.Text;
        lista.Add(new ListItem(prasanje, ddltema.SelectedIndex.ToString()));
        lstProblemPrasanje.DataSource = lista;
        lstProblemPrasanje.DataTextField = "Text";
        lstProblemPrasanje.DataValueField = "Value";
        lstProblemPrasanje.DataBind();
        Application["prasanja"] = lista;
    }
    Application.UnLock();
}

And when I click this button in the ListBox I get numbers (which is the value of the item) instead the content of the text field.

preguntado el 31 de julio de 12 a las 10:07

Where you get numbers instead of Text?. Try out removing these lines lstProblemPrasanje.DataTextField = "Text"; lstProblemPrasanje.DataValueField = "Value"; -

2 Respuestas

Since your datasource is List of ListItems, you don't have to specify DataTextField and DataValueField. Just comment out the following lines.

lstProblemPrasanje.DataTextField = "Text";
lstProblemPrasanje.DataValueField = "Value";

Entonces tu código debería ser:

protected void btnPrasaj_Click(object sender, EventArgs e)
{
    List<ListItem> lista = new List<ListItem>();
    string prasanje = null;
    Application.Lock();
    if (Application["prasanja"] == null) // za prvpat se postavuva prasanje
    {
        prasanje = txtNaslov.Text + "\n\n\n" + txtPrasanje.Text;
        lista.Add(new ListItem(prasanje, ddltema.SelectedIndex.ToString()));
        lstProblemPrasanje.DataSource = lista;
        //lstProblemPrasanje.DataTextField = "Text";
        //lstProblemPrasanje.DataValueField = "Value";
        lstProblemPrasanje.DataBind();
        Application["prasanja"] = lista;
    }
    else
    {
        lista=(List<ListItem>)Application["prasanja"];
        prasanje = txtNaslov.Text + "\n\n\n" + txtPrasanje.Text;
        lista.Add(new ListItem(prasanje, ddltema.SelectedIndex.ToString()));
        lstProblemPrasanje.DataSource = lista;
        //lstProblemPrasanje.DataTextField = "Text";
        //lstProblemPrasanje.DataValueField = "Value";
        lstProblemPrasanje.DataBind();
        Application["prasanja"] = lista;
    }
    Application.UnLock();
}

Respondido 31 Jul 12, 10:07

You do not need specifying DataText y DataValue fields explicitly in case of such data source items, also simplify your code by removing duplicate parts in if/else de la siguiente manera:

protected void btnPrasaj_Click(object sender, EventArgs e)
{
    string key = "prasanja";
    List<ListItem> lista = new List<ListItem>();
    string prasanje = null;
    Application.Lock();
    if (Application[key] != null) // za prvpat se postavuva prasanje
    {
        lista = (List<ListItem>)Application[key];
    }

    prasanje = txtNaslov.Text + "\n\n\n" + txtPrasanje.Text;
    lista.Add(new ListItem(prasanje, ddltema.SelectedIndex.ToString()));
    lstProblemPrasanje.DataSource = lista;
    lstProblemPrasanje.DataBind();
    Application[key] = lista;
    Application.UnLock();
}

Respondido 31 Jul 12, 10:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.