Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > accessing elements added htmlselect using javascript

Reply
Thread Tools

accessing elements added htmlselect using javascript

 
 
Suhail A, Salman
Guest
Posts: n/a
 
      08-13-2003
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[i].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



 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      08-13-2003
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, Salman" <> wrote in message
news:...
> 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[i].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
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting HtmlSelect SelectedIndex using a value writebrent@gmail.com ASP .Net 1 08-25-2006 06:03 AM
HtmlSelect value not available when using callback instead of post =?Utf-8?B?Wm9vZG9y?= ASP .Net 0 11-15-2005 09:36 AM
HtmlSelect populated dynamically, fails to return selected item Stephen Miller ASP .Net 2 01-28-2004 11:10 PM
ASP.NET 2.0: Are they going to fix HtmlSelect? nospam ASP .Net 4 10-16-2003 02:53 PM
HtmlSelect control is not helping and client side coding became a real headache Suhail A, Salman ASP .Net 0 08-13-2003 10:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57