-
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
-
 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.. :-)
-
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
-
 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>
<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...
-
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
-
 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 02:40 AM.
-
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
-
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>
<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.. :-)
-
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
-
Replies: 1
Last Post: 06-12-2006, 07:34 PM
-
By Tazmania in forum VB Classic
Replies: 1
Last Post: 05-22-2006, 08:44 AM
-
Replies: 0
Last Post: 10-24-2001, 04:09 PM
-
By Steve J Golem in forum VB Classic
Replies: 2
Last Post: 12-09-2000, 10:38 PM
-
Replies: 1
Last Post: 09-14-2000, 11:04 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks