Custom Control - Specific placement of HTML Textwriter output - Ho

G

Guest

I have a custom menu control that I am creating using C#. I am rendering
HTML from a StringBuilder in my control to add the needed JavaScript to the
HTML output. I need to have the JavaScript output from the stringbuilder
placed at the bottom of my <BODY> section, after the top navigation menu
dropdown dataset output. Currently the JavaScript is output to the top of
the <BODY> section of my HTML output from the control.

How can I accomplish this?

Thanks,
Hawk
 
G

Guest

I believe I've done something similar to this before, where I wanted to push
extra controls onto the end of a form. I did this by referencing the form in
my codebehind and then using the Form.Controls.Add(Control) method to add
whichever controls I wanted to create programatically during the PreRender
event.

The PreRender event is effectively the last event to fire before the ASPX
page saves the controls to viewstate, it is the last event available to be
overloaded prior to the page rendering out--it even fires after the Event
Handlers.

Try putting a runat="server" attribute in your body tag (ie: <body
id="bdyPage" runat="server"> of your aspx markup, and then referencing it as
an HtmlGenericControl in your codebehind: protected
System.Web.UI.HtmlControls.HtmlGenericControl bdyPage). Then overload the
ASPX page's PreRender event and output your StringBuilder/JavaScript to the
page at that time. You can expose this through a public method or property
in your custom menu control.

Hope it works/helps.
 
G

Guest

I think I figured it out... I ended up using a "Literal" and did the following:

//Builds script at bottom of page
System.Text.StringBuilder _BuilderBottom = new
System.Text.StringBuilder();
_BuilderBottom.Append("<script type=\"text/javascript\">\n");
_BuilderBottom.Append(" var nav= new DropDownMenu('Navigation');\n");
_BuilderBottom.Append(" nav.delay.show = 0;\n");
_BuilderBottom.Append(" nav.delay.hide = 400;\n");
_BuilderBottom.Append(" nav.position.levelX.left = 2;\n");
_BuilderBottom.Append(" nav.init();\n");
_BuilderBottom.Append(" </script>\n");

//Adds the javascript to the bottom of the table
Literal javaScriptLiteral = new Literal();
javaScriptLiteral.Text = _BuilderBottom.ToString();
Controls.Add(javaScriptLiteral);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top