anyone good with javascript?

G

Guest

Hi I have 3 drop down list boxes on a webform with a submit button. When the
submit button is selected if any of the dropdown listbox selected index = 0 I
want to popup a message box and not allow the submit to work. Anyhow
thinking this may require javascript.
thanks.
 
P

Phillip Ian

Assuming your lists are DropDownList1, 2 and 3, and your button is
btnOk...

In page_load:

btnOk.Attributes.Add("onClick", "javascript:return validateOk()")
Dim s As String = ""
s &= vbCrLf & "<script language='javascript'><!--" & vbCrLf
s &= vbTab & "function validateOk() {" & vbCrLf
s &= vbTab & vbTab & "if (document.Form1." & DropDownList1.ClientID
& ".selectedIndex == 0) { " & vbCrLf
s &= vbTab & vbTab & vbTab & "alert('Choose from the first
dropdown!');" & vbCrLf
s &= vbTab & vbTab & vbTab & "return false;" & vbCrLf
s &= vbTab & vbTab & "}" & vbCrLf
s &= vbTab & vbTab & "if (document.Form1." & DropDownList2.ClientID
& ".selectedIndex == 0) { " & vbCrLf
s &= vbTab & vbTab & vbTab & "alert('Choose from the second
dropdown!');" & vbCrLf
s &= vbTab & vbTab & vbTab & "return false;" & vbCrLf
s &= vbTab & vbTab & "}" & vbCrLf
s &= vbTab & vbTab & "if (document.Form1." & DropDownList3.ClientID
& ".selectedIndex == 0) { " & vbCrLf
s &= vbTab & vbTab & vbTab & "alert('Choose from the third
dropdown!');" & vbCrLf
s &= vbTab & vbTab & vbTab & "return false;" & vbCrLf
s &= vbTab & vbTab & "}" & vbCrLf
s &= vbTab & vbTab & "return true;" & vbCrLf
s &= vbTab & "}" & vbCrLf
s &= "--></script>" & vbCrLf
Page.RegisterClientScriptBlock("OkButtonVal", s)

You can, of course, omit a lot of the vbTab and vbCrLf stuff...I just
throw it in for readability when debugging.
 
J

jasonkester

First of all, Yikes!

What's stopping you from putting that Script block in the HTML, where
it belongs? In fact, that whole thing should be client side:

<input type="submit" onclick="return validateOk();" runat=server/>

<script>
// decode all that script above and put it here.
</script>

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
P

Phillip Ian

My only excuse is that I work with lots of user controls...thus I need
to use .ClientID rather than the actual name of the component.

If it's all on the page, I guess no reason. :)

-Phil
 
J

jasonkester

Sounds reasonable enough. Though I'd probably still try to only spit
out as much code from the server as necessary:

Dim handleScript As String = String.Format("var boxHandle = {0};",
box.ClientID);
Page.RegisterClientScriptBlock­(handleScript) ;

<script>
function box_IndexChanged
{
var box = document.getElementByID(boxHandle);
...
}
</script>


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top