Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-04-21
6
506 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

789 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