Solved

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

Posted on 2004-04-21
6
499 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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 …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

832 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