App_Code and control

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

App_Code and control

 
0
  #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
----------------------------
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14.  
  15.  
  16. /// <summary>
  17. /// Summary description for readblog
  18. /// </summary>
  19. ///
  20.  
  21. 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
----------------------------
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14.  
  15.  
  16. public partial class Blog : System.Web.UI.Page
  17. {
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. if (IsPostBack)
  21. {
  22.  
  23. }
  24. else
  25. {
  26. readblog rb = new readblog("blogs.xml");
  27. }
  28. }
  29. }

THX! <- Noob!
Last edited by peter_budo; Feb 4th, 2008 at 1:21 pm. Reason: Please use code tags even for code snippets
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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

  1. void RebuildBlogPage(DataSet ds)
  2. {
  3. //string prevUser = ""; //Etsitään edellinen user
  4.  
  5. foreach (DataRow dr in ds.Tables[0].Rows)
  6. {
  7.  
  8. //Lisää topic
  9. TableRow tr = new TableRow();
  10. tr.Cells.Add(new TableCell());
  11. tr.Cells[0].CssClass = "h3";
  12. tr.Cells[0].Text = dr[0].ToString();
  13.  
  14. this.BlogTable.Rows.Add(tr);
  15.  
  16. //Lisää date
  17. tr = new TableRow();
  18. tr.Cells.Add(new TableCell());
  19. tr.Cells[0].CssClass = "h5";
  20. tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
  21. dr[3].ToString();
  22. this.BlogTable.Rows.Add(tr);
  23.  
  24. //Lisää BLog
  25. tr = new TableRow();
  26. tr.Cells.Add(new TableCell());
  27. tr.Cells[0].Width = 200;
  28. tr.Cells[0].CssClass = "textblog";
  29. tr.Cells[0].Text = dr[2].ToString();
  30. this.BlogTable.Rows.Add(tr);
  31.  
  32. //lisätään separator
  33. tr = new TableRow();
  34. tr.Cells.Add(new TableCell());
  35. tr.Cells[0].ColumnSpan = 2;
  36.  
  37. this.BlogTable.Rows.Add(tr);
  38. }

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 Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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:

  1. Table RebuildBlogPage(DataSet ds)
  2. {
  3. //string prevUser = ""; //Etsitään edellinen user
  4. Table BlogTable = new Table();
  5.  
  6. foreach (DataRow dr in ds.Tables[0].Rows)
  7. {
  8.  
  9. //Lisää topic
  10. TableRow tr = new TableRow();
  11. tr.Cells.Add(new TableCell());
  12. tr.Cells[0].CssClass = "h3";
  13. tr.Cells[0].Text = dr[0].ToString();
  14.  
  15. BlogTable.Rows.Add(tr);
  16.  
  17. //Lisää date
  18. tr = new TableRow();
  19. tr.Cells.Add(new TableCell());
  20. tr.Cells[0].CssClass = "h5";
  21. tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
  22. dr[3].ToString();
  23. BlogTable.Rows.Add(tr);
  24.  
  25. //Lisää BLog
  26. tr = new TableRow();
  27. tr.Cells.Add(new TableCell());
  28. tr.Cells[0].Width = 200;
  29. tr.Cells[0].CssClass = "textblog";
  30. tr.Cells[0].Text = dr[2].ToString();
  31. BlogTable.Rows.Add(tr);
  32.  
  33. //lisätään separator
  34. tr = new TableRow();
  35. tr.Cells.Add(new TableCell());
  36. tr.Cells[0].ColumnSpan = 2;
  37.  
  38. BlogTable.Rows.Add(tr);
  39. }
  40.  
  41. return BlogTable;
  42. }

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 Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14.  
  15. public partial class Blog : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (IsPostBack)
  20. {
  21. }
  22. else
  23. {
  24. readblog rb = new readblog("blogs.xml");
  25. }
  26. }
  27. protected void WriteBlog1_Click(object sender, EventArgs e)
  28. {
  29. this.Response.Redirect("WriteBlog.aspx");
  30. }
  31. }

