简体   繁体   中英

listbox.Items.Count is equal to zero in code

I have two listboxes in my aspx page,which i called them ListBox1 and ListBox2.

I set the datasource of ListBox1 in pageload but ListBox2 is empty.

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        for (int count = 0; count <= Convert.ToInt32(lbright.Items.Count); count++)
        {
            string str = null;
            string[] strArr = null;
            int count2 = 0;
            str = lbright.Items[count].ToString();
            char[] splitchar = { ';' };
            strArr = str.Split(splitchar);
            for (count2 = 0; count2 <= strArr.Length - 1; count++)
            {
                Response.Write(
                    "Option" + count + ":<br />" +
                    "   " + "Value" + count2 + ":" + strArr[count]
                    );
            }
        }
    }

The user can double click on items in ListBox1 to add them to ListBox2.I have wrote this code in JQuery.Everything works fine in this part.

<script type="text/javascript">
$(document).ready(function () {
    $('#lbleft').on('dblclick', 'option', function () {
        var element = $("#lbleft option:selected");
        var value = element.val();
        var text = element.text();
        element.remove();

        $("#lbright").append('<option value="' + value + '">' + text + '</option>');
    });
    $('#lbright').on('dblclick', 'option', function () {
        var element = $("#lbright option:selected");
        var value = element.val();
        var text = element.text();
        element.remove();

        $("#lbleft").append('<option value="' + value + '">' + text + '</option>');
    });
});

The problem is when i want to read items from ListBox2.
I have a button in my page which user can save items of ListBox2 by clicking on this button.but the count of items of ListBox2 is zero in codebehind.

Since you are adding items to 2nd ListBox at client side using JS, it won't add those values to viewstate , that is why those are not available at serverside using lbright.Items . Asp.net relies on viewstate to get those values, it will available if those are set in serverside.

To make it work, you can get those added value to 2nd listbox using Request.Form["lbright"].ToString() . Provided those values are selected by end-user, like clicking on each option - why? Because those we will be posted to server if selected, for multiple options, it will give comma separated values.

But if they are not selected by end user, above suggestion will not work. To solve this, you need to set that a hidden field ( add new value to hidden field and append rest) and get that using the same thing Request.Form["hdnField"].ToString() , here advantage is, it is independent of end user selection of item from right box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM