The name 'xxx' does not exist in the current context 錯誤解決方法之一

嵌套在內層的asp:datalist或者其他控件是無法直接被code behind調用的,應該現在外層的datalist加事件onitemdatabound事件處理函數,使用方法如下

aspx文件:


	
		
                    <%#DataBinder.Eval(Container.DataItem,"name") %>
                    
   <%#DataBinder.Eval(Container.DataItem,"boardtype") %>
asxp.cs文件:(在事件处理函数中加入内层DataList数据邦定)

protected void Page_Load(object sender, EventArgs e)
    {
        SQL sql = new SQL("select * from bigsorts");
        SqlDataAdapter sda = sql.GetSqlDataAdapter();
        DataSet ds = new DataSet();
        sda.Fill(ds,"table1");
        DataList1.DataSource = ds;
        DataList1.DataBind();
        sql.CloseConn();
        Response.Write(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
    }

    protected void DataList1_DataBinding(object sender, DataListItemEventArgs e)
    {
        DataList dl = (DataList)e.Item.FindControl("DataList2");
        Label l = (Label)e.Item.FindControl("L");
        SQL sql = new SQL("select * from tblsorts where parentid=" + l.Text);
        SqlDataAdapter sda = sql.GetSqlDataAdapter();
        DataSet ds = new DataSet();
        sda.Fill(ds, "table");
        dl.DataSource = ds;
        dl.DataBind();
    } 



你可能感兴趣的:(ASP.NET)