我用gridview输出数据库的列表(c#)。但是碰到<>符号的时候,资料会被截断显示。我想替换成&lt;&gt;,请问在gridview里如何替换?我是新手,请直接给代码,越详细越好,谢谢!

解决方案 »

  1.   

    1,可以在绑定数据的时候替换2,可以先格式化dataset数据源,再绑定
      

  2.   

    <asp:BoundField 加个 HtmlEncode属性设为FALSE试试
      

  3.   

    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            if(e.Row.RowType == DataControlRowType.DataRow)
            {//假设第一列为需要替换的值的
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("<","&lt");
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(">","&gt");
            }
        }
      

  4.   

    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
            //throw new Exception("The method or operation is not implemented."); 
            if(e.Row.RowType == DataControlRowType.DataRow) 
            {//假设第一列为需要替换的值的 
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(" <","&lt"); 
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(">","&gt"); 
            } 
        }
      

  5.   

    手动增加一个自定义模板,在里面用Label或者其他绑定到相应的字段,然后在GridView加载的时候通过后台代码格式化。
      

  6.   


    我使用这个以后,相应列全部变成空白;要是直接赋值的话就会显示abc(e.Row.Cells[0].Text = "abc");请问怎样修改?
      

  7.   

    默认情况下,如果显示内容中有HTML标签,比如<BR>,页面会将标签直接显示出来,如果你要让<BR>换行有效,只要将该字段的HTMLEncode属性设置为false就行了如:
    <asp: BoundField DataField="Title" HeaderTest="Title" HtmlEncode="False">希望对你有用.
      

  8.   


    我使用这个以后,相应列全部变成空白;要是直接赋值的话就会显示abc(e.Row.Cells[0].Text = "abc");请问怎样修改?
      

  9.   

    不好意思,引用错了,#9的方法是不是要先转成template。
      

  10.   


    我使用这个以后,相应列全部变成空白;要是直接赋值的话就会显示abc(e.Row.Cells[0].Text = "abc");请问怎样修改?