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

  • Thread starter Mad Scientist Jr
  • Start date
M

Mad Scientist Jr

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 I don't have
c# on this machine to try and modify it).. anyway...

I'm trying to get javascipt to select all items in a <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
 
B

bruce barker

because your anchor is refreshing the page. try:

<a href="javascript:selectListBox2();">click me</a>

-- bruce (sqlwork.com)


Mad Scientist Jr said:
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 I don't have
c# on this machine to try and modify it).. anyway...

I'm trying to get javascipt to select all items in a <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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top