Custom Server Control

M

Madison

I had created custom textbox server control and try to use in the web
application by placeing DLL in bin folder and add DLL in the reference. Then
in the web.config I added the following:
<controls>
<add namespace="pscControl.psc" assembly="pscControl" tagPrefix="psc"/>
</controls>

In the web form I can call <psc:requiredTextbox id="test" runat="server" />
I do not get any errors but when I switch to design mode I got the following
message:

Error Rendering Control - test
An unhandled execptions has occurred.
Object reference not set to an instance of an object.

What is the correct way to call customer server control?
Thank you.
 
W

Walter Wang [MSFT]

Hi,

This is the correct way. Unless the error says "Error creating control",
the control is created successfully. Since the error you're experiencing
says "Error rendering control", it's probably because the control's
rendering code (or the control designer if the control associates with a
custom control designer) throws some exception. Try to post some of your
control code for further inspection.

By the way, welcome to asp.net newsgroup!

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Nelson

Madison,

Please quicly do onething before confirming that this is the problem due to
rendering of Custom Controls.

Instead of doing a dll reference to the CustomControl try adding the project
reference.
Remove the dll reference for you CustomControl
Go to File - > Add - > Existing Project / New Project
I mean, add a class library project type to your website and put all your
code inside that. Name the assemblies and Namespaces properly.

Now compile the entire website (including your class library project for
CustomControl) Visual Studio 2005 automatically senses your custom control.
You'll see your control in the Toolbox. Drag and Drop this control to the
designer surface.

If the error still p ersists, then problem is surely with the way the custom
control is rendered.
Please provide us the snippet you have used for creating custom control.
 
W

Walter Wang [MSFT]

Hi,

Have you seen the suggestions above? Would you please let us know the
status of this post? Thank you.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hi Walter:

I'm sorry that I did not follow up the answer. I got urgent project to get
it done. Now I have time to get back the track again.
 
M

Madison

Hi,

Below is my coding for creating web custom control (My application name is
pscControl)

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace psc

Public Class myTextBox
Inherits TextBox
Private req As RequiredFieldValidator
Public InvalidMessage As String
Public ClientScript As String = "true"

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
req = New RequiredFieldValidator
req.ControlToValidate = Me.ID
req.ErrorMessage = Me.InvalidMessage
req.EnableClientScript = Me.ClientScript.ToLower <> "false"
Me.Controls.Add(req)
End Sub

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
MyBase.Render(writer)
req.RenderControl(writer)
End Sub

End Class

End Namespace

And here how I use it in the aspx page
<%@ Register TagPrefix="psc" Namespace="pscControl.psc"
Assembly="pscControl" %>


<psc:myTextBox ID="test1" runat="server" InvalidMessage="Required Field">
</psc:myTextBox>

when I go to Design mode I got this message:
Error Rendering Control - test1
An unhandled exception has occurred.
Object reference not set to an instance of an object.

Thank you for your help.
 
W

Walter Wang [MSFT]

Hi Madison,

I can see your purpose is to create a customized textbox control that has a
RequiredFieldValidator attached. Well, this is better served with a
composite control instead inheriting from TextBox directly.

Also, you will need to use ViewState to persiste the public properties
InvalidMessage and ClientScript instead of simply using public member
variables.

Last, you need to instantiate the constituent controls in
CreateChildControls method instead of OnInit:

Public Class myTextBox2
Inherits CompositeControl
Private txt As TextBox
Private req As RequiredFieldValidator

Public Property InvalidMessage() As String
Get
Return CType(ViewState("InvalidMessage"), String)
End Get
Set(ByVal value As String)
ViewState("InvalidMessage") = value
End Set
End Property

Public Property ClientScript() As Boolean
Get
Dim v As Object = ViewState("ClientScript")
If v Is Nothing Then Return False
Return CType(v, Boolean)
End Get
Set(ByVal value As Boolean)
ViewState("ClientScript") = value
End Set
End Property

Protected Overrides Sub CreateChildControls()
Controls.Clear()
txt = New TextBox()
txt.ID = "text1"
Controls.Add(txt)

req = New RequiredFieldValidator()
req.ControlToValidate = txt.ID
req.ErrorMessage = InvalidMessage
req.EnableClientScript = ClientScript
Controls.Add(req)
End Sub
End Class

Please refer to following article on how to create a composite control in
ASP.NET:

#A Crash Course on ASP.NET Control Development: Building Composite Controls
http://msdn2.microsoft.com/en-us/library/aa479016.aspx


There're also some other great crash course articles on developing ASP.NET
custom controls there.

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hello Walter,

Thank you for your reply. I will read the article about create a composite
control as you recomment.
 
W

Walter Wang [MSFT]

Hi Madison,

Thanks for your follow-up.

Please feel free to let me know if there's anything I can help.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top