JRevathi
asked on
How to access elements inserted into htmlselect through javascript on code behind
Hi All,
I have inserted elements into htmlselect server control through javascript. I wanted to access those elements inserted from code behind. I could access the control, but not the elements inserted , It always returns 0 as the list count.
Following is my code
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx. vb" Inherits="WebApplication1. WebForm1"% >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>HtmlselectAutoPostB ack.aspx</ title>
<script language="javascript">
function bodyload(){
with(document.Form1){
sel.length = 0
sel[sel.length] = new Option("Red",1)
sel[sel.length] = new Option("Green",2)
sel[sel.length] = new Option("Blue",3)
}
}
</script>
</HEAD>
<body onload="bodyload();">
<form id="Form1" method="post" Runat="Server">
<select id="sel" name="sel" runat="server" size="3" multiple>
</select>
<br>
<br>
<asp:button id="btn" Runat="server" Text="test"></asp:button>
<br>
</form>
</body>
</HTML>
code behind:
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
For i As Integer = 0 To sel.Items.Count - 1
If sel.Items(i).Selected Then
Response.Write(sel.Items(i ).Value & " : " & sel.Items(i).Text & " <br>")
End If
Next
End Sub
Earlier reply would help
Thanks in advance
I have inserted elements into htmlselect server control through javascript. I wanted to access those elements inserted from code behind. I could access the control, but not the elements inserted , It always returns 0 as the list count.
Following is my code
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>HtmlselectAutoPostB
<script language="javascript">
function bodyload(){
with(document.Form1){
sel.length = 0
sel[sel.length] = new Option("Red",1)
sel[sel.length] = new Option("Green",2)
sel[sel.length] = new Option("Blue",3)
}
}
</script>
</HEAD>
<body onload="bodyload();">
<form id="Form1" method="post" Runat="Server">
<select id="sel" name="sel" runat="server" size="3" multiple>
</select>
<br>
<br>
<asp:button id="btn" Runat="server" Text="test"></asp:button>
<br>
</form>
</body>
</HTML>
code behind:
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
For i As Integer = 0 To sel.Items.Count - 1
If sel.Items(i).Selected Then
Response.Write(sel.Items(i
End If
Next
End Sub
Earlier reply would help
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
mmarinov
hello again :)
hello again :)
ASKER
Thank you for both of u.
hi chaniewskim,
when i post here there was anything here :-)
when i post here there was anything here :-)
I know I know
I had the same with you a few times
so I say 'hi' :)
I had the same with you a few times
so I say 'hi' :)
Sorry for the answer, but you can not get the items add to the select control through javascript
reason:
because of the client script in your code behind .net doesn't know that there is some change in this control and when you submit the page you lose all added items
if you want to check what is the value/text of the selected item in the htmlselect you can add a hidden field with runat="server" write a javascript function that fill thie sield onchange of the htmlselect but thats all
B..M