Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detect components in a webform

Status
Not open for further replies.

julk

Programmer
Jun 23, 2004
10
US
hi im kind of new to C#.
i am doing a Ritch text editor dll, for a webform that im going to need.
the components runs fine it does all it need to do, and exposes all the properties i need it to expose. the proble that i have is when i drop two of my componentes into the webform there area few conflicts.

I am generating all the javaScript functions that do the BOLD,ITALIC
for the buttons. since i am doing that if two controls are droped the java script is generated twice. there fore creting an error in java script.
i found quick solution for that by just naming the function like this

functionName_"the id of the control"

this generated diferent function for diferent controls. which is fine and works. but also as it is generating twice the javaScript. with just two components i get 300 line of code. i think this is a big problem. i want to create a unique function for all of the controls that get droped into the webform. jus by calling a function and sending the name of the caller
kind o like this

changeFont(var fontSize, "name of the control that call the function");

so i gues the question is: is there a way to detect how many of my controls ahve been droped into the webform?

with this info i would only generate one set of function for all of the controls.
i have an example running at
 
julk,
This might shed some light, although I'm not sure it's completely applicable to you. This method works very nicely with simple classes, and since in .NET everything is an object, it just may work for you.

The trick is to implement a static variable in your class. This variable will represent the number of instances that have been created, thus, you should increment it in the class constructor. it would look something like this:
Code:
[COLOR=blue]public class[/color] YourClass
{
  [COLOR=green]// variable to count number of instances[/color]
  [COLOR=blue]private static int[/color] _instCount;
 
  [COLOR=green]...

  // Increment _instCount in the 
  // class constructor[/color]
  [COLOR=blue]public[/color] YourClass()
  {
    _instCount++;
  } 

  [COLOR=green]// Static read-only property that 
  // returns the number of instances[/color]
  [COLOR=blue]public static int[/color] Instances
  {
    [COLOR=blue]get[/color] {[COLOR=blue]return[/color] _instCount}
  }
}
Now, the statement YourClass.Instances is accessible from anywhere in your code, and it will return the number of instances of YourClass that have been created. For code that's inside the class itself, you may simply use the private static variable _instCount.

The last point to mention is the syncronization of _instCount and the number of instances that have actually been created. In other words, what happens when you destroy an instance of YourClass? How would you go about decrementing _instCount?

Well, the simplest thing is to do is to implement the IDisposable interface. (I mean, since your classes will be created at design time, I don't even think you'll ever need this, but it's good to have). As you may be aware, the IDisposable interface exposes only one method, Dispose, which is used to dispose of resources a given instance of a class uses. Before destroying an object, good programming practice dictates that the Dispose method be called. Of course, if the client code does not call Dispose, you need a way to call it yourself to ensure that your _instCount is decremented. That's where yet another method, called Finalize(), comes in. This is a sepcial method called by the Garbage Collector before it relases the momory allocated for an object. Finalized() is implemented in C# as what is called a Destructor. Thus, you need to implement a destructor for your class and call Dispose from it.

This is all a bit complicated, because: 1 - The GC doesn't call finalize until the next garbage collection (which doesn't exactly have to be when the object goes out of scope or is set to null). 2 - Finalize() may be called multiple by the GC, and 3 - When Finalize() is called, other objects that are accessed by your object may have been already destroyed. This means that you shouldn't try to access them. Because of those three points, you should orchestrate your Destuctor and Dispose method in such a way that would prevent you from (1)trying to access other objects if they're not avaiable, and (2)executing the code in Dispose twice. Jesus!!! I hope all of this makes sense. If it does, here's how you would update the class above to add the syncronization I just spoke of:
Code:
[COLOR=blue]public class[/color] YourClass [b]: IDisposable[/b]
{
  [COLOR=green]// variable to count number of instances[/color]
  [COLOR=blue]private static int[/color] _instCount;

  [COLOR=green]// Flags if this object has been disposed[/color]
  [b][COLOR=blue]private int[/color] _disposed;[/b]
 
  [COLOR=green]...

  // Increment _instCount in the 
  // class constructor[/color]
  [COLOR=blue]public[/color] YourClass()
  {
    _instCount++;
  } 

  [COLOR=green]// Static read-only property that 
  // returns the number of instances[/color]
  [COLOR=blue]public static int[/color] Instances
  {
    [COLOR=blue]get[/color] {[COLOR=blue]return[/color] _instCount}
  }

  [COLOR=green]//-------------------------------------
  // New members below
  //-------------------------------------[/color]
  
  [COLOR=green]// This is IDisposable.Dispose[/color]
  [COLOR=blue]public void[/color] Dispose()
  {
    Dispose([COLOR=blue]true[/color]);
  }

  [COLOR=blue]public void[/color] Dispose([COLOR=blue]bool[/color] disposing)
  {
    [COLOR=blue]if[/color] (!_disposed)
    {
      [COLOR=blue]if[/color] (disposing)
      {
      [COLOR=green]/* This object is being disposed,
         not finalized. It's safe to access
         other objects here
      */[/color]
      }
     
      [COLOR=green]// Decrement the instance count and flag
      // that the object has been disposed[/color]
      [b]_instCount--;
      _disposed = [COLOR=blue]true[/color];[/b]
    }
  }  

  [COLOR=green]// Class destructor - this is the
  // Finalize() method in disguise[/color]
  ~YourClass()
  {
    Dispose([COLOR=blue]false[/color]);
  }  
}
Two points and I'm done:
1 - Before getting rid of an object, client code must call Dispose(true) so that the _instCount is properly decrement it.

2 - If Dispose(true) is not called by client code, _instCount might not be decremented until the application terminates. Yikes!!! But, hey, that's better than nothing.

I hope all this makes sense and that it at leasts points you in the right direction.

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
thank you for your responce but, after i posted this question, i went on and search the net. and this is what i found. i dont need to used a static variable (although i does seem like a logic solution), all i need to do was register a client script

Code:
if(!Page.IsClientScriptBlockRegistered("Ritch_text_javaScript"))
{
  Page.RegisterClientScriptBlock("Ritch_text_javaScript", scriptString);
}

this seem to work only the first object dropped into the form will generate the javaScript i neeed to control all other controls.
thank you for your responce, i think i will use this solution, but it is always helpfull to know other solutions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top