Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access list box items added on client side

Tags:

c#

asp.net

  1. I have a listbox with runat=server
    1. Items are added to this listbox on the client side, using javascript
    2. I would want to retrieve the items on the server side on the click of a button

The problem is in the Buttons's server side click handler, I cannot see the new items added to the listbox. Only the items that were there on page_load are displayed. How do i accomplish what i want to do

Edit 1

My Code Is like this

 protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            if (ViewState["gameID"] == null)
            {
                //Populate Listbox
                //Set gameid in viewstate
            }

            //Add javascript handlers to buttons
            btnSomeButton.Attributes.Add("onclick", "aJavaScriptFunction"); 

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ListItemCollection x = ListBoxRanks.Items;
        //Here items is has just those items that are added to the listbox on page load

    }
like image 278
Midhat Avatar asked Jan 25 '26 22:01

Midhat


1 Answers

Ah when abstractions leek :)

Web server controls are serialised to view state before the response is sent, the control is recreated on postback and the options all put back from view state.

When you add option items client side they are not added to viewstate. The only way is to use your own hidden field to serialise client side additions and read them on postback or ajax the additions serverside.

like image 75
hollystyles Avatar answered Jan 28 '26 14:01

hollystyles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!