2 Replies - 8484 Views - Last Post: 21 September 2011 - 08:03 AM

#1 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

How to create a validator based static member dropdown list

Posted 17 August 2011 - 05:19 AM

Basically I have created a dropdown list and a textbox


The textbox has a range validator on it and that is working

basically i want to have the textbox activate when the third option is selected only


which looks like so

Option 1 car
option 2 Ute
Option 3 Motor Cycle

Which has no Datasource as it is a static list of these options only

so i was thinking of using if dropdownlist1.items3.selected

textbox.active = true to enable it to work only when the correct option is selected


Or would it be more logical to while items!=3.selected

ErrorMessage && textbox.hidden
i have just started today with asp


what do i need to do to achieve this and would i need to select not checking for empty text while it is hidden or would this work as is


I know a little Javascript and can guess at it though im not 100% sure on my method


Any Help is Greatly Appreciated Thanks

This post has been edited by jaimesharp: 17 August 2011 - 09:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to create a validator based static member dropdown list

#2 jaimesharp  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 27-May 09

Re: How to create a validator based static member dropdown list

Posted 19 September 2011 - 02:45 AM

Basically I have added a database to this to store the values now i want to compare the selected item so that if the value is not equal to motor cycle it should disable a textbox which can be called textbox for now so that i can get to a button to change views as this is a multiview page


Otherwise something like if <> DropDownList1().items3.selected then Disable remaining elements and validators how to i write this as i have never done this


If DropDownList1.SelectedValue = "Motor Cycle" the elements afterwards on the view

or i can leave it static without the database as there are only 3 members like so

<asp:ListItem Value="0">Motor Car</asp:ListItem>
<asp:ListItem Value="1">Ute/Light Goods Vehicle</asp:ListItem>
<asp:ListItem Value="2">Motor Cycle</asp:ListItem>
</asp:DropDownList>

Would i create classes and disable all members of that class

Any Help is greatly Appreciated

This post has been edited by jaimesharp: 19 September 2011 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: How to create a validator based static member dropdown list

Posted 21 September 2011 - 08:03 AM

What you need to do is call some Javascript when the last option DropDownList is selected.
First you need to write the Javascript that disables the TextBox.

This is easy enough but first you need to know a few things about writing Javascript that works with ASP.NET objects.

As you know, ASP.NET objects (like TextBoxes, DropDownLists, Labels etc etc) are rendered as HTML elements so that the browser can display them (for example a TextBox is render as an HTML <input type="text" /> element).

ASP.NET generates valid HTML so that it is displayed properly...in order to do this it gives each HTML element rendered an "id". This ID is known as the ClientID and it is based on the ID that you give to the ASP.NET control. You can access the ClientID in your server side code (your C# or VB.NET code) using the ASP.NET object's ClientID property.

With this in mind, you can create Javascript that access the correct HTML element for your ASP.NET object.

You could create the following Javascript function to enable the TextBox:
function EnableTextBox(textboxID, isEnabled)
{
  var textboxElement = document.getElementById(textboxID);
  if textboxElement
  {
    textboxElement.disabled = !isEnabled;
    textboxElement.readonly = isEnabled;
  }
}


You would have to pass the ClientID of the ASP.NET TextBox into this method.
So...how do you do that?

Well you need to make the "Motorcycle" DropDownList option call this method when it is clicked in the browser. You have to add the Javascript "onclick" code to the list item.

So, maybe in your Page PreRender Event, access the motorcycle list item and add the "onclick" attribute to it:

(VB.NET)
  dropdownlist1.Items(2).Attributes.Add("onclick","EnableTextBox('" + motorcycleTextBox.ClientID + "', true);")


In reality you're probably going to want to call the Javascript code for every list item...the motorcycle list item will be the only one that will pass "true" as the IsEnabled parameter to the Javascript function:

(VB.NET)
  For Each li In dropdownlist1.Items
    If li.Text = "Motorcycle"
      li.Attributes.Add("onclick","EnableTextBox('" + motorcycleTextBox.ClientID + "', true);")
    Else
      li.Attributes.Add("onclick","EnableTextBox('" + motorcycleTextBox.ClientID + "', false);")
    End If
  Next


That should get you started...there is a way to write the ClientID directly into the Javascript but this is only possible if the Javascript is part of the page etc etc.

-Frinny

This post has been edited by Frinavale: 21 September 2011 - 09:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1