datalist绑定的数据库表的class_id是整型,但我想在datalist里显示字符串,就是判断,如果绑定的数据等于1,就显示“***” ,如果是2就显示另外的信息

解决方案 »

  1.   

    表达式绑定 <%# data-binding expression %>http://www.cnblogs.com/terryli/archive/2008/03/25/1120482.html
      

  2.   

    你在sql里面做。
    也可以用#Eval绑定值外面套一个函数,
    sql更方便,select 
      classid= case when class_id=1 then '***' 
                    when class_id=2  then 'FFF'
                     else 'RR' end
    from table    
      

  3.   

    1 是可以再Sql语句中处理
    2 在ItemDataBound中处理
      

  4.   


    declare @table table (id int,[value] int)
    insert into @table
    select 1,1 union all
    select 2,2 union all
    select 3,3 union all
    select 4,4select id,case bb.[value] when '1' then '***' when'2' then 'AAA' 
    else [value] end  as [value]
    from 
    ( select id,cast([value] as varchar(20)) as [value] from @table) bb/*
    id          value
    ----------- --------------------
    1           ***
    2           AAA
    3           3
    4           4
    */
      

  5.   


      <asp:DataList ID="DataList1" runat="server">
                <HeaderTemplate>
                    <table>
                        <tr>
                            <td>编号</td>
                            <td>名称</td>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                     <tr>
                            <td><%# GetText(Eval("ID").ToString()) %></td>
                            <td><%# Eval("Name") %></td>
                        </tr>
                </ItemTemplate>
                <FooterTemplate>
                </table>
                </FooterTemplate>
            </asp:DataList>protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataList1.DataSource = GetData();
                    DataList1.DataBind();
                }
            }
            private DataTable GetData()
            {
                //新建表
                DataTable dt = new DataTable();            //定义表结构
                dt.Columns.Add("Id", typeof(System.Int32));
                dt.Columns.Add("Name", typeof(System.String));            //添加新行
                for (int i = 0; i <= 5; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = i;
                    dr[1] = "oec200" + i;
                    dt.Rows.Add(dr);
                }
                return dt;
            }        public string GetText(string id)
            {
                return "编号:" + id;
            }
      

  6.   

    Text='<%# Eval("class_id") == 1 ? "***" : "XXX"%>'