User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 426,312 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,307 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 2314 | Replies: 8 | Solved
Reply
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

App_Code and control

  #1  
Feb 4th, 2008
Hey,

I have a this readblog.cs file in projects App_Code folder, and at main folder I have Blog.aspx -> Blog.aspx.cs file.

And I have a Table control in Blog.aspx file, called Bloglist, and I am trying to modify it from App_Code/readblog.cs class file and I dont know how to use Using statements or something like that to get reference so I could use Bloglist at App_Code folder? . here's a snippet from both of files->


----------------------------
Down under App_Code/readblog.cs
----------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;


/// <summary>
/// Summary description for readblog
/// </summary>
/// 

public class readblog
and somewhere down here is that this.Bloglist item, and it doesnt find it (of course, cause theres not a reference to the Blog.aspx.cs file, and I dont know how to do it? I have tryed namespaces and etc, maeby im just confused cause its in the App_Code folder
----------------------------
Down under blog.aspx.cs
----------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;


public partial class Blog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{

}
else
{
readblog rb = new readblog("blogs.xml");
}
}
}

THX! <- Noob!
Last edited by peter_budo : Feb 4th, 2008 at 12:21 pm. Reason: Please use code tags even for code snippets
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Atlanta
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

  #2  
Feb 4th, 2008
When using a website project, you don't need to worry about namespaces. Anything in App_Code is automatically available in the pages in the rest of the site (for better or worse).

The code you posted should work (assuming readblog has a constructor that accepts a string parameter). Are you getting a compilation error?
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

Re: App_Code and control

  #3  
Feb 4th, 2008
Originally Posted by gt1329a View Post
When using a website project, you don't need to worry about namespaces. Anything in App_Code is automatically available in the pages in the rest of the site (for better or worse).

The code you posted should work (assuming readblog has a constructor that accepts a string parameter). Are you getting a compilation error?


Yeah it works that way, no problem in there, but other way around.

I have this code in App_Code/readblog.cs file

    void RebuildBlogPage(DataSet ds)
    {
        //string prevUser = ""; //Etsitään edellinen user

        foreach (DataRow dr in ds.Tables[0].Rows)
        {            
            
            //Lisää topic
            TableRow tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h3";
            tr.Cells[0].Text = dr[0].ToString();
            
            this.BlogTable.Rows.Add(tr);            

            //Lisää date
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h5";
            tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
                dr[3].ToString();
            this.BlogTable.Rows.Add(tr);          

            //Lisää BLog
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].Width = 200;
            tr.Cells[0].CssClass = "textblog";
            tr.Cells[0].Text = dr[2].ToString();
            this.BlogTable.Rows.Add(tr);

            //lisätään separator
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].ColumnSpan = 2;

            this.BlogTable.Rows.Add(tr);             
        }

Error ->
'readblog' does not contain a definition for 'BlogTable' and no extension method 'BlogTable' accepting a first argument of type 'readblog' could be found (are you missing a using directive or an assembly reference?)

And that BlogTable is at ~/Blog.aspx file?
Reply With Quote  
Join Date: Feb 2008
Location: Atlanta
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

  #4  
Feb 4th, 2008
I'm assuming that's some code that's been moved from an ASPX page's code behind into its own class? It sounds like BlogTable was a control on that page.

If so, what you probably need to do is have RebuildBlogPage return a Table. Something like:

    Table RebuildBlogPage(DataSet ds)
    {
        //string prevUser = ""; //Etsitään edellinen user
        Table BlogTable = new Table();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {            
            
            //Lisää topic
            TableRow tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h3";
            tr.Cells[0].Text = dr[0].ToString();
            
            BlogTable.Rows.Add(tr);            

            //Lisää date
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h5";
            tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
                dr[3].ToString();
            BlogTable.Rows.Add(tr);          

            //Lisää BLog
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].Width = 200;
            tr.Cells[0].CssClass = "textblog";
            tr.Cells[0].Text = dr[2].ToString();
            BlogTable.Rows.Add(tr);

            //lisätään separator
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].ColumnSpan = 2;

            BlogTable.Rows.Add(tr);             
        }

        return BlogTable;
    }

Then, in your ASPX page, you would add the return value of that method to the controls collection of your page, placeholder, panel, or whatever you're using for layout.
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

Re: App_Code and control

  #5  
Feb 4th, 2008
Now we are getting to point. But my knowledge is not enough yet, ill past both codes here

Blog.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

    public partial class Blog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
            else
            {
                readblog rb = new readblog("blogs.xml");                
            }
        }
        protected void WriteBlog1_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("WriteBlog.aspx");
        }
    }

