Solved

User Controls - naming conventions?

Posted on 2004-10-19
3
613 Views
Last Modified: 2012-06-27
Hi there,

Just started to create a user control and wondered if anybody can comment on naming conventions etc?

For my actual control I used  Pascal Casing and called it MainHeader

then when ever i insert this into a page it gives an automatic name of MainHeaderx where x is =1 , 2 or whatever..

What do people normally do here MainHeaderDefaultPage i.e. MainHeader (name of control) and afterwards add the page name?? or use an underscore ?? or just leave as 1,2,3 etc .. but this naming seems strange and unfriendly...

Any ideas or help would be really great

thanks

ian
0
Comment
Question by:ianinspain
  • 2
3 Comments
 

Author Comment

by:ianinspain
ID: 12345334
Oh and I forgot to mention, is it ok to put Javascript into a usercontrol, i am using an application called AllWebMenus which are basically some nice menus but they need to attach javascript where the menu is (usercontrol), i know best practises are to put the javascript in the head section of a webform, anyway to overide this .. i.e. force a move when page is built or should I just cut and past the javascript into the main webform under Head section?'

Ian
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 500 total points
ID: 12345548
Naming convention should be anything you feel comfortable with. But do please note that the INamingContainer (implemented by almost all WebControls) will change the resulting id of your object in the output. With usercontrols all I can say is that the name should reflect the object in such a way that you see the name and know what object you have.  

I would give my usercontrol a meaningful name, and whenever used on a page I would give the instance a meaningful id... or else it would come up as ctrlX with X being a number... makes things hard to distinguish.

Next question you have is how to add javascript to the header. The page object provides several functions to add javascript code to the page. For example (code copied from MSDN)

<html>
  <head>
     <script language="VB" runat="server">
    Sub Page_Load( sender as Object,e as EventArgs)
         
       'Form the script that is to be registered at client side.
        Dim scriptString as String = "<script language=JavaScript> function DoClick() {"
        scriptString += "myForm.show.value='Welcome to Microsoft .NET'}<"
        scriptString += "/"
        scriptString += "script>"
     
            If(Not IsClientScriptBlockRegistered("clientScript"))
        RegisterClientScriptBlock("clientScript", scriptString)
        End If
     End Sub
     </script>
  </head>
  <body topmargin="20" leftmargin="10">
     <form id="myForm" runat="server">
        <input type="text" id="show" style="width=200">
      <input type="button" value="ClickMe" onclick="DoClick()">
     </form>
  </body>
</html>

But there are other functions to add script to the page. Here is an overview.

IsClientScriptBlockRegistered Determines if the client script block is registered with the page.
IsStartupScriptRegistered Determines if the client startup script is registered with the Page object.
RegisterArrayDeclaration Declares a value that is declared as an ECMAScript array declaration when the page renders.
RegisterClientScriptBlock Allows ASP.NET server controls to emit client-side script blocks in the Page.
RegisterOnSubmitStatement Allows a page to access the client OnSubmit event. The script should be a function call to client code registered elsewhere.
RegisterStartupScript Allows ASP.NET server controls to emit client-side script blocks in the Page.
0
 

Author Comment

by:ianinspain
ID: 12387255
Thanks CJ_S,

Thanks for the help, I have decided to name like this..

If my control is called "Header" then an instance on my default.aspx would be called

defaultHeader

Thanks for the script stuff too ... that was great,

Thanks - awarding points now

Ian
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

778 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