Solved

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

Posted on 2004-04-21
6
516 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
[X]
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
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

724 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