Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# code request: Using "if" "else" in a user control to write javascript into the body "onload" event

Posted on 2005-05-03
11
Medium Priority
?
322 Views
Last Modified: 2007-12-19
I have an admin system that uses "user controls" to provide the open head and body (header.ascx) and close body (footer.ascx) of the admin centre html pages.

So for each page I (currently) have something like:

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="admin._default" %>
<form id="mainForm" method="post" runat="server">
      Main Content Here
</form>

Each page references a class containing the code section below to load the header and footer controls at the start and end of the page respectively:

protected override void OnInit(System.EventArgs e)
  {
    this.Controls.AddAt(0, LoadControl("Header.ascx"));
    base.OnInit(e);
    this.Controls.Add(LoadControl("Footer.ascx"));
}

The header control currently looks like this:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Header.ascx.cs" Inherits="admin.Header" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html>
  <head>    
    <title>Admin Center</title>
    <meta name='robots' content='noindex,nofollow' ></head>
   
  <body>

Here's the question:

I'm reworking an old ASP3 script that simply does this:

<%
   show_what = "this_menu_option" 'Hard coded into the top of each main page (but could be passed in the QueryString
   
   If show_what <> "" Then
     Response.Write("<body onload=""MM_showHideLayers('" & show_what & "','','show')"">")
   Else
    Response.Write("<body>")
   End If
%>

So currently the Header.ascx control includes the <body> tag, so how would I get the Header.ascx to pickup on the "show_what" variable and replicate the code output as shown above in the ASP 3 example (this time in C#)?

Many thanks.
0
Comment
Question by:w3digital
  • 5
  • 5
11 Comments
 
LVL 10

Expert Comment

by:lengreen
ID: 13922045
Hi

Is this what you are after?

<%
   string show_what = "this_menu_option"; //Hard coded into the top of each main page (but could be passed in the QueryString
   
   if (show_what != ""){
     Response.Write("<body onload=""MM_showHideLayers('" + show_what + "','','show')"">");
   } else {
    Response.Write("<body>");
   }
%>

cheers

Len
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13922109
or you can do it in code by gaining a reference to the body tag and simply adding an attribute.
0
 

Author Comment

by:w3digital
ID: 13922293
Thanks Len,

Acutally no, I don't think that is what I am after as I will need to specific on a page by page basis which javascript menu option I wish to call from the body onload event handler. So the "show_what" variable will be specified in ... say ... the default.aspx.cs.

Then the header.aspx should output the code:

                               <body onload="MM_showHideLayers('main','','show')">

I did try something like the code you have submitted above and got the following error:

The name 'show_what' does not exist in the class or namespace 'ASP.Header_ascx'

I was thinking something like this (but that works...)

                              // CODE behind default.aspx.cs

            private void Page_Load(object sender, System.EventArgs e)
            {
                                                show_what = "admin_menu"; // put here for example only
                  if (show_what != "")
                  {
                        // below code is rubbish but the sort of thing I need to programmatically add
                        // an onload attribute to <body> tag on a page by page basis
                        Body.Attributes["onload"] = "MM_showHideLayers('" + show_what + "','','show')";
                  }
            }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:w3digital
ID: 13922295
Ah, I just saw gregoryyoung's comment after submitting my thoughts.

That's what I'm after.

gregoryyoung, can you provide a code sample please?
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 1500 total points
ID: 13922347
0
 

Author Comment

by:w3digital
ID: 13922454
Thanks gregoryyoung,

I reckon I'm almost there but am doing something wrong.

Here's the Page_Load code:

                                private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  this.PageTitle = "Admin main menu";
                  HtmlGenericControl bodytag = (HtmlGenericControl)FindControl("body");
                  bodytag.Attributes["onload"] = "MM_showHideLayers('admin_show','','show')";
            }

Here's the error message: Any ideas?

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 18:                   HtmlGenericControl bodytag = (HtmlGenericControl)FindControl("body");
Line 19:                   bodytag.Attributes["onload"] = "MM_showHideLayers('admin_show','','show')";
Line 20:             }
Line 21:
 
Thanks.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13922470
is bodytag null ?

can you also post your aspx ...
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13922478
remember you need to put runat=server id= on the body tag.

Greg
0
 

Author Comment

by:w3digital
ID: 13922527
Hi Grey

Yeah, I did ensure the runat=server etc but I was missing it - doh!

I have this:

<body id="body" runat="server">

then in the default.aspx.cs I have:

            private void Page_Load(object sender, System.EventArgs e)
            {
                  HtmlGenericControl bodytag = (HtmlGenericControl)FindControl("body");
                  bodytag.Attributes["onload"] = "MM_showHideLayers('admin_show','','show')";
            }

But I guess having the open body in the header.ascx file and the close body in the footer.ascx file is causing problems as I get the below error:

Parser Error Message: Unexpected end of file looking for </body> tag.

I imagine calling the code in the Page_Load event is the wrong place. Is there anywhere else I should put this code that will work? Or am I asking the impossible when <body> is in one user control and </body> is in another?

Thanks very much for your help you certainly deserve the points.
0
 

Author Comment

by:w3digital
ID: 13922545
Ah, I see it's not the code, it's the actual Header.ascx user control that's playing up. I think I'm going to bring the body tag into the main file. That'll be simpler.

Yes, that sorts it!

Ta very much
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13922574
no worries. glad to help ...

btw its a nice thing to put in a page base class for reuse :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question