Textbox2 control that inherits from Textbox

W

Warren Patterson

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
 
L

LepardUK

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?
 
W

Warren Patterson

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='#'
onclick=""alert(" + strMessage + ");"" alt='Click for more info.'
id='notify_" + Me.ID.ToString + "' name='notify_" + Me.ID.ToString +
"'><font color='red'>" + _ErrorChar + "</font></a>"

Else

sTextboxHtml = sTextboxHtml + "&nbsp;<img style=""display:none"" src='" +
_ErrorImagePath + "' onclick=""alert(" + strMessage + ");""
onmouseover=this.style.cursor='hand' alt='Click for more info.' id='notify_"
+ Me.ID.ToString + "' name='notify_" + Me.ID.ToString + "'>"

End If

End If



HtmlTextWriter.Close()

output.Write(sTextboxHtml)

End Sub

Private Sub GenerateJS()



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

'**** ****

'**** This is code that is global for the whole page any textbox2 control
uses it****

'**** ****

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

Dim scrp As String = ""

scrp += " <script language='javascript'> "

scrp += " function focusAgain(objName) {"

scrp += " var objID = objName.id;"

scrp += " var obj = document.getElementById(objName.id);"

scrp += " obj.style.background = 'cyan';"

scrp += " }" 'End Function

' Checks for valid email addresses

scrp += " function chkEmail(AnArray) { "

scrp += " var obj;"

scrp += " obj = document.getElementById(AnArray.ID);"

scrp += " var checkStr = obj;"

scrp += " var str = checkStr.value;"

scrp += " if ((str.indexOf('.') > 2) && (str.indexOf('@') > 0) || (str ==
'')) "

scrp += " { "

scrp += " AnArray.Error = 'False';"

scrp += " } "

scrp += " else { "

scrp += " AnArray.Error = 'True';"

scrp += " }"

scrp += " } "

scrp += "function chkNumeric(AnArray,minval,maxval,comma,period,hyphen)"

'// only allow 0-9 be entered, plus any values passed

'// (can be in any order, and don't have to be comma, period, or hyphen)

'// if all numbers allow commas, periods, hyphens or whatever,

'// just hard code it here and take out the passed parameters

scrp += "{"

scrp += "var obj;"

scrp += "obj = document.getElementById(AnArray.ID);"

scrp += "var checkOK = '0123456789' + comma + period + hyphen;"

scrp += "var checkStr = obj;"

scrp += "var allValid = true;"

scrp += "var decPoints = 0;"

scrp += "var allNum = '';"

scrp += ""

scrp += "for (i = 0; i < checkStr.value.length; i++)"

scrp += "{"

scrp += "ch = checkStr.value.charAt(i);"

scrp += "for (j = 0; j < checkOK.length; j++)"

scrp += "if (ch == checkOK.charAt(j))"

scrp += "break;"

scrp += "if (j == checkOK.length)"

scrp += "{"

scrp += "allValid = false;"

scrp += "break;"

scrp += "}"

scrp += "if (ch != ',')"

scrp += "allNum += ch;"

scrp += "}"

scrp += " if ((!allValid) && (checkStr.value != ''))"

scrp += " {"

scrp += " AnArray.Error = 'True';"

scrp += " }"

scrp += " else {"

scrp += " AnArray.Error = 'False';"

scrp += " }"

scrp += "}" 'end function

scrp += " function chkMinMax(AnArray,minval,maxval) {"

scrp += " var obj;"

scrp += " obj = document.getElementById(AnArray.ID);"

scrp += " var checkOK = '0123456789';"

scrp += " var checkStr = obj;"

scrp += " var allValid = true;"

scrp += " var decPoints = 0;"

scrp += " var allNum = '';"

scrp += " for (i = 0; i < checkStr.value.length; i++)"

scrp += " {"

scrp += " ch = checkStr.value.charAt(i);"

scrp += " for (j = 0; j < checkOK.length; j++)"

scrp += " if (ch == checkOK.charAt(j))"

scrp += " break;"

scrp += " if (j == checkOK.length)"

scrp += " {"

scrp += " allValid = false;"

scrp += " break;"

scrp += " }"

scrp += " if (ch != ',')"

scrp += " allNum += ch;"

scrp += " }"

scrp += " var chkVal = allNum;"

scrp += " var prsVal = parseInt(allNum);"

scrp += " if (chkVal != '' && (prsVal < minval || prsVal > maxval)) "

scrp += " { "

scrp += " AnArray.Error = 'True';"

scrp += " } "

scrp += " else {"

scrp += " AnArray.Error = 'False';"

scrp += " }"

scrp += " }" 'end function

scrp += " function chkMandatory(AnArray) { "

scrp += " var obj;"

scrp += " obj = document.getElementById(AnArray.ID);"

scrp += " var checkStr = obj;"

scrp += " if (checkStr.value.length != 0) "

scrp += " { "

scrp += " AnArray.Error = 'False';"

scrp += " } "

scrp += " else { "

scrp += " AnArray.Error = 'True';"

scrp += " }"

scrp += " } "

' Set a variable up to be used to count errors (if errors > 0 then page not
allowed to submit)

scrp += " var strArray = '';"

scrp += " var intErrCount;"

scrp += " intErrCount = 0;"

' If more than 1 textbox2 is added to a form, we need to see if

' any of the textbox2's want the onSubmit message to be shown

scrp += " var intShowSubmitError = 0;"

scrp += "function objTextboxes(ID, Validator, Mandatory, Error) {"

scrp += " this.ID = ID;"

scrp += " this.Validator = Validator;"

scrp += " this.Mandatory = Mandatory;"

scrp += " this.Error = Error;"

scrp += "}"



'Buttons that submit a form on a page will only validate if the button's
onclick event has onclick='validate(true);'

scrp += "var blvalidate = true;"

scrp += "function validate(Validate) {"

scrp += " blvalidate = Validate;"

scrp += "}"



' Loops through all elemnts in the form to locate those startign with
'notify_' and then gets their

' visibility status, if they are visible, then there is an error on the
control, else, there is no error

scrp += "function CheckForError() { "

scrp += " if (blvalidate == true) {"

scrp += " var x = 'var myArray=new Array(' +
strArray.substr(1,strArray.length) + ');';"

scrp += " eval(x); "

'Loop through elements in array and check if they have errors on the form

scrp += " for (z=0; z<myArray.length; z++) {"

'Valid email

scrp += " if (myArray[z].Validator == 'Email') { "

scrp += " chkEmail(myArray[z]);"

scrp += " }"

'Is Numeric

scrp += " if (myArray[z].Validator == 'Numeric') { "

scrp += " chkNumeric(myArray[z],0,0,',','.','');"

scrp += " }"

'Is between min and max value

scrp += " if (myArray[z].Validator == 'MinMax') { "

scrp += " chkNumeric(myArray[z],0,0,',','.','');"

scrp += " if (myArray[z].Error == 'False')"

scrp += " {"

scrp += " chkMinMax(myArray[z]," + _minValue.ToString + "," +
_maxValue.ToString + ");"

scrp += " }"

scrp += " }"

'Has data

scrp += " if (myArray[z].Mandatory == 'True') { "

scrp += " if (myArray[z].Error == 'False')"

scrp += " {"

scrp += " chkMandatory(myArray[z]);"

scrp += " }"

scrp += " }"

scrp += " }"

' Loop through elements in array and check if they have errors in the Error
property, if an error, then

' display the error message.

scrp += " for (z=0; z<myArray.length; z++) {"

scrp += " if (myArray[z].Error == 'True') { "

scrp += " var objNotify;"

scrp += " objNotify = document.getElementById('notify_' + myArray[z].ID);"

scrp += " objNotify.style.display = '';"

scrp += " intErrCount = intErrCount + 1;"

scrp += " }"

scrp += " else {"

scrp += " var objNotify;"

scrp += " objNotify = document.getElementById('notify_' + myArray[z].ID);"

scrp += " objNotify.style.display = 'none';"

scrp += " "

scrp += " }"

scrp += " }"



scrp += " if ((intErrCount != 0)) {"

scrp += " if (intShowSubmitError != 0) {"

scrp += " alert('There is a problem with the form you are trying to
submit.'); "

scrp += " }"

'End If

'End If

scrp += " intErrCount = 0; "

scrp += " return false; "

scrp += " } "

scrp += " else { "

scrp += " return true;"

scrp += " }"



scrp += "}" 'End if (blvalidate == true)

scrp += "} "

scrp += " </script> "

' Adds the global javascript

Page.ClientScript.RegisterStartupScript(Me.GetType(), "textbox2", scrp)





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

'**** ****

'**** This is code that is control specific (Different for each control on a
page****

'**** ****

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

Dim scrp2 As String

scrp2 = " <script language='javascript'> "

If _ErrorMessageOnSubmitVisible Then

scrp2 += "intShowSubmitError = intShowSubmitError +1;"

End If

Select Case _validator

Case ValidatorType.None

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""None"",""" + _Mandatory.ToString + """,""False"")'"

Case ValidatorType.Email

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""Email"",""" + _Mandatory.ToString + """,""False"")'"

Case ValidatorType.Numeric

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""Numeric"",""" + _Mandatory.ToString + """,""False"")'"

Case ValidatorType.MinMax

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""MinMax"",""" + _Mandatory.ToString + """,""False"")'"

End Select

scrp2 += " </script> "

' Adds the control specific javascript

Page.ClientScript.RegisterStartupScript(Me.GetType(), Me.ID.ToString, scrp2)









End Sub



Private Sub Textbox2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Injects an onSubmit attribute into the form tag

Page.ClientScript.RegisterOnSubmitStatement(Me.GetType(), "Textbox2c",
"return CheckForError();")

End Sub



End Class
 
T

Teemu Keiski

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




Warren Patterson said:
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='#'
onclick=""alert(" + strMessage + ");"" alt='Click for more info.'
id='notify_" + Me.ID.ToString + "' name='notify_" + Me.ID.ToString +
"'><font color='red'>" + _ErrorChar + "</font></a>"

Else

sTextboxHtml = sTextboxHtml + "&nbsp;<img style=""display:none"" src='" +
_ErrorImagePath + "' onclick=""alert(" + strMessage + ");""
onmouseover=this.style.cursor='hand' alt='Click for more info.'
id='notify_" + Me.ID.ToString + "' name='notify_" + Me.ID.ToString + "'>"

End If

End If



HtmlTextWriter.Close()

output.Write(sTextboxHtml)

End Sub

Private Sub GenerateJS()



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

'**** ****

'**** This is code that is global for the whole page any textbox2 control
uses it****

'**** ****

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

Dim scrp As String = ""

scrp += " <script language='javascript'> "

scrp += " function focusAgain(objName) {"

scrp += " var objID = objName.id;"

scrp += " var obj = document.getElementById(objName.id);"

scrp += " obj.style.background = 'cyan';"

scrp += " }" 'End Function

' Checks for valid email addresses

scrp += " function chkEmail(AnArray) { "

scrp += " var obj;"

scrp += " obj = document.getElementById(AnArray.ID);"

scrp += " var checkStr = obj;"

scrp += " var str = checkStr.value;"

scrp += " if ((str.indexOf('.') > 2) && (str.indexOf('@') > 0) || (str ==
'')) "

scrp += " { "

scrp += " AnArray.Error = 'False';"

scrp += " } "

scrp += " else { "

scrp += " AnArray.Error = 'True';"

scrp += " }"

scrp += " } "

scrp += "function chkNumeric(AnArray,minval,maxval,comma,period,hyphen)"

'// only allow 0-9 be entered, plus any values passed

'// (can be in any order, and don't have to be comma, period, or hyphen)

'// if all numbers allow commas, periods, hyphens or whatever,

'// just hard code it here and take out the passed parameters

scrp += "{"

scrp += "var obj;"

scrp += "obj = document.getElementById(AnArray.ID);"

scrp += "var checkOK = '0123456789' + comma + period + hyphen;"

scrp += "var checkStr = obj;"

scrp += "var allValid = true;"

scrp += "var decPoints = 0;"

scrp += "var allNum = '';"

scrp += ""

scrp += "for (i = 0; i < checkStr.value.length; i++)"

scrp += "{"

scrp += "ch = checkStr.value.charAt(i);"

scrp += "for (j = 0; j < checkOK.length; j++)"

scrp += "if (ch == checkOK.charAt(j))"

scrp += "break;"

scrp += "if (j == checkOK.length)"

scrp += "{"

scrp += "allValid = false;"

scrp += "break;"

scrp += "}"

scrp += "if (ch != ',')"

scrp += "allNum += ch;"

scrp += "}"

scrp += " if ((!allValid) && (checkStr.value != ''))"

scrp += " {"

scrp += " AnArray.Error = 'True';"

scrp += " }"

scrp += " else {"

scrp += " AnArray.Error = 'False';"

scrp += " }"

scrp += "}" 'end function

scrp += " function chkMinMax(AnArray,minval,maxval) {"

scrp += " var obj;"

scrp += " obj = document.getElementById(AnArray.ID);"

scrp += " var checkOK = '0123456789';"

scrp += " var checkStr = obj;"

scrp += " var allValid = true;"

scrp += " var decPoints = 0;"

scrp += " var allNum = '';"

scrp += " for (i = 0; i < checkStr.value.length; i++)"

scrp += " {"

scrp += " ch = checkStr.value.charAt(i);"

scrp += " for (j = 0; j < checkOK.length; j++)"

scrp += " if (ch == checkOK.charAt(j))"

scrp += " break;"

scrp += " if (j == checkOK.length)"

scrp += " {"

scrp += " allValid = false;"

scrp += " break;"

scrp += " }"

scrp += " if (ch != ',')"

scrp += " allNum += ch;"

scrp += " }"

scrp += " var chkVal = allNum;"

scrp += " var prsVal = parseInt(allNum);"

scrp += " if (chkVal != '' && (prsVal < minval || prsVal > maxval)) "

scrp += " { "

scrp += " AnArray.Error = 'True';"

scrp += " } "

scrp += " else {"

scrp += " AnArray.Error = 'False';"

scrp += " }"

scrp += " }" 'end function

scrp += " function chkMandatory(AnArray) { "

scrp += " var obj;"

scrp += " obj = document.getElementById(AnArray.ID);"

scrp += " var checkStr = obj;"

scrp += " if (checkStr.value.length != 0) "

scrp += " { "

scrp += " AnArray.Error = 'False';"

scrp += " } "

scrp += " else { "

scrp += " AnArray.Error = 'True';"

scrp += " }"

scrp += " } "

' Set a variable up to be used to count errors (if errors > 0 then page
not allowed to submit)

scrp += " var strArray = '';"

scrp += " var intErrCount;"

scrp += " intErrCount = 0;"

' If more than 1 textbox2 is added to a form, we need to see if

' any of the textbox2's want the onSubmit message to be shown

scrp += " var intShowSubmitError = 0;"

scrp += "function objTextboxes(ID, Validator, Mandatory, Error) {"

scrp += " this.ID = ID;"

scrp += " this.Validator = Validator;"

scrp += " this.Mandatory = Mandatory;"

scrp += " this.Error = Error;"

scrp += "}"



'Buttons that submit a form on a page will only validate if the button's
onclick event has onclick='validate(true);'

scrp += "var blvalidate = true;"

scrp += "function validate(Validate) {"

scrp += " blvalidate = Validate;"

scrp += "}"



' Loops through all elemnts in the form to locate those startign with
'notify_' and then gets their

' visibility status, if they are visible, then there is an error on the
control, else, there is no error

scrp += "function CheckForError() { "

scrp += " if (blvalidate == true) {"

scrp += " var x = 'var myArray=new Array(' +
strArray.substr(1,strArray.length) + ');';"

scrp += " eval(x); "

'Loop through elements in array and check if they have errors on the form

scrp += " for (z=0; z<myArray.length; z++) {"

'Valid email

scrp += " if (myArray[z].Validator == 'Email') { "

scrp += " chkEmail(myArray[z]);"

scrp += " }"

'Is Numeric

scrp += " if (myArray[z].Validator == 'Numeric') { "

scrp += " chkNumeric(myArray[z],0,0,',','.','');"

scrp += " }"

'Is between min and max value

scrp += " if (myArray[z].Validator == 'MinMax') { "

scrp += " chkNumeric(myArray[z],0,0,',','.','');"

scrp += " if (myArray[z].Error == 'False')"

scrp += " {"

scrp += " chkMinMax(myArray[z]," + _minValue.ToString + "," +
_maxValue.ToString + ");"

scrp += " }"

scrp += " }"

'Has data

scrp += " if (myArray[z].Mandatory == 'True') { "

scrp += " if (myArray[z].Error == 'False')"

scrp += " {"

scrp += " chkMandatory(myArray[z]);"

scrp += " }"

scrp += " }"

scrp += " }"

' Loop through elements in array and check if they have errors in the
Error property, if an error, then

' display the error message.

scrp += " for (z=0; z<myArray.length; z++) {"

scrp += " if (myArray[z].Error == 'True') { "

scrp += " var objNotify;"

scrp += " objNotify = document.getElementById('notify_' + myArray[z].ID);"

scrp += " objNotify.style.display = '';"

scrp += " intErrCount = intErrCount + 1;"

scrp += " }"

scrp += " else {"

scrp += " var objNotify;"

scrp += " objNotify = document.getElementById('notify_' + myArray[z].ID);"

scrp += " objNotify.style.display = 'none';"

scrp += " "

scrp += " }"

scrp += " }"



scrp += " if ((intErrCount != 0)) {"

scrp += " if (intShowSubmitError != 0) {"

scrp += " alert('There is a problem with the form you are trying to
submit.'); "

scrp += " }"

'End If

'End If

scrp += " intErrCount = 0; "

scrp += " return false; "

scrp += " } "

scrp += " else { "

scrp += " return true;"

scrp += " }"



scrp += "}" 'End if (blvalidate == true)

scrp += "} "

scrp += " </script> "

' Adds the global javascript

Page.ClientScript.RegisterStartupScript(Me.GetType(), "textbox2", scrp)





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

'**** ****

'**** This is code that is control specific (Different for each control on
a page****

'**** ****

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

Dim scrp2 As String

scrp2 = " <script language='javascript'> "

If _ErrorMessageOnSubmitVisible Then

scrp2 += "intShowSubmitError = intShowSubmitError +1;"

End If

Select Case _validator

Case ValidatorType.None

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""None"",""" + _Mandatory.ToString + """,""False"")'"

Case ValidatorType.Email

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""Email"",""" + _Mandatory.ToString + """,""False"")'"

Case ValidatorType.Numeric

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""Numeric"",""" + _Mandatory.ToString + """,""False"")'"

Case ValidatorType.MinMax

scrp2 += "strArray = strArray + ',new objTextboxes(""" + Me.ID.ToString +
""",""MinMax"",""" + _Mandatory.ToString + """,""False"")'"

End Select

scrp2 += " </script> "

' Adds the control specific javascript

Page.ClientScript.RegisterStartupScript(Me.GetType(), Me.ID.ToString,
scrp2)









End Sub



Private Sub Textbox2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Injects an onSubmit attribute into the form tag

Page.ClientScript.RegisterOnSubmitStatement(Me.GetType(), "Textbox2c",
"return CheckForError();")

End Sub



End Class











LepardUK said:
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?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top