Solved

How to access elements inserted into htmlselect through javascript on code behind

Posted on 2004-04-21
6
480 Views
Last Modified: 2013-11-19
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>HtmlselectAutoPostBack.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

0
Comment
Question by:JRevathi
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
chaniewskim earned 100 total points
ID: 10886159
Sorry, impossible.
You could find some workaround: for example use hidden field and write there information about js-inserted entries, for example:

1,Option 1;2,Option2...

Then you could parse that string on server-side and recreate options on server-side or use that information in other way.
But only this way. SELECT doesn't post it's entries to server on postback.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10886217
Hi JRevathi,

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
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10886234
mmarinov
hello again :)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:JRevathi
ID: 10886268
Thank you for both of u.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10886279
hi chaniewskim,

when i post here there was anything here :-)
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10886299
I know I know
I had the same with you a few times
so I say 'hi' :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now