Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 6th, 2007, 12:29 PM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to avoid page refreshing

hi all

I am trying to solve this problem for a while. but not yet done

I dont know javasxript, but i have to use that in my application for the below task

I have 3 listboxes in my page, and have them loaded with data

I have 3 checkboxes also under each listbox

What i want to do is, when checkbox1 is checked, listbox1 items all get selected

When checkbox is unchecked, the items get deselected.

Also i am trying so that, while the checkbox is checked and the items are selected, if the

user selects a particular item in listbox, the checkbox check should get removed.

The same thing i want to accomplish for the 3 listboxes

All these i have done in code behind, which causes page refreshingI am trying to do them in javascript

I hve tried the below code, but its not getting any result, the listbox items are not being selected
Code:
function selectChange(checkBox1)

{
var listBox = document.getElementById('lstAccounts');

if(checkBox1.checked)
{
for (var i=0; i<listBox.options.length; i++)
{
listBox.options[i].selected = true;
}
}
else
{
for (var i=0; i<listBox.options.length; i++)
{
listBox.options[i].selected = false;
}
}
}

<asp:ListBox ID="lstAccounts" runat="server" Height="125px" Width="135px" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox> <input id="Checkbox1" type="checkbox" onclick="selectChange(this)" />Check All
Hope someone can help me in this

Thanks
 
Old June 12th, 2007, 02:11 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Sarah lee!!


*** You are using asp.net 2.0 or something else.
*** Also ensure that the html code rendered by the ASP.Net Framework,i.e ListBox ID and other controls id is correct as you were writing in ur js code.
Plz check the following working code.


<script language="javascript">
function selectChange(checkBox1)

{
var listBox = document.getElementById('lstAccounts');

if(checkBox1.checked)
{
for (var i=0; i<listBox.options.length; i++)
{
listBox.options[i].selected = true;
}
}
else
{
for (var i=0; i<listBox.options.length; i++)
{
listBox.options[i].selected = false;
}
}
}
</script>
<input type=checkbox id=checkbox1 onclick="selectChange(this)">
<select name="lstAccounts" id="lstAccounts" multiple>
<option value=1>1</option>
<option value=1>11</option>
<option value=1>12</option>
<option value=1>123</option>
<option value=1>13</option>
</select>
Hope this will help you

Cheers :)

vinod
 
Old June 12th, 2007, 07:51 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 107
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Thank you for your response

I got the javascript to work
But it lead to another issue
I am calling javascript frm server side
What happens is as the listbox 1 get populated, in my application,
 i have a button which refreshes the page thereby populating listbox2,
 with reference to the items selected in listbox1
So what is happening is,
- the user selects checkall-- all items get selected
- the user hits the refresh button -- all the items deselected and also checkbox is marked unchecked

Hope you can understand my issue
Please help me to fix this dilemma

Thank you very much for your help so far







Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with refreshing the page fozia_izhar .NET Framework 1.x 1 March 24th, 2007 02:18 PM
page not refreshing Bill Crawley ASP.NET 2.0 Basics 8 February 28th, 2007 11:39 AM
refreshing XML withour refreshing the page sasidhar79 XML 1 January 12th, 2005 05:16 AM
Problem In refreshing a page. zaeem Classic ASP Components 2 November 4th, 2003 12:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.