Validator not working when inside UpdatePanel

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I have a new validator (inherits from BaseValidator), but it does not always
work when it is inside an UpdatePanel. When I use it inside an update panel,
it works fine until I do the first successful submit. After the first
successful submit, the validator basically acts as if it is no longer there.
Why could this be happening? Any help would be appreciated. Thanks.
 
G

Gregory A. Beamer

I have a new validator (inherits from BaseValidator), but it does not
always work when it is inside an UpdatePanel. When I use it inside an
update panel, it works fine until I do the first successful submit.
After the first successful submit, the validator basically acts as if
it is no longer there. Why could this be happening? Any help would be
appreciated. Thanks.

Having not tried this, my assumption would be that the intiial state is
changed once the submit is done. This means you need a bit of JavaScript
to re-activate it.

To confirm this, I would consider using Firefox with Firebug, or
similar, to debug the AJAX.

For IE, you can use Fiddler, but it takes a bit more work as it is an
"external" tool.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
N

Nathan Sokalski

I have never used Firebug or Fiddler before, although I have heard of and
seen them so I don't expect to have any problem using them. However, because
my situation does not involve recieving an error, and I do not know exactly
what is getting changed when I submit, I really don't have any idea where to
start as far as what to look for. Can you help me get started? Thanks.
 
B

bruce barker

if your validator is in an update panel, then you have to code it
correctly. your validator is rendered as a span, and will be replaced
along with any other dom objects in the panel(s). this means you need to
rerun init code. if it registers any client script, it must use
ScriptManager to doit. if it references any $gets(), they must be redone.

-- bruce (sqlwork.com)
 
N

Nathan Sokalski

OK, I think that I have confirmed that is my problem. The init script that I
need to rerun is a script generated by ASP.NET to set the attributes I add
when overriding the AddAttributesToRender method inherited from
BaseValidator. This script looks something like the following:

var ctl00_cphExampleContent_ctl00 = document.all ?
document.all["ctl00_cphExampleContent_ctl00"] :
document.getElementById("ctl00_cphExampleContent_ctl00");
ctl00_cphExampleContent_ctl00.controltovalidate =
"ctl00_cphExampleContent_txtMinimum";
ctl00_cphExampleContent_ctl00.errormessage = "You must enter at least 5
characters.";
ctl00_cphExampleContent_ctl00.display = "Dynamic";
ctl00_cphExampleContent_ctl00.validationGroup = "minimum";

The script is obviously very simple, but because it is automatically
generated, how can I rerun it? Also, because this is a validator that I am
creating to be shared between multiple applications and users, I do not
actually know whether it will be inside an UpdatePanel or not, so I either
need to detect that as a condition in the validator or have code that works
in either scenario. Any ideas? Thanks.
 
B

bruce barker

inline script (writer.write) will not work because when the client
script in update panel sets the innerHTML, any script in the html is
ignored by the browser.

use ScriptManager.RegisterStartupScript to render the script. or as it
looks like you are only setting properties, use
ScriptManager.RegisterExpandoAttribute.

to work with update panels you must use ScriptManager, not the old
ClientScript.

-- bruce (sqlwork.com)
 
N

Nathan Sokalski

