繁体   English   中英

在GridView(ASP.NET/C#)中的DataBinding中设置标签文本

[英]Setting label text in DataBinding in a GridView (ASP.NET/C#)

我需要对GridView中的列内容执行一些字符串操作,并且为此使用DataBinding事件作为模板字段。 我正在将所有Environment.NewLine转换为
用于输出。

这是代码:

protected void Label1_DataBinding(object sender, EventArgs e)
        {
            Label lb = (Label)sender;

            lb.Text.Replace(Environment.NewLine, "<br />");

        }

但这是行不通的。 但有趣的是,如果我将其分配给这样的字符串:

protected void Label1_DataBinding(object sender, EventArgs e)
        {
            Label lb = (Label)sender;

            string outputtest = lb.Text.Replace(Environment.NewLine, "<br />");

            Response.Write(outputtest);

        }

它在顶部写入正确的,新修改的字符串-但是为什么不将其反馈到网格视图?

Replace实际上并没有设置任何值-它仅返回替换字符串。 尝试:

protected void Label1_DataBinding(object sender, EventArgs e)
{
    Label lb = (Label)sender;

    lb.Text = lb.Text.Replace(Environment.NewLine, "<br />");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM