Listbox validation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Listbox validation

  1. #1
    Join Date
    Jan 2006
    Posts
    12

    Listbox validation

    Hi,
    I have countries listbox, for instance if i choose singapore listbox it should display the corresponding states listbox of singapore, if i chose anyother country it should display a label saying "ENTER STATE" followed by a blank textbox. Anybody have sample coding for this. Thank You.

    Balen

  2. #2
    Join Date
    Dec 2004
    Posts
    717
    Quote Originally Posted by balan
    Hi,
    I have countries listbox, for instance if i choose singapore listbox it should display the corresponding states listbox of singapore, if i chose anyother country it should display a label saying "ENTER STATE" followed by a blank textbox. Anybody have sample coding for this. Thank You.
    Balen
    Im not very sure that I got you.. cuz' i think ur req is a lit bit strange.
    Let me tell you what I understand so far.
    1) There are two listbox controls on the webform.
    2) if the user choose singapore listbox it should display the corresponding states listbox of singapore.
    3) Then, If the user choose other country, one label with text "ENTER STATE" and one textbox will be shown..

    Is that your requirement?

    Oki.. anyway..
    If Yes, You can achieve with the following way.
    1) Put two listboxes in webform.and one lable and one textbox.
    Let's say.. Listbox1 for Countries and Listbox2 for state.
    Code:
    <asp:ListBox ID="ListBox1" runat="server" Width="150px" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
                <asp:ListItem Value="1"></asp:ListItem>
                <asp:ListItem Value="2"></asp:ListItem>
            </asp:ListBox>
            <asp:ListBox ID="ListBox2" runat="server" Width="150px">
            </asp:ListBox>
    <asp:Label ID="Label1" runat="server" Text="ENTER STATE" Visible="False"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) Cache["prev"] = "";
    }    
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) {
        if (ListBox1.SelectedIndex.ToString() != Cache["prev"] && Cache["prev"] != "") {
            Label1.Visible = true;
            TextBox1.Visible = true;
            return;
        }
        else {
            Label1.Visible = false;
            TextBox1.Visible = false;
            Cache["prev"] = ListBox1.SelectedIndex;
        }
        switch (ListBox1.SelectedIndex) {
            case 0:
                ListBox2.Items.Clear();
                ListBox2.Items.AddRange(new ListItem[] { new ListItem("AA"),
                                                         new ListItem("BB"),
                                                         new ListItem("CC")});
                break;
            case 1:
                ListBox2.Items.Clear();
                ListBox2.Items.AddRange(new ListItem[] { new ListItem("DD"),
                                                         new ListItem("EE"),
                                                         new ListItem("FF")});
                break;
        }
    }
    That's all..
    Hope it would help..
    Feel free to let us know if it's not what you want.. :-)
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  3. #3
    Join Date
    Jan 2006
    Posts
    12
    Hi,
    Thank you for trying to sovle my problem. Ur understanding is right, but i need it in javascript, hope u have given it in asp. Sorry i did not mention it in my prev thread.

    Balen

  4. #4
    Join Date
    Dec 2004
    Posts
    717
    Quote Originally Posted by balan
    Hi,
    Thank you for trying to sovle my problem. Ur understanding is right, but i need it in javascript, hope u have given it in asp. Sorry i did not mention it in my prev thread.

    Balen
    Oki. Balen.. I've done for you.. Check it out.

    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <meta name="GENERATOR" content="Microsoft FrontPage 6.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>New Page 1</title>
    <script language="javascript">
    
     function loadStates(){
       var oLst1 = document.getElementById("Select1");
       var oLst2 = document.getElementById("Select2");
       
       if(document.cookie != "" && document.cookie != oLst1.selectedIndex){
          lbl.style.visibility="visible";
          txt.style.visibility="visible";
    
       }
       else{
         lbl.style.visibility="hidden";
         txt.style.visibility="hidden";
         document.cookie = oLst1.selectedIndex;
       }
       //= oLst1.selectedIndex;
          
       RemoveAll(oLst2);
       switch(oLst1.selectedIndex){
         case 0: //Myanmar
              AddItem(oLst2, "AAA", "a");
              AddItem(oLst2, "BBB", "a");
              break;
         case 1: //India
              AddItem(oLst2, "UP", "up");
              AddItem(oLst2, "BB", "b");
              break;
         case 2: //US
              AddItem(oLst2, "NK", "nk");
              AddItem(oLst2, "CA", "ca");
              break;
         case 3: //UK
              AddItem(oLst2, "LD", "ld");
              AddItem(oLst2, "CK", "ck");
    		  break;
    
       }
     }
     function RemoveAll(objListBox){
        for(var i=objListBox.children.length-1;i>=0;i--)
          objListBox.remove(i);
     }
     function AddItem(objListBox, strText, strId){
      var newOpt;
      newOpt = document.createElement("OPTION");
      newOpt = new Option(strText,strText);
      newOpt.id = strId;
      objListBox.add(newOpt);
     }
    </script>
    </head>
    
    <body>
    <SELECT id="Select1" name="Select1" size="3" runat="server" style="width: 168; height: 104" onclick="loadStates();">
      <OPTION>Myanmar</OPTION>
      <OPTION>India</OPTION>
      <OPTION>US</OPTION>
      <OPTION>UK</OPTION>
    </SELECT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <SELECT id="Select2" name="Select2" size="3" runat="server" style="width: 168; height: 106">
    </SELECT>
    <p>
    <div id="lbl" style="visibility:hidden">ENTER STATE</div>
    <input id="txt" type=text style="visibility:hidden"></input>
    
    </body>
    </html>
    .

    More ~
    http://michaelsync.wordpress.com/200...-from-listbox/

    Hopefully, it would help...
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  5. #5
    Join Date
    Jan 2006
    Posts
    12
    Hi,
    If i choose "Myanmar" alone the textbox should come and if i am clicking other than myanmar textbox should not appear. But now whatever the country i click the textbox is displaying. How to solve this?

    Balan

  6. #6
    Join Date
    Dec 2004
    Posts
    717
    Quote Originally Posted by balan
    Hi,
    If i choose "Myanmar" alone the textbox should come and if i am clicking other than myanmar textbox should not appear. But now whatever the country i click the textbox is displaying. How to solve this?

    Balan
    That's why I was telling you that your requirement is strange. but you said that Im right..

    Let me tell you what I understand.
    1) There will be four textboxes if there are four countries.
    2) If the user click a particular country then the text related to the selected country should be shown..
    eg: If the user click "Myanmar", the textbox for Myanmar will be shown.
    If the user click "India", the textbox for Myanmar will be disappeared and the textbox for India will be displayed..

    Is this what you want?

    Why do you wanna have "lbl" and "textbox"?

    If we know the details of the thing that you want, we can help you more.
    It would be great if you tell us the details steps by steps...
    Last edited by Sync; 07-07-2006 at 03:40 AM.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  7. #7
    Join Date
    Jan 2006
    Posts
    12
    Hi,
    Sorry if i had confused earlier. I am listing my requirments in steps.

    1. I have only 1 listbox which contains all countries list

    2. If i choose UK it should display another listbox parallel to countries listbox which contains only UK states. This is applied only for uk not for other countries.

    3. If i click any countries other than UK it should display a textbox saying enter the state name. When i click the countries other than UK the above states listbox should be disappeared or disabled.

    Balen

  8. #8
    Join Date
    Dec 2004
    Posts
    717
    1. I have only 1 listbox which contains all countries list

    2. If i choose UK it should display another listbox parallel to countries listbox which contains only UK states. This is applied only for uk not for other countries.

    3. If i click any countries other than UK it should display a textbox saying enter the state name. When i click the countries other than UK the above states listbox should be disappeared or disabled.
    Okay.. Balan. Check it out below.
    If you click UK, the state listbox will be shown. (no textbox, no label at that time)
    If you click other countries, the state listbox will be invisibled.
    and lbl & txt will be shown.

    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>New Page 1</title>
    <script language="javascript">
    
     function loadStates(){
       var oLst1 = document.getElementById("Select1");
       var oLst2 = document.getElementById("Select2");   
    
       if(oLst1.selectedIndex == 3){
          Select2.style.visibility="visible";
          lbl.style.visibility="hidden";
          txt.style.visibility="hidden";
       } 
       else{
          lbl.style.visibility="visible";
          txt.style.visibility="visible";
          Select2.style.visibility="hidden";
       }   
     } 
    </script>
    </head>
    
    <body>
    <SELECT id="Select1" name="Select1" size="3" runat="server" style="width: 168; height: 104" onclick="loadStates();">
      <OPTION>Myanmar</OPTION>
      <OPTION>India</OPTION>
      <OPTION>US</OPTION>
      <OPTION>UK</OPTION>
    </SELECT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <SELECT id="Select2" name="Select2" size="5" runat="server"  style="width: 168; height: 104;visibility:hidden">
      <OPTION>UK State 1</OPTION>
      <OPTION>UK State 2</OPTION>
      <OPTION>UK State 3</OPTION>
    </SELECT>
    <p>
    <div id="lbl" style="visibility:hidden">ENTER STATE</div>
    <input id="txt" type=text style="visibility:hidden">
    
    </body>
    Hope this is what you want.. :-)
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  9. #9
    Join Date
    Jan 2006
    Posts
    12
    Hi Michael,
    Your coding works fine and this is the output i was expecting. Thank You for helping me to solve this problem. Thank You.

    Balan

Similar Threads

  1. how to bind the listbox and textbox
    By JenHu in forum .NET
    Replies: 1
    Last Post: 06-12-2006, 08:34 PM
  2. Validation for Data control event
    By Tazmania in forum VB Classic
    Replies: 1
    Last Post: 05-22-2006, 09:44 AM
  3. Replies: 0
    Last Post: 10-24-2001, 05:09 PM
  4. ListBox API
    By Steve J Golem in forum VB Classic
    Replies: 2
    Last Post: 12-09-2000, 11:38 PM
  5. Validation
    By Marcia in forum Java
    Replies: 1
    Last Post: 09-14-2000, 12:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center