I'm sorry, I think I gave incorrect information in my original post. The
script does not set the attributes set in AddAttributesToRender, it sets
several expando attributes used by validators (which, as you could probably
see, in this case included controltovalidate, errormessage, display, and
validationGroup). But I think you misunderstood what I meant when I said the
script is generated by ASP.NET. I am not using ScriptManager OR ClientScript
directly; the script I showed you is added by the BaseValidator class in
code that I do not see (or at least I don't know where it is), I believe
that it is a script that is created regardless of what I do. Because I am
not the one who writes the code that adds this script, where can I go to fix
my problem (or is there a function and/or property that returns the script
that gets generated so that I can rerun the script at the appropriate
time?)? Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

bruce barker said:
inline script (writer.write) will not work because when the client script
in update panel sets the innerHTML, any script in the html is ignored by
the browser.

use ScriptManager.RegisterStartupScript to render the script. or as it
looks like you are only setting properties, use
ScriptManager.RegisterExpandoAttribute.

to work with update panels you must use ScriptManager, not the old
ClientScript.

-- bruce (sqlwork.com)


Nathan said:
OK, I think that I have confirmed that is my problem. The init script
that I need to rerun is a script generated by ASP.NET to set the
attributes I add when overriding the AddAttributesToRender method
inherited from BaseValidator. This script looks something like the
following:

var ctl00_cphExampleContent_ctl00 = document.all ?
document.all["ctl00_cphExampleContent_ctl00"] :
document.getElementById("ctl00_cphExampleContent_ctl00");
ctl00_cphExampleContent_ctl00.controltovalidate =
"ctl00_cphExampleContent_txtMinimum";
ctl00_cphExampleContent_ctl00.errormessage = "You must enter at least 5
characters.";
ctl00_cphExampleContent_ctl00.display = "Dynamic";
ctl00_cphExampleContent_ctl00.validationGroup = "minimum";

The script is obviously very simple, but because it is automatically
generated, how can I rerun it? Also, because this is a validator that I
am creating to be shared between multiple applications and users, I do
not actually know whether it will be inside an UpdatePanel or not, so I
either need to detect that as a condition in the validator or have code
that works in either scenario. Any ideas? Thanks.
 
B

bruce barker

the .net validators with 3.5 (webextensions.dll) were rewritten to use
ScriptManger. I don't know where your validator is from, but you need an
update panel aware one.

-- bruce (sqlwork.com)
 
N

Nathan Sokalski

I am using Visual Studio 2008 and ASP.NET 3.5, and my validator is one that
I wrote which inherits from BaseValidator (the validator was written using
Visual Studio 2008 and ASP.NET 3.5 as well). Any more suggestions or ideas?
Thanks.
 
G

Gregory A. Beamer

I have never used Firebug or Fiddler before, although I have heard of
and seen them so I don't expect to have any problem using them.
However, because my situation does not involve recieving an error, and
I do not know exactly what is getting changed when I submit, I really
don't have any idea where to start as far as what to look for. Can you
help me get started? Thanks.

Video or text?

There are quite a few links that talk about debugging AJAX on this
Google search:
http://snurl.com/tlfyg

The top link is a You Tube video that shows how to do it.

The search (above) is where I would start. if that yields nothing, then
we can look at other options.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
N

Nathan Sokalski

Actually, I think I have found the source of the problem, as you will see in
the response from bruce barker (not that I didn't appreciate yours or plan
on trying yours also). The problem is the script generated by BaseValidator
to set some of the expando attributes for the span tag which is the
validator (such as controltovalidate, errormessage, display,
validationGroup, etc.). The script works fine, but it needs rerun after the
UpdatePanel does one of it's asynch postbacks, which I am having trouble
figuring out how to do, since the script is generated and added completely
automatically. Any ideas (or if you think the problem could be something
else)? Thanks.
 
E

Eric M

I'm having the exact same issue... That code is not being re-run on
the asynch post-back, so when I access the property in client code, it
is undefined.
 
N

Nathan Sokalski

I found some stuff that says there are updated versions of the validators
for AJAX that fix this, but those posts were from 2007, and not only is this
over 2 years later, but I did my stuff with ASP.NET 3.5, and I always get
all the stuff from WindowsUpdate right away (it says the updates will be
provided through WindowsUpdate). Here is where I got that info:

http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx

I don't know if the article is wrong or if I don't have all the updates I
think I do, but maybe this will help us fix everything. Hopefully.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

I'm having the exact same issue... That code is not being re-run on
the asynch post-back, so when I access the property in client code, it
is undefined.
 
J

Jay Woerndle

I know this is an old thread, but I just spent 2 days trying to make this work and thought I would save someone else the pain of getting a proper solution. I used older code that I found on the net for a CheckBoxList validator and it worked until the a button was clicked that did an asynchronous update of the updatepanel. The validator I was using also extended the basevalidator class and did an override of the AddAttributesToRender function as well as the OnPreRender function.

What I did to fix it was to replace code in both functions. In OnPreRender, I replaced
Page.ClientScript.RegisterStartupScript(GetType(), "evaluationfunction", javascriptString, true);
with the newer function
ScriptManager.RegisterStartupScript(this, GetType(), "evaluationfunction", javascriptString, true);

In AddAttributesToRender I replaced two functions that look like this:
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "javascriptFunctionName", false);
with the newer function
ScriptManager.RegisterExpandoAttribute(this, this.ClientID, "evaluationfunction", "javascriptFunctionName", false);
where javascriptFunctionName is the actual name of the javascript function that will be embedded to the aspx page.

Like Bruce said above, these functions are suited to the UpdatePanel where the old ones were not.

I have a new validator (inherits from BaseValidator), but it does not always
work when it is inside an UpdatePanel. When I use it inside an update panel,
it works fine until I do the first successful submit. After the first
successful submit, the validator basically acts as if it is no longer there.
Why could this be happening? Any help would be appreciated. Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/
On Friday, December 04, 2009 3:04 PM Gregory A. Beamer wrote:
Having not tried this, my assumption would be that the intiial state is
changed once the submit is done. This means you need a bit of JavaScript
to re-activate it.

To confirm this, I would consider using Firefox with Firebug, or
similar, to debug the AJAX.

For IE, you can use Fiddler, but it takes a bit more work as it is an
"external" tool.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
On Saturday, December 05, 2009 12:48 AM Nathan Sokalski wrote:
OK, I think that I have confirmed that is my problem. The init script that I
need to rerun is a script generated by ASP.NET to set the attributes I add
when overriding the AddAttributesToRender method inherited from
BaseValidator. This script looks something like the following:

var ctl00_cphExampleContent_ctl00 = document.all ?
document.all["ctl00_cphExampleContent_ctl00"] :
document.getElementById("ctl00_cphExampleContent_ctl00");
ctl00_cphExampleContent_ctl00.controltovalidate =
"ctl00_cphExampleContent_txtMinimum";
ctl00_cphExampleContent_ctl00.errormessage = "You must enter at least 5
characters.";
ctl00_cphExampleContent_ctl00.display = "Dynamic";
ctl00_cphExampleContent_ctl00.validationGroup = "minimum";

The script is obviously very simple, but because it is automatically
generated, how can I rerun it? Also, because this is a validator that I am
creating to be shared between multiple applications and users, I do not
actually know whether it will be inside an UpdatePanel or not, so I either
need to detect that as a condition in the validator or have code that works
in either scenario. Any ideas? Thanks.
Submitted via EggHeadCafe - Software Developer Portal of Choice
Book Review: Excel 2010 - The Missing Manual [OReilly]
http://www.eggheadcafe.com/tutorial...w-excel-2010--the-missing-manual-oreilly.aspx
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top