locked
Registering controls RRS feed

  • Question

  • User1760382400 posted

    I'm trying to understand how to register a user control so that it can be used anywhere in my web site. Here's my dilemma:

    I created a customer validation control in a .vb file. That file lives in the App_Code directory. I tested it on a .aspx web page in the main folder of the web site. I now want to use that control inside another user control. My user controls are in a sub directory called "Controls". I opened the new control and registered the first control in the aspx file, but it refuses to let me add the control to the new one. I tried registering the control in the web.config file but had no luck there either. I have to admit I'm a bit confused about the different ways to register controls. I see some examples that have 'assembly' references, others that do not. Some that have 'namespace' references, other that do not. Can someone explain how to register the control in my specific case (above) and maybe give me a general explanation of when assembly and namespace references are required.

    Thanks in advance...

     

    Sunday, April 6, 2008 9:47 PM

Answers

  • User-16411453 posted

    I looked up the class, and I guess your talking about the RequiredFieldValidator Controls, and are not writing a validator from scratch.

    WebCustomControl :

    I use them in my AskQuestion Server Control. They were sort of tricky to implement at first, but after a week I understood there gig. I don't use a basevalidator class. The control has built-in Ajax Update Panel, RFV, Validation Checkers. The caveat was that they had to be implemented in OnInit, and could not be created in CreateChildControls.

    sample of the control: http://www.ice5software.com/SoftwarePrograms/AskQuestion.aspx

    I declared them as a global in the top of the class because I wrote it awhile ago

    Public Class AskQuestion_Control
    	Inherits WebControl
    	Implements IPostBackDataHandler
    
    	Public Event TextChanged As EventHandler
    	Public Event ButtonClick As EventHandler
    
    	Public rfvQuestion_Name As System.Web.UI.WebControls.RequiredFieldValidator
    
    	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    		MyBase.OnInit(e)
    
    		Controls.Clear()
    		Dim txtQuestion_Name As TextBox
    		txtQuestion_Name = New TextBox
    		txtQuestion_Name.ID = [ID] & "_txtQuestion_Name"
    		txtQuestion_Name.CssClass = [CssStyle_InputControl]
    		txtQuestion_Name.ValidationGroup = "Question"
    		txtQuestion_Name.Width = 140
    		txtQuestion_Name.Attributes.Add(HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference(Me, "field"))
    		tdNameField.Controls.Add(txtQuestion_Name)
    
    		rfvQuestion_Name = New RequiredFieldValidator
    		rfvQuestion_Name.ControlToValidate = [ID] & "_txtQuestion_Name"
    		rfvQuestion_Name.ValidationGroup = [ID] & "_Question"
    		rfvQuestion_Name.Display = ValidatorDisplay.Dynamic
    		rfvQuestion_Name.CssClass = [CssStyle_Text]
    		rfvQuestion_Name.ErrorMessage = "*"
    		tdNameField.Controls.Add(rfvQuestion_Name)	
    	End Sub
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2008 2:16 PM

All replies

  • User-16411453 posted

     The majority of controls in this forum are server controls, like web custom control, composite control, etc. They can be created in new projects such as class projects, or control projects.

    You compile them into a dll file, and then you place them in the bin folder.  From there, you can register the assembly in your aspx markup, or webform code.  The tag prefix stuff.

    I guess you can import the compiled control into a new control, and recompile the new control.

    I use the namespaces in my controls to separate code modules.  I have modules that use the same public variable names and collections, so I use namespaces so they don't conflict with each other. I can have classes with the same name, but the namespace divides them. The code is different to render different output.

    Like school lunches with food in them; you can have 2 lunch bags that are identical with the same sandwich, chips and water, but one is for bill and the other is for sue. 

    When you regsiter the assembly in your markup using namespaces, your telling bill to grab the lunch bag with bill written on it, and sue to grab the bag with sue written on it.

    If you have 1 lunch bag, and 1 kid, then you don't need to use the namespace. You just register the assembly in your markup. 1 kid, 1 lunch


     

     

    Monday, April 7, 2008 2:01 AM
  • User1760382400 posted

    Thanks for the reply. Good info. I tried to build my control in a totally different projec but then ran into a problem with the required references. To build a new Validator control, I need to inherit 'BaseValidator'. For the life of me I can't seem to get the correct item imported so that the new project knows about the BaseValidator class. That's another topic that I don't quite understand, i.e. importing. I can never seem to find the correct module(s) to import. Any suggestions?

     

    Monday, April 7, 2008 12:27 PM
  • User-16411453 posted

    I looked up the class, and I guess your talking about the RequiredFieldValidator Controls, and are not writing a validator from scratch.

    WebCustomControl :

    I use them in my AskQuestion Server Control. They were sort of tricky to implement at first, but after a week I understood there gig. I don't use a basevalidator class. The control has built-in Ajax Update Panel, RFV, Validation Checkers. The caveat was that they had to be implemented in OnInit, and could not be created in CreateChildControls.

    sample of the control: http://www.ice5software.com/SoftwarePrograms/AskQuestion.aspx

    I declared them as a global in the top of the class because I wrote it awhile ago

    Public Class AskQuestion_Control
    	Inherits WebControl
    	Implements IPostBackDataHandler
    
    	Public Event TextChanged As EventHandler
    	Public Event ButtonClick As EventHandler
    
    	Public rfvQuestion_Name As System.Web.UI.WebControls.RequiredFieldValidator
    
    	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    		MyBase.OnInit(e)
    
    		Controls.Clear()
    		Dim txtQuestion_Name As TextBox
    		txtQuestion_Name = New TextBox
    		txtQuestion_Name.ID = [ID] & "_txtQuestion_Name"
    		txtQuestion_Name.CssClass = [CssStyle_InputControl]
    		txtQuestion_Name.ValidationGroup = "Question"
    		txtQuestion_Name.Width = 140
    		txtQuestion_Name.Attributes.Add(HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference(Me, "field"))
    		tdNameField.Controls.Add(txtQuestion_Name)
    
    		rfvQuestion_Name = New RequiredFieldValidator
    		rfvQuestion_Name.ControlToValidate = [ID] & "_txtQuestion_Name"
    		rfvQuestion_Name.ValidationGroup = [ID] & "_Question"
    		rfvQuestion_Name.Display = ValidatorDisplay.Dynamic
    		rfvQuestion_Name.CssClass = [CssStyle_Text]
    		rfvQuestion_Name.ErrorMessage = "*"
    		tdNameField.Controls.Add(rfvQuestion_Name)	
    	End Sub
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2008 2:16 PM