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="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table>
<tbody>
<tr>
<td>
<SELECT multiple size="20" name="ListBox2" id="ListBox2"
style="WIDTH:35 0px">
<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="Multi Line"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<a href="#" onclick="JavaSc ript:selectList Box2();">click me</a>
<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.P age
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
End Sub
Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on
Protected WithEvents txtOut As System.Web.UI.W ebControls.Text Box
'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Dim iLoopRows As Int16
If (IsPostBack) Then
txtOut.Text = Request.Form.It em("ListBox2")
Else
Button1.Attribu tes.Add("onclic k",
"JavaScript:sel ectListBox2();" )
End If
End Sub
End Class
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="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table>
<tbody>
<tr>
<td>
<SELECT multiple size="20" name="ListBox2" id="ListBox2"
style="WIDTH:35 0px">
<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="Multi Line"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<a href="#" onclick="JavaSc ript:selectList Box2();">click me</a>
<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.P age
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
End Sub
Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on
Protected WithEvents txtOut As System.Web.UI.W ebControls.Text Box
'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Dim iLoopRows As Int16
If (IsPostBack) Then
txtOut.Text = Request.Form.It em("ListBox2")
Else
Button1.Attribu tes.Add("onclic k",
"JavaScript:sel ectListBox2();" )
End If
End Sub
End Class
Comment