Solved

User Controls - naming conventions?

Posted on 2004-10-19
3
611 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now