Click here to Skip to main content
15,310,056 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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

1 solution

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;

}
   

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900