Clear Listbox Items using javascript

457 views
Skip to first unread message

abhinandan

unread,
Jun 16, 2008, 7:58:51 AM6/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting, abhina...@rasstechnologies.com
Hi All,

I have
--> 2 listboxes,consider lstbox1 and lstbox2

--> 3 buttons,
* "btnSend" to push item from lstbox1 to lstbox2.
* "btnSave" to save the item in the lstbox2.
* "btnCancel" to cancel the futher transaction.

Am sending selected item from lstbox1 to lstbox2 by clicking on
btnSend and then when i dont want to save this transaction i click
btnCancel.Now when i click on btnCancel lstbox2 clears.


page_load()
{
btnCancel.Attributes.Add("OnClick","javascript:return
confirm_Cancel();");
}


<script language="javascript">
function confirm_Cancel()
{
if (confirm('Are you sure you want to Cancel?')==true)
{
document..getElementById('<%=lstempgenall.ClientID
%>').options.length=0;
or
document.getElementById('<%=lstempgenall.ClientID
%>').options.length=null;
return true;
}
else
return false;
}


the problem am facing is after i click on btnCancel,lstbox2 clears and
then again if i select other item from lstbox1 and send,the old value
which seemed to be cleared from the lstbox2 reappears.

how do i completely remove the items from the lstbox2 and it should
not reappear?


Thanks a lot in Advance,

Regards,
Abhinandan

Cerebrus

unread,
Jun 16, 2008, 12:40:02 PM6/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You haven't shown us the "Send" button's code.

abhinandan

unread,
Jun 17, 2008, 12:43:38 AM6/17/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
protected void btSend_Click(object sender, EventArgs e)
{
if (lstbox1.SelectedIndex > -1)
{
foreach (ListItem ls in lstBox1.Items)
{
if (ls.Selected == true)
{
int allid = int.Parse(ls.Value);

bool res = islstEmpAllExists(allid, lstbox2);

if (!res)
{
lstbox2.Items.Add(ls);

}
else
{

ScriptManager.RegisterStartupScript(this.Page, typeof(String),
"errall", "alert('Allergy already exists');", true);


}

}

}
}
else
{
ScriptManager.RegisterStartupScript(this.Page,
typeof(String), "errall1", "alert('Select one item from the allergy
list');", true);
}

}


public Boolean islstEmpAllExists(int allid, ListBox lstempall)
{
Checking if there is any duplicate entry and return true or
false.
> Abhinandan- Hide quoted text -

- Show quoted text -
Reply all
Reply to author
Forward
0 new messages