为什么DataList控件只显示我的code最后的值? [英] Why the Datalist showing only last value with my code?

查看:202
本文介绍了为什么DataList控件只显示我的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
 }

在此,我越来越从我Datalist中到LABEL3只有最后一个值。根据我点击我不是在标签得到的值。
在这code,我应该怎么改/办?

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 ?

推荐答案

您所看到的最后一个记录的原因是,你是不是留住标签在循环的价值。更好地使用的StringBuilder

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
}

这篇关于为什么DataList控件只显示我的code最后的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