QPR
asked on
Moving my code behind to a class file
I have a few pages that build an identical menu to show on the page.
It occured to me that my code behind pages were repating the same code and that I should move the block into my class file and call it from there.
It works by gathering some data and then putting it in the InnerHTML of some named <span> areas.
The data returned and the span IDs are identical acroos all pages.
My problem is that once the code is moved from behind the pages spanName.InnerHTML is no longer recognised.
How can I say that the span I'm referring to is in the page that called this method?
Part 2: This menu is static 95% of the time. Is there a way I could cache this data and then have the menu built from this on subsequent pages rather than keep going back to the server (data comes from sharepoint via web services).
Code in code window
It occured to me that my code behind pages were repating the same code and that I should move the block into my class file and call it from there.
It works by gathering some data and then putting it in the InnerHTML of some named <span> areas.
The data returned and the span IDs are identical acroos all pages.
My problem is that once the code is moved from behind the pages spanName.InnerHTML is no longer recognised.
How can I say that the span I'm referring to is in the page that called this method?
Part 2: This menu is static 95% of the time. Is there a way I could cache this data and then have the menu built from this on subsequent pages rather than keep going back to the server (data comes from sharepoint via web services).
Code in code window
/* This is the code that I moved from the .aspx.cs to the .cs file */
public string BuildMenu(object sender, System.EventArgs e)
{
string MenuTitles = "";
StringBuilder sb = new StringBuilder(MenuTitles);
string theTopics = "";
theTopics = MakeMenu("Topics", CAML query removed for readability);
sb.Append(theTopics);
TopicMenu.InnerHtml = sb.ToString();
sb.Clear();
string theIssues = "";
theIssues = MakeMenu("Issues", CAML query removed for readability);
sb.Append(theIssues);
IssueMenu.InnerHtml = sb.ToString();
sb.Clear();
string theObjs = "";
theObjs = MakeMenu("Objectives", CAML query removed for readability);
sb.Append(theObjs);
ObjectivesMenu.InnerHtml = sb.ToString();
sb.Clear();
string theZones = "";
theZones = MakeMenu("Zones", CAML query removed for readability);
sb.Append(theZones);
ZonesMenu.InnerHtml = sb.ToString();
sb.Clear();
sb = null;
}
/* This is how the code is called from within the aspx.cs file on page load */
ExtractListData etl = new ExtractListData();
etl.BuildMenu();
ASKER
so a class file cannot refer to a page control on the page that called the code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
getting there.....
I have <span>s in my page and I want to populate them.
I am using....
Page page = (Page)HttpContext.Current. Handler;
Control spanTopic = (Control)page.FindControl( "TopicMenu ");
but I don't seem to be able to set the innerHTML of the control now.
spanTopic.innerHtml = sb.ToString();
But I get the error that system.web.ui.control does not contain a definition for innerhtml
I have <span>s in my page and I want to populate them.
I am using....
Page page = (Page)HttpContext.Current.
Control spanTopic = (Control)page.FindControl(
but I don't seem to be able to set the innerHTML of the control now.
spanTopic.innerHtml = sb.ToString();
But I get the error that system.web.ui.control does not contain a definition for innerhtml
Hi,
I am really sorry for sending so much links. Actually my network speed was slow and so this caused the problem. And so I continue sending links (as I was guessing it has been not send).
However here is the solution to your problem:
(1) Following code declared in .aspx page:
<span id="test" runat="Server">Hello Sister!</span>
(2) Following code in .cs (class file):
The HtmlGenericControl creates a server-side control for HTML tags that do not have an HTML control associated with them. Perfect examples of this are the <span> and <div> tags.
HtmlGenericControl control=(HtmlGenericContro l)page.Fin dControl(" test");
if (control != null)
{
control.InnerHtml = "Hello Brother!";
}
Regards,
VSS
I am really sorry for sending so much links. Actually my network speed was slow and so this caused the problem. And so I continue sending links (as I was guessing it has been not send).
However here is the solution to your problem:
(1) Following code declared in .aspx page:
<span id="test" runat="Server">Hello Sister!</span>
(2) Following code in .cs (class file):
The HtmlGenericControl creates a server-side control for HTML tags that do not have an HTML control associated with them. Perfect examples of this are the <span> and <div> tags.
HtmlGenericControl control=(HtmlGenericContro
if (control != null)
{
control.InnerHtml = "Hello Brother!";
}
Regards,
VSS
ASKER
thnks, I got it to work but hadn't posted back yet. I used this code
Page page = (Page)HttpContext.Current. Handler;
HtmlGenericControl strTopics = default(HtmlGenericControl );
HtmlGenericControl strIssues = default(HtmlGenericControl );
HtmlGenericControl strObjectives = default(HtmlGenericControl );
HtmlGenericControl strZones = default(HtmlGenericControl );
strTopics = (HtmlGenericControl)page.F indControl ("TopicMen u");
strIssues = (HtmlGenericControl)page.F indControl ("IssueMen u");
strObjectives = (HtmlGenericControl)page.F indControl ("Objectiv esMenu");
strZones = (HtmlGenericControl)page.F indControl ("ZonesMen u");
Page page = (Page)HttpContext.Current.
HtmlGenericControl strTopics = default(HtmlGenericControl
HtmlGenericControl strIssues = default(HtmlGenericControl
HtmlGenericControl strObjectives = default(HtmlGenericControl
HtmlGenericControl strZones = default(HtmlGenericControl
strTopics = (HtmlGenericControl)page.F
strIssues = (HtmlGenericControl)page.F
strObjectives = (HtmlGenericControl)page.F
strZones = (HtmlGenericControl)page.F
Good ... Very good!
Happy Programming :)
Regards,
VSS
Happy Programming :)
Regards,
VSS
(1) As far I understand your problem statement you are trying for maintaining menu in all your pages consistently. As you mentioned in your problem statement, "The data returned and the span IDs are identical acroos all pages".
So in that case I would suggest you to use Master Page for all your .aspx pages. And set menu in that master page.
(2) And you can try your above code in master page code behind file as all required namespaces are declared there as compare to class files.
Regards,
VSS