Change of state in reponse to the click event of a button.

G

Greg

I have a button on my form that I am using to allow the user to
validate the contents of a certain textbox.
Depending on the results of the validation, I need to change the
backcolour of the text box and make a call to the database, and
populate several controls with the result from this call to the
database.
However, since the click event is fired too late in the page cycle, it
is too late for the changes made to the controls to actually appear on
the form - these changes will only appear the next time a postback
occurs.

What is the best solution to this? Force another postback once I have
received data from the database, or to do some Javascript work to
change the values from the client side ( I assume this is possible,
but correct me if I'm wrong!).

Regards,

Greg.
 
E

Eliyahu Goldin

Greg,

Apparently you are setting up the controls in Page_Load event. You need
someone to tell you that the button was clicked. One of the ways is to
introduce a hidden input element and set it on client side when the button
is clicked. You can access the hidden field in the Page_Load.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
M

Mark Rae

Force another postback once I have received data from the database,

Generally speaking, the fewer roundtrips to the server and back that your
app needs to do, the better it will perform...
or to do some Javascript work to change the values from the client side
( I assume this is possible, but correct me if I'm wrong!).

Almost certainly.

<script type="text/javascript">
function validateForm()
{
if(...some client-side test fails...)
{
return false;
}
}
</script>


<asp:Button ID="MyButton" OnClick="MyButton_Click" OnClientClick="return
validateForm();" Text="Test" />

When the user clicks MyButton, it will run the JavaScript function. If the
JavaScript functions returns false, the button will not proceed with the
postback.
 
G

Greg

Thanks Eliyahu and Mark,

That worked a treat! This has been annoying me for a long time, I'm so
pleased I got it sorted!

Greg.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top