日期:2014-05-17  浏览次数:20887 次

求助:关于数据绑定 为什么写在前台就行 写在后台就不行呢?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication2.test" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script  Language="C#" runat="server">
    ArrayList lst = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        lst.Add("爬山");
        lst.Add("打球");
        lst.Add("音乐");
        Page.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form runat=server>
<asp:CheckBoxList ID="CheckBoxList1" DataSource='<%#lst %>' runat="server"/>
</form>
</body>
</html>


以上是把代码都写在前台了 可以实现数据绑定

但是如果数据写在后台,却报错找不到
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="WebApplication2.test" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form runat=server>
<asp:CheckBoxList ID="CheckBoxList1" DataSource='<%#lst %>' runat="server"/>
</form>
</body>
</html>


namespace WebApplication2
{
    public partial class test : System.Web.UI.Page
    {

    ArrayList lst = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        lst.Add("爬山");
        lst.Add("打球");
        lst.Add("音乐");
        Page.DataBind();
    }
       
    }
}


说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 

编译器错误消息: CS0103: 当前上下文中不存在名称“lst”

这是为什么呢
------最佳解决方案--------------------
ArrayList lst = new ArrayList();
=>
public ArrayList lst = new ArrayList();
------其他解决方案--------------------
后台: 
 CheckBoxList1.DataSource = lst;
            CheckBoxList1.DataBind();
------其他解决方案--------------------
多谢:)
以前搞java都是存request里传来传去的
现在有了绑定这种东西 不是大哥指点我还真想不到问题在这里