如何在asp.net中更改ListView标签的值

时间:2013-07-26 05:42:39

标签: asp.net c#-4.0 listview label

您好我在ListView的ListView1_ItemDataBound ItemDataBound上的ListView中执行此代码 我正在尝试这个代码,但下面没有工作:

int count = 0;
            foreach (ListViewDataItem row in ListView1.Items)
            {
                if (row.ItemType == ListViewItemType.DataItem)
                {
                    Label Label2 = (Label)row.FindControl("Label2");
                    if (Label2.Text == "") continue;
                    count++;
                    if (count == 1)
                    {
                        Label2.Text = "Week One Result";
                    }
                    if (count == 2)
                    {
                        Label2.Text = "Week Two Result";
                    }
                    if (count == 3)
                    {
                        Label2.Text = "Week Three Result";
                    }
                }
            }

我想基于ListView的行数更改Label的值我在ListView1_ItemDataBound中尝试此代码但代码不起作用我没有在哪里做错。请帮我解决这个问题如何在行中更改Label2的值,行1,2或3的每个标签都有不同的值

我该怎么做?请帮帮我。

谢谢

1 个答案:

答案 0 :(得分:0)

更正您的代码。列表视图itemdatabound的示例。

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label lbl = (Label)e.Item.FindControl("Label2");
        // now what ever u want
        int index = e.Item.DataItemIndex;
        if (index == 0)// here 0 stand for row one
        {
            lbl.Text = "Week One Result";
        }
        if (index == 1)
        {
            lbl.Text = "Week Two Result";
        }
        if (index == 2)
        {
            lbl.Text = "Week Three Result";
        }
    }
}