newb: client script with asp.net

S

sklett

I need to add extensive validation and interaction client scripting to a web
form. I've done some initial searches for "asp.net and client scripting"
and I've found a couple articles that show methods like "register script
block" and what not. None of these methods will do what I need, I need to
add onChange, onClick, etc events to my controls.

For a simple example: How would I pop an alert when a user clicks on a
server textbox control?

Is this possible?

Thanks for any help,
Steve
 
J

JeffP@Work

Steve,

Yes.

There are two aspects, one is Page validation and the other is validation of
the control.

I personally prefer to handle page validation with a "Can I Process?" sProc
so I can apply complex validation, that can change with scope creep and I
often don't have access to production WebServers, but do to databases, so I
can adapt my sProc business rules in the sProc's w/out having to rebuild the
project.

Anyway I've digressed....

Control events and validation are done with <asp:RequiredFieldValidator.....

Here's a textbox for entering the user's ID and on keyboard "Enter" posts,
this combined with the setFocus script makes logging in easy....

<asp:textbox id="tbId" onkeydown="if ((event.which &amp;&amp; event.which ==
13) ||
(event.keyCode &amp;&amp; event.keyCode == 13))
{document.Form1.btnLogin.click();return false;} else return true;"
runat="server" ToolTip="Enter Your Assigned ID" Width="126px"></asp:textbox>

or a more simple textbox....
<asp:textbox id="tbId" runat="server" ToolTip="Enter Your Assigned ID"
Width="126px"></asp:textbox>

<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="tbId" Display="Dynamic"
ErrorMessage="You must specify a ID.">*</asp:requiredfieldvalidator>

Here is where the error msg displays, setting the validator to Dynamic
allows the error to hide this lable until re-evaluated.
<asp:Label id="lblInvalidLogin" runat="server" ForeColor="Red"
Visible="False">The ID entered was not valid.</asp:Label>

Regarding your registering a script, here's a call so that the cursor is in
the ID field in the pageLoad event

If Not Page.IsStartupScriptRegistered("LoginFocusScript") Then
Page.RegisterStartupScript("LoginFocusScript", Me.setLoginFocusScript)

HTH

JeffP....
 
S

sklett

Jeff, thank you for the great response. I realize now that you can simply
add javascript to an asp server control directly in the HTML!

I was over complicating things :0)
 
J

JeffP@Work

NP

Also, the * asterisk is used as a tiny unassuming placeholder for the
lable....

JeffP....
 

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

Top