can anybody tell me how do u validate a listbox to select atleast one item from it. I have a listbox within the template field of a grid view. and these listboxes can vary from 1 to 4. how can i know how many listboxes are present so that i can loop in javascript.
7 15000 acoder 16,027
Recognized Expert Moderator MVP
Welcome to Bytes!
I assume you mean a multiple select. You can validate it by looping over the options (see example) and checking how many are selected (check the selected property).
To get all the list boxes in a page, use document.getEle mentsByTagName( "select") and loop over the resulting array.
thanks for the reply.. i have drop down and lsit boxes in my page.. these listboxes are dynamic and can vary from 1 to 4.
as u said i have used document.getEle mentsByTagName( "select") to get all the references.. this is working fine for the dropdown, when it comes to list box ... instead of selecting the labeldesc of the lisbox it takes the values of the list box.
acoder 16,027
Recognized Expert Moderator MVP
Can you show the code for the list box. How does it differ from a drop-down?
This is the Java script functin for validating the drop down.. listbox n text box... -
function ValidateSave()
-
{
-
var lblSelectClient = document.getElementById('<%=ddlSelectClient.ClientID%>');
-
if(lblSelectClient.selectedIndex==0)
-
{
-
alert('Please select a client');
-
return false;
-
}
-
-
var ddlSelectHierarchy = document.getElementById('<%=ddlSelectHierarchy.ClientID%>');
-
if(ddlSelectHierarchy.selectedIndex==0)
-
{
-
alert('Please select a hierarchy');
-
return false;
-
}
-
-
var hdMode = document.getElementById('<%=hdMode.ClientID%>');
-
var isSelected = false;
-
var hasList = false;
-
var strLevel="";
-
//if(hdMode.value="HierEdit")
-
{
-
var dropdownReferences = document.getElementsByTagName("select");
-
-
if(dropdownReferences !=null)
-
{
-
if(dropdownReferences.length != null)
-
{
-
for (i = 0; i <dropdownReferences.length-1; i++)
-
{
-
if(dropdownReferences[i].id.search("LstHierA")==-1)
-
{
-
if(dropdownReferences[i].selectedIndex==0)
-
{
-
var level = document.getElementById(replace(dropdownReferences[i].name.replace("ddlLevel","lblLevelDesc"),"$","_")).innerText;
-
if(level.search("Select")!=-1)
-
{
-
level=level.replace("Select ","");
-
}
-
-
alert('Please select a value for '+level);
-
return false;
-
}
-
}
-
else
-
{
-
hasList = true;
-
strLevel = document.getElementById(replace(dropdownReferences[i].name.replace("lstHierA","lblLevelDesc"),"$","_")).innerText;
-
if(strLevel!="")
-
{
-
strLevel=strLevel.replace("Select ","");
-
}
-
for(j=0;j<dropdownReferences[i].length;j++)
-
{
-
if(dropdownReferences[i].options[j].selected)
-
{
-
-
isSelected=true;
-
break;
-
}
-
}
-
}
-
}
-
}
-
}
-
-
var inputReferences= document.getElementsByTagName("input");
-
-
if (inputReferences != null)
-
{
-
if (inputReferences.length != null)
-
{
-
for (i = 0; i <= inputReferences.length-1; i++)
-
{
-
if (inputReferences[i].type == 'text')
-
{
-
if (inputReferences[i].value == null || inputReferences[i].value == "")
-
{
-
-
var level = document.getElementById(replace(inputReferences[i].name.replace("txtEnterNewLevel","lblLevelDesc"),"$","_")).innerText;
-
if(level.search("Select")!=-1)
-
{
-
level=level.replace("Select ","");
-
}
-
else
-
{
-
level = document.getElementById(replace(inputReferences[i].name.replace("txtLeafNode","lblLevelDesc"),"$","_")).innerText;
-
if(level.search("Enter")!=-1)
-
{
-
level=level.replace("Enter ","");
-
}
-
}
-
-
alert('Please enter a value for '+level);
-
inputReferences[i].focus();
-
return false;
-
-
}
-
var Ok;
-
OK=CheckLength(inputReferences[i]);
-
if(!OK)
-
{
-
return false;
-
}
-
}
-
}
-
}
-
}
-
}
-
if(hasList)
-
{
-
if(!isSelected)
-
{
-
if(strLevel!="")
-
{
-
alert('Please select a '+strLevel);
-
return false;
-
}
-
}
-
}
-
-
return true;
-
}
-
-
lstHierA is the name of the listbox
acoder 16,027
Recognized Expert Moderator MVP
OK, but what about the actual list box code (view source at the client to see how it's rendered in HTML).
- <asp:GridView ID="grdLevels" runat="server" AutoGenerateColumns="False" Width="750px" OnRowDataBound="grdLevels_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" EnableViewState="true">
-
<Columns>
-
<asp:TemplateField HeaderText="LevelDesc" ShowHeader="False">
-
<ItemTemplate>
-
<asp:Label ID="lblLevelDesc" Width="150px" runat="server" Text='<%# Bind("LevelDesc") %>'></asp:Label>
-
</ItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField ShowHeader="False" HeaderText="LevelControls">
-
<ItemTemplate>
-
<asp:DropDownList ID="ddlLevel" Width="200px" Enabled="false" OnSelectedIndexChanged="ddlLevel_SelectedIndexChanged" runat="server" AutoPostBack="true" AppendDataBoundItems="true">
-
<asp:ListItem Selected="True" Value="0">--Select--</asp:ListItem>
-
</asp:DropDownList>
-
<asp:TextBox ID="txtLeafNode" runat="server"></asp:TextBox>
-
-
-
-
<div id='divList1' runat="server" style="Z-INDEX: 102;OVERFLOW:auto; WIDTH: 247px;height:90px" >
-
-
<asp:ListBox ID="LsthierA" SelectionMode="Multiple" Width="700px" runat="server" Rows="5" Height="10000">
-
</asp:ListBox>
-
-
</div>
-
-
-
<asp:ListBox ID="lstLevel" Width="200px" Visible="false" runat="server"></asp:ListBox>
-
<asp:HiddenField ID="hdlLevelKey" Value='<%#DataBinder.Eval(Container.DataItem,"LevelKey") %>' runat="server" />
-
<asp:HiddenField ID="hdIsLeafNode" Value='<%#DataBinder.Eval(Container.DataItem,"IsLeafNode") %>' runat="server" />
-
<asp:HiddenField ID="hdIsRoot" Value='<%#DataBinder.Eval(Container.DataItem,"IsRoot") %>' runat="server" />
-
<asp:HiddenField ID="hdDDLSelection" runat="server"/>
-
<asp:HiddenField ID="hdHierKey" runat="server"/>
-
-
<asp:TextBox ID="txtEnterNewLevel" Width="195px" Visible="false" runat="server" Enabled="false"></asp:TextBox>
-
</ItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="NewLevelButton">
-
<ItemTemplate>
-
<asp:Button ID="btnNewLevel" OnClick="btnNewLevel_Click" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"LevelDesc") %>' />
-
</ItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="AttributeValueButton">
-
<ItemTemplate>
-
<asp:Button ID="btnAtVal" runat="server" Text="Add Attribute Value" OnClick="btnAtVal_Click" Width="118px" />
-
</ItemTemplate>
-
</asp:TemplateField>
-
-
</Columns>
-
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
-
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
-
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
-
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
-
</asp:GridView>
_______________ _______________ _______________ ______ Is this wat u asked for??? In the function validatesave() .. am getting the references of the items with tag select. which includes dropdowns and listboxes which vary from (0-4). 1. i want to know how many listboxes are present?? wat's happening is .. whenever i select a item(eg., ABCD) from the listbox select item... it's giving an pop up alert saying.. Please select a value for ABCD. Actually validation should happen when am not selecting from the listbox and it should be like ' Please selec a value for Item'? Please let me know wat is wrong in function validate save?
acoder 16,027
Recognized Expert Moderator MVP @laks2886
Almost, but not quite. What you've posted is ASP.NET code, which is not much use for JavaScript. JavaScript runs on the client-side, so it sees the client-side generated code which you should post (load the page and view source in the browser).
PS. please remember to use [code] tags when posting code. Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: collie |
last post by:
Hi,
I have 2 listboxes. The first gets populated from the db as soon as
the page loads. The second listbox get populated based on the user's
selection from the first listbox. However, currently the code is
such that with each selection there is a postback. We want to avoid it
using filter and javascript. I am not using ADO.NET but adodbc and no
datagrids or datasets (please don't tell me that i should as my boss
clearly doesn't want...
|
by: Daniel |
last post by:
hi,
I had an asp:listbox, and everytime i click item inside, the bar
automatically go to the top, is there any way to keep the scroll position?
I turn on the smartNavigation, it still doesn't work.
Thanks ahead.
|
by: Bob P. |
last post by:
Hello,
I have a page with:
* two side-by-side asp:listboxes and two arrow asp:buttons allowing
users to add/remove email addresses between them -- very much like
Outlook, where you have the address book on the left, and you build
the message recipients on the right.
* an asp:button that, when clicked, fires off some code to create and
send the mail message
|
by: deepak |
last post by:
Hi all,
i m transefering values from one listbox to another listbox using
javascript(both are html controls), the first listbox is populating with
database at page_load time and the second listbox is empty initially .the two
buttons which i have taken are both(btnAd,btnRemove) have values Add,Remove
and both are input type with run as server control property set to true,.my
script is given below , however when i click on add button the ...
|
by: Janaka |
last post by:
Help! I have two ListBox controls on my web form. The first one gets
populated on entry with values from the DB. I then use JavaScript to copy
options from this ListBox to my second one. (I have also tried changing the
second ListBox to an HtmlSelect control) using bog standard JavaScript code
like so where "used" is the name of my <select> control:
used.options = new Option(name, typeId);
This all works fine and I can move across...
| |
by: Jeff |
last post by:
....still new at this. ...hopefully a simple question
Is there some practical way of altering the function of a keypress in Visual Web.net 2005 using VB without causing a postback on
each keypress? For example: A have a listbox with a number of selections. The up and down keyboard keys change the indicated listbox
selection and by default the <tabkey takes the focus away from the listbox and onto a button that will submit the selection. I'm...
|
by: Dave |
last post by:
I recently found out that the RequiredFieldValidator works differently
for a ListBox than I needed; it checks to see if a row is selected,
rather than checking to see if the listbox is empty. I tried using a
CustomValidator but it does not seem to be firing (I have e.false in
the validation function but still no intervention). Does the
CustomValidator also require that a row is selected? Can I use
validators for this requirement or do I...
|
by: Lit |
last post by:
Hi,
How can I capture the vertical scroll bar position for a Listbox.
I have a Listbox of 100 items + when I click on it I post back remove the
item selected.
After returning to the client browser the list box scroll position is at the
top.
|
by: Doogie |
last post by:
I have a .net app that a user currently enters a number in a text box,
hits a button and a data call is executed. She wants the ability to
enter in multiple numbers (up to 100).
So to make things look better visually for that, I created a listbox
under the text box. She enters the number in the text box, clicks
another button I added and the number is stored in the list box. Then
my plan was to grab all those numbers from the list box...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |