首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于数据绑定 为什么写在前台就行 写在后台就不行呢

2012-12-15 
求助:关于数据绑定 为什么写在前台就行 写在后台就不行呢?%@ Page LanguageC# AutoEventWireuptrue

求助:关于数据绑定 为什么写在前台就行 写在后台就不行呢?

<%@ 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里传来传去的
现在有了绑定这种东西 不是大哥指点我还真想不到问题在这里

热点排行