p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   How to avoid page refreshing (http://p2p.wrox.com/showthread.php?t=58787)

sarah lee June 6th, 2007 12:29 PM

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

vinod_yadav1919 June 12th, 2007 02:11 AM

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

sarah lee June 12th, 2007 07:51 AM

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




All times are GMT -4. The time now is 09:33 AM.

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