gkrishy
gkrishy

Reputation: 756

Why the Datalist showing only last value with my code?

public void rt_changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
  // Declare l, also give it a default value, in the case that datalist is empty.
   Label l = null;
   foreach (DataListItem li in datalist.Items)
   { 
      l = li.FindControl("nl") as Label;
   }
   Label3.Text = l.Text; // l values is not getting
 }

In this, am getting only last value from my Datalist to Label3. Based on my click am not getting the values in label. What should I change/do in this code ?

Upvotes: 0

Views: 178

Answers (1)

Habib
Habib

Reputation: 223247

The reason you are seeing the last record is that you are not retaining the value of your label in your loop. Better use a StringBuilder.

public void rt_changed(object sender, AjaxControlToolkit.RatingEventArgs e)
{
    StringBuilder sb = new StringBuilder();
    // Declare l, also give it a default value, in the case that datalist is empty.
    Label l = null;
    foreach (DataListItem li in datalist.Items)
    {
        l = li.FindControl("nl") as Label;
        sb.AppendLine(l.Text);
    }
    Label3.Text = sb.ToString(); // l values is not getting
}

Upvotes: 1

Related Questions