473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

autoselect all items in <SELECT> control is undone when form submits

I'm trying to get javascipt select all items in a HTML form <SELECT>
control and submit the form to an asp.net page. For some reason when
the link is clicked, you can see the items all get selected, but then
they are somehow unselected as the form posts. Any idea? My Html and
..net code follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test SELECT</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>

<form id="Form1" method="post" runat="server">

<table>
<tbody>
<tr>
<td>
<SELECT multiple size="20" name="ListBox2" id="ListBox2"
style="WIDTH:350px">
<OPTION selected value="Value 1">Text 1</OPTION>
<OPTION>Text 2</OPTION>
<OPTION>Text 3</OPTION>
<OPTION>Text 4</OPTION>
<OPTION>Text 5</OPTION>
<OPTION>Text 6</OPTION>
<OPTION>Text 7</OPTION>
<OPTION>Final option</OPTION>
</SELECT>
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtOut" runat="server" Columns="60" Rows="10"
TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<a href="#" onclick="javascript:selectListBox2();">click me</a>
&nbsp;
<asp:Button id="Button1" runat="server"
Text="Button"></asp:Button>
</td>
</tr>
</tbody>
</table>

<script>
<!--
function selectListBox2()
{
var iLoop;
for (iLoop = 0; iLoop < document.Form1.ListBox2.length; iLoop++)
{
document.Form1.ListBox2[iLoop].selected = true;
}
document.Form1.submit();
}
// -->
</script>

</form>

</body>
</HTML>


Public Class test_DualList
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents txtOut As System.Web.UI.WebControls.TextBox

'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim iLoopRows As Int16

If (IsPostBack) Then
txtOut.Text = Request.Form.Item("ListBox2")
Else
Button1.Attributes.Add("onclick",
"javascript:selectListBox2();")
End If

End Sub
End Class
Jul 20 '05 #1
2 4537
Mad Scientist Jr wrote:

I'm trying to get javascipt select all items in a HTML form <SELECT>
control and submit the form to an asp.net page. For some reason when
the link is clicked, you can see the items all get selected, but then
they are somehow unselected as the form posts. Any idea? My Html and
.net code follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test SELECT</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>

<form id="Form1" method="post" runat="server">

<table>
<tbody>
<tr>
<td>
<SELECT multiple size="20" name="ListBox2" id="ListBox2"
style="WIDTH:350px">
<OPTION selected value="Value 1">Text 1</OPTION>
<OPTION>Text 2</OPTION>
<OPTION>Text 3</OPTION>
<OPTION>Text 4</OPTION>
<OPTION>Text 5</OPTION>
<OPTION>Text 6</OPTION>
<OPTION>Text 7</OPTION>
<OPTION>Final option</OPTION>
</SELECT>
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtOut" runat="server" Columns="60" Rows="10"
TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<a href="#" onclick="javascript:selectListBox2();">click me</a>
&nbsp;
<asp:Button id="Button1" runat="server"
Text="Button"></asp:Button>
</td>
</tr>
</tbody>
</table>

<script>
<!--
function selectListBox2()
{
var iLoop;
for (iLoop = 0; iLoop < document.Form1.ListBox2.length; iLoop++)
{
document.Form1.ListBox2[iLoop].selected = true;
}
document.Form1.submit();
}
// -->
</script>

</form>

</body>
</HTML>

Public Class test_DualList
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents txtOut As System.Web.UI.WebControls.TextBox

'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim iLoopRows As Int16

If (IsPostBack) Then
txtOut.Text = Request.Form.Item("ListBox2")
Else
Button1.Attributes.Add("onclick",
"javascript:selectListBox2();")
End If

End Sub
End Class


Have you tried running the code with the javascript function in the HEAD
of the page instead of inline ?
If not, please try that first.

J.
Jul 20 '05 #2
On 5 Mar 2004 12:07:18 -0800, Mad Scientist Jr <us*************@yahoo.com>
wrote:
I'm trying to get javascipt select all items in a HTML form <SELECT>
control and submit the form to an asp.net page. For some reason when
the link is clicked, you can see the items all get selected, but then
they are somehow unselected as the form posts. Any idea? My Html and
.net code follows:
You could have posted a lot less code (what I've left below is
sufficient), especially omitting the VBScript. Also, please only use two
spaces when indenting you code. Eight characters makes it difficult to
read: four should be the maximum.

[snipped menu and text box]
<tr>
<td>
<a href="#" onclick="javascript:selectListBox2();">click me</a>
I think your problem is here. It is quite likely that once the
selectListBox2() function has executed, the browser tries to navigate to
'#', resetting the form in the process. Try:

<a href="" onclick="selectListBox2();return false;">click me</a>

I've also made some comments below.

[snipped button]
<script>
The type attribute is required:

<script type="text/javascript">
<!--
Hiding scripts is a practice that is now obsolete. Remove the SGML comment
delimiters.
function selectListBox2()
{
var iLoop;
for (iLoop = 0; iLoop < document.Form1.ListBox2.length; iLoop++)
{
document.Form1.ListBox2[iLoop].selected = true;
}
document.Form1.submit();
}


This can be better implemented as:

function selectListBox2() {
var f = document.form1, e = f.elements, n = e.length;

for( var i = 0; i < n; ++i )
e[ i ].selected = true;
f.submit();
}

The repeated property accesses in the original add quite an overhead.

My final comment is to consider more descriptive names. selectListBox2 and
form1, as well as similar instances in the document, are *very* bad
identifier names.

[snipped VBScript]

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: headware | last post by:
I have a <select> control that contains many entries. It allows the user to multi-select a group of them, click a button, and store the selected data in a database. Normally they do this starting...
6
by: Omar | last post by:
Hi, In a JSP I have the next: .... codigo = "<select name='" + nombre + "'>\n<option selected value='default'>Escoge</option><option value='todos'>Todos</option>"; if (miRS != null) while...
6
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets...
1
by: Mariusz | last post by:
Hi does anyone know an easy way to define object <select> in that way when clicking on it works as right alt is used. In other words - I need to rebuild select than when I click on first option...
1
by: Mad Scientist Jr | last post by:
I'm stuck trying to work with a HTML <SELECT> control and javascript (similar to DualList but that control doesn't offer enough options to totally control the text on the buttons and control, also...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
7
by: lambertb | last post by:
Hi, is this possible to achieve this, and how? http://img217.imageshack.us/img217/779/derrrvw2.png thanks!
1
by: Eric | last post by:
Hello, I'm trying (without any success) to get the value of a disabled select control. From reviewing: http://www.w3.org/TR/html401/interact/forms.html#adef-disabled It doesn't sound like...
5
by: Alan M Dunsmuir | last post by:
I have a Form with a <SELECTtab, its <OPTIONtabs generated by records in a database table using PHP5 and a 'SELECT' query. The values displayed are chosen to be recognisable to the users (they...
1
by: sreenathvanmelil | last post by:
Hai, one <select> option in an php form, we select any one , that related values displayed in another one combobox. both <select> option details from database(mySql) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.