CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2006
    Posts
    357

    Weird problem with posting user control and listboxes

    Hey there,

    Ive got a user control ive made that is basically 2 list boxes that allow you to pass data between them both... so you can choose what items from the first listbox you want...

    Anyway this is all in a custom user control i have made, the logic all works great until i start bringing the server side into it.

    I have 2 buttons that move the items around, and they do it all via JS as i dont want to have to post back to the server every time they move the data around... That part all works great and i can move the items around from the left to the right box etc.

    Anyway, when i go to submit to the page the right listbox is always empty, there are no items/selectedindex yet when i submit there may be 5+ items in there. Im assuming that the problem stems from the listboxes not having the data in there from the start so it just ignores it or something when posting... but whatever the cause im in need of a solution, been scouring the net all morning and cant find much.

    The main things i need to adhere to are:

    - The control cannot refresh the page on moving the data round, this all has to be done clientside
    - The 2nd listbox needs to persist through submits, incase any data is incorrect when validating


    If anyone knows what im on about and known a possible solution or can point me in the right direction i will give you many thanks!

  2. #2
    Join Date
    Jan 2009
    Location
    Cochin, India
    Posts
    40

    Smile Re: Weird problem with posting user control and listboxes

    Hi,

    I have faced that problem before. In my case I had 2 list boxes and one list box was full of list items the first time the page is loaded in the browser. Then I had a couple of buttons with which I could move items from one list box to the other and backwards totally on the client side. I did all this through extensive javascript. That is I could move the items back and forth between the list boxes using javascript. And every time I moved something I made sure that each item in either list box had its values and text stored as a comma delimited string in a hidden field. This was also done through javascript. And on post back I examined this hidden field to get the items in the list boxes. I do not think there is any way except this to do what you want. The items you add to the list box on the client side are simply not visible when it reaches the server.

    If it were a simple html text box like say

    <input type="text" id="mytxt" value="*" name="mytxt" />

    then even though it is not a server control, after post back you can get the value which was typed into it in the client side by using

    Request.Form["mytxt"]

    But in your case you are adding list items to a list box on the client side. At least in my case, when I faced precisely the same problem, I had to put all the values and text of the items in the list boxes in two hidden field variables as comma delimited strings and then examine those hidden fields on post back to get at the values in each list box.

    You see when you post back the list boxes get reinitialized and the items you added in the client side simply aren't there. I can help you with some javascript code for getting the values in the list boxes by using hidden fields if you like.

    Hope this helps.

    Warm Regards


    Quote Originally Posted by Grofit View Post
    Hey there,

    Ive got a user control ive made that is basically 2 list boxes that allow you to pass data between them both... so you can choose what items from the first listbox you want...

    Anyway this is all in a custom user control i have made, the logic all works great until i start bringing the server side into it.

    I have 2 buttons that move the items around, and they do it all via JS as i dont want to have to post back to the server every time they move the data around... That part all works great and i can move the items around from the left to the right box etc.

    Anyway, when i go to submit to the page the right listbox is always empty, there are no items/selectedindex yet when i submit there may be 5+ items in there. Im assuming that the problem stems from the listboxes not having the data in there from the start so it just ignores it or something when posting... but whatever the cause im in need of a solution, been scouring the net all morning and cant find much.

    The main things i need to adhere to are:

    - The control cannot refresh the page on moving the data round, this all has to be done clientside
    - The 2nd listbox needs to persist through submits, incase any data is incorrect when validating


    If anyone knows what im on about and known a possible solution or can point me in the right direction i will give you many thanks!
    Last edited by sr_jay; February 25th, 2009 at 12:29 AM.
    Jay
    Support Resort
    http://www.supportresort.com
    Bringing offshore expertise to the world

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured