943,838 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 4858
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 20th, 2005
0

Listbox Related Matter

Expand Post »
Hi all,

I'm relatively new to ASP.NET - used to code in Coldfusion.
I've got this control created in a page <asp:listbox>.
I also have a javascript function in the same page to add listitems into the listbox on certain client calls such as the following:-

ASP.NET Syntax (Toggle Plain Text)
  1. var anOption = document.createElement("OPTION");
  2. obj.add(anOption);
  3. anOption.text = "foo";
  4. anOption.value = "1";

I am sure that the items are added into the listbox as I can see it on the front end. Okay the problem is, when I trigger a function call in the vb language, I get listbox1.Items.Count = 0. It should be 1 there. So I'd like to find out from fellow ASP.NET developers if the asp's server control will take note of the listitems updated via javascript.

Thanks in advance.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Dec 20th, 2005
0

Re: Listbox Related Matter

I might be able to help you with the .Net side, but I am not as good as you with the javascript. Can you paste the rest of the javascript that adds the option to the listbox?
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Dec 21st, 2005
0

Re: Listbox Related Matter

Hey kev, thanks in advance.
Here's the function in javascript:-

ASP.NET Syntax (Toggle Plain Text)
  1. function addOptionToListBox(lsVal, lsNam, obj) {
  2. var anOption = document.createElement("OPTION");
  3. obj.add(anOption);
  4. anOption.text = lsNam;
  5. anOption.value = lsVal;
  6. }

Then onclick of a button, I am trying to get the value of listbox1.Items.Count in vb.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Dec 21st, 2005
0

Re: Listbox Related Matter

sorry, I still can't get it to even add the option on the client side. Can you post the whole page?
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Dec 21st, 2005
0

Re: Listbox Related Matter

Elements you add via JavaScript have nothing to do with Objects created in ASP.NET. Given the client-server nature of the web, surely you can see why this is the case.

Server Objects (ASP.NET "Web Controls") exist only on the server. When done processing, the ViewState object is created and embedded within the response stream as a hidden form variable. This is the mechanism by which Web Controls are recreated and restored to their state on PostBack.

Since your JavaScript is creating client elements, and not adding them to the ViewState variable, there is no way your server-side control can create corresponding Web Controls on postback.

What you need to do:

Server-side, you can iterate through the Request object to retrieve posted form values, including any options etc. you created client-side.

OR

Create all your controls server-side.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Dec 21st, 2005
0

Re: Listbox Related Matter

right, that (option a) was what I was going for. Wanted to give him some sample code but couldn't get the javascript part working
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Dec 21st, 2005
0

Re: Listbox Related Matter

Thanks a lot kev and tgreer. I'll try out the first option suggested and come back with a feedback
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Dec 22nd, 2005
0

Re: Listbox Related Matter

can you post the rest of the aspx page? I can't get it to work and now you have me hooked on making it work?
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Dec 23rd, 2005
0

Re: Listbox Related Matter

Err, it's kinda difficult eh kev.
But here goes:-

File1.aspx:-

ASP.NET Syntax (Toggle Plain Text)
  1. <asp:ListBox ID="Groups" runat="server" SelectionMode="multiple" width="250"/>
  2. <script type="text/javascript">
  3. function getCustomerList(custIDList, custNameList){
  4. var lbClients = document.getElementById("<%=Clients.ClientID %>");
  5. var lbHidden = document.getElementById("<%=hdnClientID.ClientID %>");
  6. var lsValue = custIDList.split(",");
  7. var lsName = custNameList.split(",");
  8. var cnt = 0;
  9. var s = lbHidden.value;
  10. for(var i=0; i<lsValue.length; i++) {
  11. cnt = 0;
  12. for(var j=0; j<lbClients.options.length; j++) {
  13. if(lbClients.options[j].value != lsValue[i])
  14. cnt++;
  15. }
  16. if(cnt == lbClients.options.length) {
  17. addOptionToListBox(lsValue[i], lsName[i], lbClients);
  18. if (s.length > 0)
  19. s = s + ",";
  20. s = s + lsValue[i];
  21. }
  22. }
  23. lbHidden.value = s;
  24. }
  25. function addOptionToListBox(lsVal, lsNam, obj) {
  26. var anOption = document.createElement("OPTION");
  27. obj.add(anOption);
  28. anOption.text = lsNam;
  29. anOption.value = lsVal;
  30. }
  31. </script>

File2.aspx (VB code):-
ASP.NET Syntax (Toggle Plain Text)
  1. strScript = "@SCRIPT>window.opener.getCustomerList('" & strValue.ToString() & "', '" & strName.ToString() & "'); window.close(); window.opener.focus();@/SCRIPT>"
  2. ClientScript.RegisterClientScriptBlock(Page.GetType(), "CloseChild", strScript.Replace("@", "<"))

Btw, I could not use any of your suggested options, tgreer.
[1] Could not use Request.Form because it's a multiple selection in the listbox.
If I am to programatically do .select() on the listitems in the listbox after adding them it, it would then look kinda ugly lol.
So I opted to add a hidden field instead - by storing a comma separated list of the customer IDs.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Dec 23rd, 2005
0

Re: Listbox Related Matter

All form values are submitted back to the server, so all form values are in the Request object, multi-select or not.

Of course, the canonical way to do this in ASP.NET is to populate your controls server-side, even if this means adding server round-trips to your logic.

Glad you got it working, but I predict you're going to have a real struggle with ASP.NET!
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Your advice is needed!
Next Thread in ASP.NET Forum Timeline: Problem With database Connectivity





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC