Groups | Blog | Home
all groups > asp.net building controls > november 2005 >

asp.net building controls : Textbox2 control that inherits from Textbox


Warren Patterson
11/30/2005 12:00:00 AM
Hi Guys,

I have a huge problem....I think.

I have created a control called Textbox2 which basically inherits from
Textbox. Now my problem is this.

The Textbox2 retains its state when I submit a form and it posts back.
However when I place a Textbox2 in the footer of a gridview, it loses its
value on postback and I have no way of getting the value. If I replace the
textbox2 with a normal textbox, I can get the value, using the same syntax,
with obvious ctype changes.

Off the bat does anyone know anything that may point me in the right
direction? If you need to see my code, I can post it.

PS: I am using .net 2.0 Framework. And I am a NOOB at control development,
having only created simple controls in teh past.

Many thank you's in advance.
Kind Regards
Warren

LepardUK
12/2/2005 2:57:55 AM
Willing to help but posting the code would be a pre-requisite.
Are You implementing IPostbackDataHandler for instance?
Does your control manage its own Viewstate or are you relying on
composite Textbox?
Warren Patterson
12/2/2005 12:08:06 PM
anyone?




[quoted text, click to view]

Warren Patterson
12/2/2005 1:05:28 PM

Hi. thanks for the response.

Here is my code:



'

'

'
**************************************************************************************************************************

' **********IMPORTANT INFORMATION WHEN WANTING THE TEXTBOX2 TO NOT VALIDATE
ON SUBMIT OF FORM BY A CERTAIN BUTTON.**********

'
**************************************************************************************************************************

'

' If this control is going to be used in a gridview or datagrid environment,
it is necessary that the buttons that submit

' the form that you dont want validation to occur on should have the
following onclick event defined

' EG: <asp:ImageButton Runat="server" ID="delete"
OnClientClick="validate('false');" ImageUrl="del.gif" BorderStyle="None"
AlternateText="Delete" CommandName="Delete"></asp:ImageButton>

' the validate function is automatically created on the form, so no need to
create it.

'

' If the button must validate, you dont need to do anything

' EG: <asp:ImageButton Runat="server" ID="Add" ImageUrl="add.gif"
BorderStyle="None" AlternateText="Add" CommandName="Add"></asp:ImageButton>

'

'
**************************************************************************************************************************

'
**************************************************************************************************************************

'
**************************************************************************************************************************



Imports System

Imports System.Web

Imports System.Web.UI

Imports System.ComponentModel

Imports System.IO.StringWriter



Imports System.Collections

Imports System.Collections.Specialized

Imports System.Web.UI.WebControls





<DefaultProperty("Text"), ToolboxData("<{0}:Textbox2
runat=server></{0}:Textbox2>")> Public Class Textbox2

Inherits TextBox

Implements INamingContainer

#Region "Globals"



Public Enum ValidatorType

None

Email

Numeric

MinMax

End Enum

#End Region

#Region "Properties"



Dim _Text As String = ""

<Bindable(True), Category("Appearance"), Description(""), DefaultValue("")>
Overrides Property [Text]() As String

Get

Return _Text

End Get

Set(ByVal Value As String)

_Text = Value

End Set

End Property





Dim _validator As ValidatorType

<Bindable(True), Category("Extended"), Description("What validator must be
used to validate input."), DefaultValue("")> Property [Validator]() As
ValidatorType

Get

Return _validator

End Get

Set(ByVal Value As ValidatorType)

_validator = Value

End Set

End Property



Dim _minValue As Decimal

<Bindable(True), Category("Extended"), Description("Minimum value that can
be input in the textbox. (Used when validator is minMax validator)"),
DefaultValue("")> Property [MinValue]() As Decimal

Get

Return _minValue

End Get

Set(ByVal Value As Decimal)

_minValue = Value

End Set

End Property

Dim _maxValue As Decimal

<Bindable(True), Category("Extended"), Description("Maximum value that can
be input in the textbox. (Used when validator is minMax validator)"),
DefaultValue("")> Property [MaxValue]() As Decimal

Get

Return _maxValue

End Get

Set(ByVal Value As Decimal)

_maxValue = Value

End Set

End Property

Dim _ErrorChar As String

<Bindable(True), Category("Extended"), Description("Select the text you want
to display if the input fails validation. (Overriden by ErrorImagePath)"),
DefaultValue("*")> Property [ErrorCharacter]() As String

Get

Return _ErrorChar

End Get

Set(ByVal Value As String)

_ErrorChar = Value

End Set

End Property

Dim _ErrorImagePath As String

<Bindable(True), Category("Extended"), DefaultValue(""), Description("Select
the image you want to display if the input fails validation. (Overrides the
ErrorCharacter)"), Editor(GetType(System.Web.UI.Design.ImageUrlEditor),
GetType(System.Drawing.Design.UITypeEditor))> Property [ErrorImagePath]() As
String

Get

Return _ErrorImagePath

End Get

Set(ByVal Value As String)

_ErrorImagePath = Value

End Set

End Property

Dim _ErrorMessageOnTextbox As String

<Bindable(True), Category("Extended"), DefaultValue(""),
Description("Overrides the default error message popup text that is
displayed when the textbox error indicator is clicked.")> Property
[ErrorMessageOnTextbox]() As String

Get

Return _ErrorMessageOnTextbox

End Get

Set(ByVal Value As String)

_ErrorMessageOnTextbox = Value

End Set

End Property



Dim _ErrorMessageOnSubmitVisible As Boolean

<Bindable(True), Category("Extended"), DefaultValue("true"),
Description("Show or hide error popup when the form is submitted and
validation fails.")> Property [ErrorMessageOnSubmitVisible]() As Boolean

Get

Return _ErrorMessageOnSubmitVisible

End Get

Set(ByVal Value As Boolean)

_ErrorMessageOnSubmitVisible = Value

End Set

End Property



Dim _Mandatory As Boolean

<Bindable(True), Category("Extended"), DefaultValue("false"),
Description("This textbox must have input.")> Property [Mandatory]() As
Boolean

Get

Return _Mandatory

End Get

Set(ByVal Value As Boolean)

_Mandatory = Value

End Set

End Property

#End Region

Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)

GenerateJS()

Dim sw As New System.IO.StringWriter

Dim HtmlTextWriter As New HtmlTextWriter(sw)

MyBase.Render(HtmlTextWriter)

Dim sTextboxHtml As String = sw.ToString

sw.Close()

sTextboxHtml = "<input type='textbox' text='" + _Text + "' value='" + _Text
+ "'runat='server' id='" + Me.ID.ToString + "' style='width:" +
Me.Width.ToString + ";' class='" + Me.CssClass.ToString + "' name='" +
Me.ID.ToString + "')>"

Dim strMessage As String = ""

Select Case _validator

Case ValidatorType.Email

strMessage = "The email address is not valid!"

Case ValidatorType.MinMax

strMessage = "Must be numeric between " + _minValue.ToString + " and " +
_maxValue.ToString

Case ValidatorType.None

strMessage = ""

Case ValidatorType.Numeric

strMessage = "Must be numeric! Only accepts the following: 0123456789."

End Select

If _Mandatory Then

If strMessage <> "" Then

strMessage = strMessage + " "

End If

strMessage = strMessage + "Cannot be empty."

End If

' if user has provided a custom error message, then it will override teh
defaults

If _ErrorMessageOnTextbox <> "" Then

strMessage = _ErrorMessageOnTextbox

End If

strMessage = "'" + strMessage + "'"

If strMessage <> "" Then

If _ErrorChar = "" Then

_ErrorChar = "*"

End If

If _ErrorImagePath = "" Then

sTextboxHtml = sTextboxHtml + "&nbsp;<a style=""display:none"" href='#'
Teemu Keiski
12/4/2005 7:55:40 PM
Just a quick glance but when you render your TextBox

sTextboxHtml = "<input type='textbox' text='" + _Text + "' value='" + _Text
+ "'runat='server' id='" + Me.ID.ToString + "' style='width:" +
Me.Width.ToString + ";' class='" + Me.CssClass.ToString + "' name='" +
Me.ID.ToString + "')>"

you shouldn't use me.ID for id and name attributes. For rendered id
attribute you should be using Me.ClientID and for name attribute
Me.UniqueID, This , especially name attribute,is crucial so that ASP.NET's
postback mechanism works.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke




[quoted text, click to view]
AddThis Social Bookmark Button