and App_Code/readblog.cs

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14.  
  15. public class readblog
  16. {
  17. public readblog()
  18. {
  19. //
  20. // TODO: Add constructor logic here
  21. //
  22. }
  23. public readblog(string xmlfilename)
  24. {
  25. DataSet ds = ReadBlogIntoTable(xmlfilename);
  26.  
  27. if ((bool)HttpContext.Current.Session["Changed"])
  28. {
  29. AppendBlogs(ds, xmlfilename);
  30. WriteXmlBlog(ds, xmlfilename);
  31. HttpContext.Current.Session["Changed"] = false;
  32. }
  33. RebuildBlogPage(ds);
  34. }
  35.  
  36. private DataSet ReadBlogIntoTable(string xmlfilename)
  37. {
  38. DataSet ds = new DataSet();
  39.  
  40. string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
  41.  
  42. if (File.Exists(filename))
  43. {
  44. ds.ReadXml(filename); //Jos on niin luetaan tiedosto datasettiin ja palautetaan se
  45. }
  46. else
  47. {
  48. DataTable dt = new DataTable("Contents");
  49. ds.Tables.Add(dt);
  50.  
  51. dt.Columns.Add("Topic", Type.GetType("System.String"));
  52. dt.Columns.Add("Time", Type.GetType("System.DateTime"));
  53. dt.Columns.Add("Content", Type.GetType("System.String"));
  54. dt.Columns.Add("Writer", Type.GetType("System.String"));
  55. }
  56. return ds;
  57. }
  58.  
  59. void AppendBlogs(DataSet ds, string xmlfilename)
  60. {
  61. DataRow dr = ds.Tables["Contents"].NewRow();
  62.  
  63. dr[0] = HttpContext.Current.Session["Topic"];
  64. dr[1] = DateTime.Now;
  65. dr[2] = HttpContext.Current.Session["Content"];
  66. dr[3] = HttpContext.Current.Session["Writer"];
  67.  
  68. ds.Tables["Contents"].Rows.Add(dr);
  69.  
  70. WriteXmlBlog(ds, xmlfilename);
  71. }
  72.  
  73. private void WriteXmlBlog(DataSet ds, string xmlfilename)
  74. {
  75. string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
  76. ds.WriteXml(filename);
  77. }
  78.  
  79. Table RebuildBlogPage(DataSet ds)
  80. {
  81. Table BTable = new Table();
  82.  
  83. foreach (DataRow dr in ds.Tables[0].Rows)
  84. {
  85. TableRow tr = new TableRow();
  86. tr.Cells.Add(new TableCell());
  87. tr.Cells[0].CssClass = "h3";
  88. tr.Cells[0].Text = dr[0].ToString();
  89.  
  90. BTable.Rows.Add(tr);
  91.  
  92. tr = new TableRow();
  93. tr.Cells.Add(new TableCell());
  94. tr.Cells[0].CssClass = "h5";
  95. tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
  96. dr[3].ToString();
  97. BTable.Rows.Add(tr);
  98.  
  99. tr = new TableRow();
  100. tr.Cells.Add(new TableCell());
  101. tr.Cells[0].Width = 200;
  102. tr.Cells[0].CssClass = "textblog";
  103. tr.Cells[0].Text = dr[2].ToString();
  104. BTable.Rows.Add(tr);
  105.  
  106. tr = new TableRow();
  107. tr.Cells.Add(new TableCell());
  108. tr.Cells[0].ColumnSpan = 2;
  109.  
  110. BTable.Rows.Add(tr);
  111. }
  112. return BTable;
  113. }
  114. }

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 Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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:

  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Web;
  5. using System.Web.UI.WebControls;
  6.  
  7. public class readblog
  8. {
  9. private DataSet _blog;
  10.  
  11. public readblog(string xmlfilename)
  12. {
  13. _blog = ReadBlogIntoTable(xmlfilename);
  14.  
  15. if ((bool)HttpContext.Current.Session["Changed"])
  16. {
  17. AppendBlogs(xmlfilename);
  18. WriteXmlBlog(xmlfilename);
  19. HttpContext.Current.Session["Changed"] = false;
  20. }
  21. }
  22.  
  23. private DataSet ReadBlogIntoTable(string xmlfilename)
  24. {
  25. DataSet ds = new DataSet();
  26.  
  27. string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
  28.  
  29. if (File.Exists(filename))
  30. {
  31. ds.ReadXml(filename); //Jos on niin luetaan tiedosto datasettiin ja palautetaan se
  32. }
  33. else
  34. {
  35. DataTable dt = new DataTable("Contents");
  36.  
  37. dt.Columns.Add("Topic", Type.GetType("System.String"));
  38. dt.Columns.Add("Time", Type.GetType("System.DateTime"));
  39. dt.Columns.Add("Content", Type.GetType("System.String"));
  40. dt.Columns.Add("Writer", Type.GetType("System.String"));
  41.  
  42. ds.Tables.Add(dt);
  43. }
  44.  
  45. return ds;
  46. }
  47.  
  48. void AppendBlogs(string xmlfilename)
  49. {
  50. DataRow dr = _blog.Tables["Contents"].NewRow();
  51.  
  52. dr[0] = HttpContext.Current.Session["Topic"];
  53. dr[1] = DateTime.Now;
  54. dr[2] = HttpContext.Current.Session["Content"];
  55. dr[3] = HttpContext.Current.Session["Writer"];
  56.  
  57. _blog.Tables["Contents"].Rows.Add(dr);
  58.  
  59. WriteXmlBlog(xmlfilename);
  60. }
  61.  
  62. private void WriteXmlBlog(string xmlfilename)
  63. {
  64. string filename = HttpContext.Current.Server.MapPath(".\\App_Data\\") + xmlfilename;
  65. _blog.WriteXml(filename);
  66. }
  67.  
  68. public Table RebuildBlogPage()
  69. {
  70. Table BTable = new Table();
  71.  
  72. foreach (DataRow dr in _blog.Tables[0].Rows)
  73. {
  74. TableRow tr = new TableRow();
  75. tr.Cells.Add(new TableCell());
  76. tr.Cells[0].CssClass = "h3";
  77. tr.Cells[0].Text = dr[0].ToString();
  78.  
  79. BTable.Rows.Add(tr);
  80.  
  81. tr = new TableRow();
  82. tr.Cells.Add(new TableCell());
  83. tr.Cells[0].CssClass = "h5";
  84. tr.Cells[0].Text = Convert.ToString(dr[1].ToString()) + " by " +
  85. dr[3].ToString();
  86. BTable.Rows.Add(tr);
  87.  
  88. tr = new TableRow();
  89. tr.Cells.Add(new TableCell());
  90. tr.Cells[0].Width = 200;
  91. tr.Cells[0].CssClass = "textblog";
  92. tr.Cells[0].Text = dr[2].ToString();
  93. BTable.Rows.Add(tr);
  94.  
  95. tr = new TableRow();
  96. tr.Cells.Add(new TableCell());
  97. tr.Cells[0].ColumnSpan = 2;
  98.  
  99. BTable.Rows.Add(tr);
  100. }
  101.  
  102. return BTable;
  103. }
  104. }

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

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14.  
  15. public partial class Blog : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (IsPostBack)
  20. {
  21. }
  22. else
  23. {
  24. readblog rb = new readblog("blogs.xml");
  25. Page.Controls.Add(rb.RebuildBlogTable());
  26. }
  27. }
  28. protected void WriteBlog1_Click(object sender, EventArgs e)
  29. {
  30. this.Response.Redirect("WriteBlog.aspx");
  31. }
  32. }

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 Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: App_Code and control

 
0
  #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 Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: gt1329a is an unknown quantity at this point 
Solved Threads: 1
gt1329a gt1329a is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: palej is an unknown quantity at this point 
Solved Threads: 0
palej palej is offline Offline
Newbie Poster

Re: App_Code and control

 
0
  #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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum


Views: 5499 | Replies: 8
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC