Click here to Skip to main content
13,355,726 members (84,338 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Can any one please tell me in ASP.NET How to ADD or REMOVE text box value to List Box item using Add and Remove buttons this should be done using Java script only without server post back.

This is my code, but its not working.

ASPX :-
<input type="button" value="Add Item" onclick="javascript:addValue()" />
<input type="button" value="Delete Item" onclick="javascript:deleteValue()" />
 <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
<asp:ListBox ID="LstChanelType" runat="server"></asp:ListBox>


Javascript :-
<script type="text/javascript">
var i = 0;
function addValue() {
var v = document.form1.txtChanelType.value;
// get the TextBox Value and assign it into the variable
AddOpt = new Option(v, v);
document.form1.LstChanelType.options[i++] = AddOpt;
return true;
}
function deleteValue() {
var s = 1;
var Index;
if (document.form1.LstChanelType.selectedIndex == -1) {
alert("Please select any item from the ListBox");
return true;
}
while (s > 0) {
Index = document.form1.LstChanelType.selectedIndex;
if (Index >= 0) {
document.form1.LstChanelType.options[Index] = null;
--i;
}
else
s = 0;
}
return true;
}
</script>


Always here i am getting an error message  :-
 Compiler Error Message: CS1026: ) expected
 Line 192: <asp:Button ID="add" Text="Add" CssClass="form_submit" runat="server" onclick="javascript:addValue()" />

Please please help me...
Posted 6-Aug-14 3:54am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Issue has been fixed

var i = 0;
function addValue() {
// alert("Hi");
var v = document.getElementById("MainContent_txtValue").value;
// alert(v);
// get the TextBox Value and assign it into the variable
AddOpt = new Option(v, v);
document.getElementById("MainContent_LstChanelType").options[i++] = AddOpt;
//document.form1.LstChanelType.options[i++] = AddOpt;
return true;
}

function deleteValue() {
var s = 1;
var Index;

if(document.getElementById("MainContent_LstChanelType").selectedIndex == -1)
{
alert("Please select any item from the ListBox");
return true;

}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 6 Aug 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100