根据后台的cs中的flag,动态绑定数据数据.
flag=0;
<span><%#Eval("Name")%></span>flag=1;
<span><%#Eval("Age")%>></span>flag=2;
<span><%#Eval("Sex")%>></span>请教前台页面如何写

解决方案 »

  1.   

    你想绑定到哪里啊?datalist 还是report 还是什么啊?
    后边给你段代码 自己看着改
     public void DBBind()
        {
            SqlConnection myConn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
            myConn.Open();
            string sqlStr = "select * from tb_GoodsInfo";
            SqlDataAdapter da = new SqlDataAdapter(sqlStr, myConn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Goods");
            DataList1.DataSource = ds.Tables["Goods"].DefaultView;
            DataList1.DataKeyField = "GoodsID";
            DataList1.DataBind();
            myConn.Close();    } <asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
                BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both" OnItemCommand="DataList1_ItemCommand" >
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                <ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                <HeaderTemplate >
                  商品的详细信息
                </HeaderTemplate>
                <ItemTemplate >
                 <table >
                  <tr>
                   <td width =110 align =left >商品编号</td>
                   <td width =110 align =right ><%#Eval("GoodsID")%></td>
                  </tr>
                  <tr>
                   <td width =110 align =left>商品名称</td>
                   <td width =110 align =right><%#Eval("GoodsName") %></td>
                  </tr>
                   <tr>
                   <td width =110 align =left>商品价格</td>
                   <td width =110 align =right><%#Eval("GoodsPrice") %></td>
                  </tr>
                   <tr>
                   <td width =150 align =left>商品介绍</td>
                   <td width =110 align =right><%#Eval("GoodsIntroduce")%></td>
                  </tr> 
                     <tr>
                         <td align="left" colspan="2">
                             <asp:LinkButton ID="LinkButton1" runat="server"  Text ="删除" CommandName ="delete">删除</asp:LinkButton></td>
                     </tr>
                 </table>
                
                </ItemTemplate>
            </asp:DataList>
      

  2.   

     protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            { 
                int GoodsID=Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
                string sqlStr = "delete  from tb_GoodsInfo where GoodsID="+GoodsID;
                SqlConnection myConn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
                myConn.Open();
                SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
                myCmd.ExecuteNonQuery();
                myCmd.Dispose();
                myConn.Dispose();
                DBBind();
            }
      

  3.   

    我是要在同一个地方根据不同的falg来决定帮哪个字段.比如
    <td width =110 align =left>商品名称 </td> 
                  <td width =110 align =right> <%#Eval("GoodsName") %> </td> 
    我可能flag不同<%#Eval("GoodsPrice") %>了
    但是还是在一行里面 
      

  4.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {            if (e.Row.Cells[3].Text == "0")
                {                e.Row.Cells[3].Text = "对应值";
                }
                else if(e.Row.Cells[3].Text == "1")
                {                e.Row.Cells[3].Text = "<font color=red>对应的值</font>";
                }else if(e.Row.Cells[3].Text == "2")
                {
                    e.Row.Cells[3].Text = "<font color=red>对应的值</font>";     
                }
            }
          
        }
      

  5.   

    在后台将flag定义成protectedhtml中:
    <%# 
        if(flag==0){Eval("Name")}
        if(flag==1){Eval("Age")}
        if(flag==2){Eval("Sex")}
     %>这样就可以了
      

  6.   


    <span> <%# flag==0?Eval("Name"):Eval("Age") %> </span> 
      

  7.   


    你在后台写个公共方法如:
    public string Flag()
    {
      //这里写你读取到的flag的真实值
        return flag;
    }然后你就可以在前台这样写:<%# Flag()=="0"?Eval("goodsname"):Eval("goodsPrice")%>