?
Solved

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

Posted on 2004-04-21
6
Medium Priority
?
518 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 400 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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:
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…
Suggested Courses

771 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