and App_Code/readblog.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public class readblog
{
	public readblog()
	{
		//
		// TODO: Add constructor logic here
		//                
	}
    public readblog(string xmlfilename)
    {        
            DataSet ds = ReadBlogIntoTable(xmlfilename);

            if ((bool)HttpContext.Current.Session["Changed"])
            {
                AppendBlogs(ds, xmlfilename);
                WriteXmlBlog(ds, xmlfilename);
                HttpContext.Current.Session["Changed"] = false; 
            }
            RebuildBlogPage(ds);
    }

    private DataSet ReadBlogIntoTable(string xmlfilename)
    {
        DataSet ds = new DataSet();

        string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;

        if (File.Exists(filename))
        {
            ds.ReadXml(filename); //Jos on niin luetaan tiedosto datasettiin ja palautetaan se
        }
        else
        {
                DataTable dt = new DataTable("Contents");
                ds.Tables.Add(dt);

                dt.Columns.Add("Topic", Type.GetType("System.String"));
                dt.Columns.Add("Time", Type.GetType("System.DateTime"));
                dt.Columns.Add("Content", Type.GetType("System.String"));
                dt.Columns.Add("Writer", Type.GetType("System.String"));
        }
        return ds;
    }

    void AppendBlogs(DataSet ds, string xmlfilename)
    {
        DataRow dr = ds.Tables["Contents"].NewRow();

        dr[0] = HttpContext.Current.Session["Topic"];
        dr[1] = DateTime.Now;
        dr[2] = HttpContext.Current.Session["Content"];
        dr[3] = HttpContext.Current.Session["Writer"];

        ds.Tables["Contents"].Rows.Add(dr);

        WriteXmlBlog(ds, xmlfilename);
    }

    private void WriteXmlBlog(DataSet ds, string xmlfilename)
    {
        string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
        ds.WriteXml(filename);
    }

    Table RebuildBlogPage(DataSet ds)
    {        
        Table BTable = new Table();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {                       
            TableRow tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h3";
            tr.Cells[0].Text = dr[0].ToString();

            BTable.Rows.Add(tr);            
            
            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].CssClass = "h5";
            tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
                dr[3].ToString();
            BTable.Rows.Add(tr);          

            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].Width = 200;
            tr.Cells[0].CssClass = "textblog";
            tr.Cells[0].Text = dr[2].ToString();
            BTable.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(new TableCell());
            tr.Cells[0].ColumnSpan = 2;

            BTable.Rows.Add(tr);
        }
        return BTable;
    }
}

Now it's mixed with that return table. Everything else is original, i tried somethings. Now I have to return that table over to Blog.cs somehow....? Thx allready, it would be nice if i could figure it out on my own, but just cant
Reply With Quote  
Join Date: Feb 2008
Location: Atlanta
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

  #6  
Feb 4th, 2008
Okay, with that in mind, things are going to be a little different.

Doing something like this for your readblog class would be a step in the right direction:

using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

public class readblog
{
  private DataSet _blog;

  public readblog(string xmlfilename)
  {
    _blog = ReadBlogIntoTable(xmlfilename);

    if ((bool)HttpContext.Current.Session["Changed"])
    {
      AppendBlogs(xmlfilename);
      WriteXmlBlog(xmlfilename);
      HttpContext.Current.Session["Changed"] = false;
    }
  }

  private DataSet ReadBlogIntoTable(string xmlfilename)
  {
    DataSet ds = new DataSet();

    string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;

    if (File.Exists(filename))
    {
      ds.ReadXml(filename); //Jos on niin luetaan tiedosto datasettiin ja palautetaan se
    }
    else
    {
      DataTable dt = new DataTable("Contents");

      dt.Columns.Add("Topic", Type.GetType("System.String"));
      dt.Columns.Add("Time", Type.GetType("System.DateTime"));
      dt.Columns.Add("Content", Type.GetType("System.String"));
      dt.Columns.Add("Writer", Type.GetType("System.String"));

      ds.Tables.Add(dt);
    }

    return ds;
  }

  void AppendBlogs(string xmlfilename)
  {
    DataRow dr = _blog.Tables["Contents"].NewRow();

    dr[0] = HttpContext.Current.Session["Topic"];
    dr[1] = DateTime.Now;
    dr[2] = HttpContext.Current.Session["Content"];
    dr[3] = HttpContext.Current.Session["Writer"];

    _blog.Tables["Contents"].Rows.Add(dr);

    WriteXmlBlog(xmlfilename);
  }

  private void WriteXmlBlog(string xmlfilename)
  {
    string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
    _blog.WriteXml(filename);
  }

  public Table RebuildBlogPage()
  {
    Table BTable = new Table();

    foreach (DataRow dr in _blog.Tables[0].Rows)
    {
      TableRow tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].CssClass = "h3";
      tr.Cells[0].Text = dr[0].ToString();

      BTable.Rows.Add(tr);

      tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].CssClass = "h5";
      tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
          dr[3].ToString();
      BTable.Rows.Add(tr);

      tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].Width = 200;
      tr.Cells[0].CssClass = "textblog";
      tr.Cells[0].Text = dr[2].ToString();
      BTable.Rows.Add(tr);

      tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells[0].ColumnSpan = 2;

      BTable.Rows.Add(tr);
    }

    return BTable;
  }
}

Then, in Blog.aspx.cs, you could add that table to the page like this:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

    public partial class Blog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
            else
            {
                readblog rb = new readblog("blogs.xml");    
                Page.Controls.Add(rb.RebuildBlogTable());            
            }
        }
        protected void WriteBlog1_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("WriteBlog.aspx");
        }
    }

Before going too far, it might not hurt for you to take a look at some tutorials on object oriented design and programming. That's what's at the heart of this. Getting off to the right start with it will make things go much more smoothly.
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: App_Code and control

  #7  
Feb 4th, 2008
Hey buddy, Blogtable was a table runat the server. It could not find the table, so therefore it is not referenced. Create a server controlled table on the page with the ID of "BlogTable".
Reply With Quote  
Join Date: Feb 2008
Location: Atlanta
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

  #8  
Feb 4th, 2008
That wouldn't work for what he's doing, because the readblog.cs class trying to reference BlogTable isn't in the scope of the Page (though, it looks like it probably was at one point).
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

Re: App_Code and control

  #9  
Feb 5th, 2008
Thx mates. I know and I am reading couple of books now from wrox. Thx, have to try that at home at learn whats happening in there
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 10:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC