Custom OnChange event

J

JohnnyV

I am creating a custom textbox control
I need to call a custom function on the client side instead of the
__doPostBack() function provided by the Framework.
I tried the code
Attributes.Add("onchange","return OnChangeEventHandler(event)");
in the OnPreRender event of the custom control, but the result html on the
client side for the textbox included the onchange as
onchange="return OnChangeEventHandler(event)__doPostBack('CtrTextBox1','')"
and this is a problem.
How to avoid the __doPostBack?

Regards
 
P

Paul Rees

Hi J

One way you could do it is to take advantage of the Validation structure in .NET. Validation occurs first on the client side, so that if the validation criteria are not met, it won't post back the page. Then when the page is posted back, it validates again for two reasons:
1. to ensure that someone isn't hacking
2. in the case that client side validation has been disabled (a user can get trapped by validation so there is a way to turn it off eg. onclick="Page_ValidatationActive=false;" so that the page can be posted back)

This isn't perfect if you want to validate immediately on change

With a CustomValidator you can create whatever validation you want in the form of your own functions, both on the client and server side. It is V good practice to make sure that both functions do the same thing though. If you setup a custom validator to control your textbox validation you can set the ClientValidationFunction property and give it the name of a Javascript function in the HTML. You must setup your JScript function by passing the variables source and arguments (see below). Set the arguments.IsValid variable based on the result of your validation

Also set the ServerValidate event procedure to a server function that validates the data in a similar manner

------------------------------------
CLIENT VALIDATION FUNCTION
------------------------------------

<script language="jscript"
function ClientValidate(source, arguments

var strValue
strValue= arguments.Valu

if (IsNumeric(strValue)==true)
arguments.IsValid = true
return true

arguments.IsValid = false
return false


function IsNumeric(strString)
// check for valid numeric string
var strValidChars = "0123456789.-"
var strChar
var blnResult = true

if (strString.length == 0) return false

// test strString consists of valid characters listed abov
for (i = 0; i &lt; strString.length &amp;&amp; blnResult == true; i++)
strChar = strString.charAt(i)
if (strValidChars.indexOf(strChar) == -1)
blnResult = false


return blnResult

</script

------------------------------------
SERVER VALIDATION FUNCTIO
------------------------------------

Private Sub CustomValidator1_ServerValidate
(ByVal source As System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs)
Handles CustomValidator1.ServerValidat

Dim strValue as strin
Tr
' Get value from ControlToValidate (passed as args
strValue = args.Valu
If IsNumeric(strValue) The
args.IsValid = Tru
Retur
End I
' Number is not Numeric, return False
args.IsValid = Fals
Retur
Catch ex As Exceptio
' If there was getting the value, return False
args.IsValid = Fals
Retur
End Tr
End Su


Hope this is of some help to you
PR
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top