accessing elements added htmlselect using javascript

S

Suhail A, Salman

Dear All,

I placed a HtmlSelect control on a web page and set it to "Run at
Server", the
objective of this HtmlSelect control is that the client adds all his
accounts to a
text box, and because this is a client side operation(no server
intervention is required), I wrote the JavaScript below to add account
number
entered in a text box to the list control, this works fine, the client can
enter all his accounts to the html select control, the problem is that when
the page is posted to the server the code within the server returns no items
within the control so I have lstCustAcc.Items.Count = 0 even on the page it
contains some thing else >0, and I could not access any item within the
control.

summary:
the data added to the htmlselect control using the client javascript are
not accessable from C#.

PLEASE HELP.


function AddAcc()
{
var lstLen = document.Form1.lstCustAcc.length;
var selectedText = document.Form1.txtAccountNo.value;
var selectedValue = document.Form1.txtAccountNo.value;
var i;
var isNew = true;

if (lstLen != 0)
{
for (i = 0; i < lstLen; i++)
{
thisitem = document.Form1.lstCustAcc.options.text;
if (thisitem == selectedText)
{
isNew = false;
break;
}
}
}
if (isNew)
{
newoption = new Option(selectedText, selectedValue, false, false);
document.Form1.lstCustAcc.options[lstLen] = newoption;
}
document.Form1.lstCustAcc.selectedIndex=-1;
}





Thanks in advance,
Suhail
 
M

Marc

All the HtmlSelect control does is generate the <SELECT ...><OPTION
....></SELECT> code in the HTML page. The HTML page knows nothing of
the HtmlSelect server control, and all that will happen when you
submit the page is what normally happens when you submit a HTML page
with a <SELECT> tag: i.e. it will just post the value of the selected
option within it.

What you will need to do is copy all of the values from the <SELECT>
<OPTION>s in to, say, a hidden text field. Then on the server you can
parse the posted value from the hidden text field.

However you submit the page, you will need to have something like:

<script>
function GetAccountDetails()
{
var lstAcconts=document.Form1.lstCustAcc;
var intIdx=0;
var strData='';

while ( intIdx < lstAccounts.length )
{
if ( intIdx > 0 )
strData+=',';

strData+=lstAccounts.options[intIdx].text;

intIdx++;
}

document.Form1.txtAccountNumbers.value=strData;
}
</script>
/* The following line must be inside the <FORM id="Form1"> ... </FORM>
tags */
<input type="hidden" id="txtAccountNumbers" value="">


Call the function from the <FORM> tag with

<FORM id="Form1" method="post" action="somepage.aspx"
onsubmit="GetAccountDetails();">


On the server, you could have the following (you'll have to convert
this to C#, cos I don't know it):


Dim strAccounts As String() = Split(Request.Form("txtAccountNumbers"),
",")


which will give you an array of the account numbers. If there is only
one element in the array, and it is an empty string, then there were
no account numbers.


Suhail A said:
Dear All,

I placed a HtmlSelect control on a web page and set it to "Run at
Server", the
objective of this HtmlSelect control is that the client adds all his
accounts to a
text box, and because this is a client side operation(no server
intervention is required), I wrote the JavaScript below to add account
number
entered in a text box to the list control, this works fine, the client can
enter all his accounts to the html select control, the problem is that when
the page is posted to the server the code within the server returns no items
within the control so I have lstCustAcc.Items.Count = 0 even on the page it
contains some thing else >0, and I could not access any item within the
control.

summary:
the data added to the htmlselect control using the client javascript are
not accessable from C#.

PLEASE HELP.


function AddAcc()
{
var lstLen = document.Form1.lstCustAcc.length;
var selectedText = document.Form1.txtAccountNo.value;
var selectedValue = document.Form1.txtAccountNo.value;
var i;
var isNew = true;

if (lstLen != 0)
{
for (i = 0; i < lstLen; i++)
{
thisitem = document.Form1.lstCustAcc.options.text;
if (thisitem == selectedText)
{
isNew = false;
break;
}
}
}
if (isNew)
{
newoption = new Option(selectedText, selectedValue, false, false);
document.Form1.lstCustAcc.options[lstLen] = newoption;
}
document.Form1.lstCustAcc.selectedIndex=-1;
}





Thanks in advance,
Suhail
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top