This is a migrated thread and some comments may be shown as answers.

Retrieve checked items in listbox from server side after EnableViewState set to false

8 Answers 407 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Rui
Top achievements
Rank 1
Rui asked on 14 Nov 2013, 09:35 PM
Hi, 

I am using RadListBox with checkboxes for user to do multi-selections. But the select options in the list box is massive data, so the postback ViewState is huge. What I did to solve is set EnableViewState="false", then try to grab the checked item in request form. This is my code:

front end:
<telerik:RadListBox ID="listbox1" runat="server" CheckBoxes="true" EnableViewState="false"
                    SelectionMode="Single">
                </telerik:RadListBox>

back end:
var listbox = Request.Form[listbox1.ClientID + "_ClientState"];

The select options are populated in Page_Load().
First I noticed "Request.Form[listbox1.ClientID]" does not exist in the form returned, instead there is "Request.Form[listbox1.ClientID + "_ClientState"]". I guess it's a difference between RadListBox and regular ListBox I am not sure.  But I am not sure what should I do next.
I want to grab the checked options. How can I do that? What structure is the "ClientState"?

Or is there another better way to get ride of the Huge ViewState issue? I know this has been a pain from ASP.NET, but any help will be appreciated.

Thanks

8 Answers, 1 is accepted

Sort by
0
Rui
Top achievements
Rank 1
answered on 14 Nov 2013, 10:05 PM
And also I am just wondering, how is RadListBox return such a big viewstate when it has around 2000 items.
When I use radgrid with around 20000 rows, the viewstate is much smaller.
0
Shinu
Top achievements
Rank 2
answered on 15 Nov 2013, 08:01 AM
Hi Rui,

Please have a look into the sample code to get the selected Item of RadListBox in RadButton Click event.

ASPX:
<telerik:RadListBox ID="RadListBox1" runat="server" CheckBoxes="true" EnableViewState="false"
    SelectionMode="Single">
</telerik:RadListBox>
<telerik:RadButton ID="RadButton1" runat="server" Text="Get Items" OnClick="RadButton1_Click">
</telerik:RadButton>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    RadListBox1.DataSourceID = "SqlDataSource1";
    RadListBox1.DataTextField = "OrderID";
    RadListBox1.DataBind();
    //select an item
    RadListBox1.SelectedIndex = 5;
}
protected void RadButton1_Click(object sender, EventArgs e)
{
    string text = RadListBox1.SelectedItem.Text;
}

Thanks,
Shinu.
0
Rui
Top achievements
Rank 1
answered on 15 Nov 2013, 02:56 PM
I was using that, but now after enableViewState set to false, the RadListBox in the postback doesn't contain the checked items anymore.
0
Rui
Top achievements
Rank 1
answered on 15 Nov 2013, 03:50 PM
what you are getting from RadListBox1.SelectedItem.Text is actually the selected item from page load.
But what I need is what user checked, the sample doesn't really help.

0
Rui
Top achievements
Rank 1
answered on 15 Nov 2013, 05:11 PM
So I tried using Load On Demand to solve my massive ViewState issue, but I still cannot get user's checkitem correctly. Here is the code:

ASP:
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="MetroTouch" Transparency="20"></telerik:RadAjaxLoadingPanel>
 
<telerik:RadListBox CssClass="bottom-field" ID="lbProviders" runat="server" CheckBoxes="true"       EnableLoadOnDemand="true" LoadingPanelID="<%# RadAjaxLoadingPanel1.ClientID %>"
  Width="200px" Height="494px">
 </telerik:RadListBox>
 
<telerik:RadButton runat="server" Text="View"
value="View" ID="Button1" OnClick="bnView_Click" />

C#:
protected void Page_Load(object sender, EventArgs e)
{
        lbProviders.DataSource = dtProviders;
        lbProviders.DataTextField = "ProviderName";
        lbProviders.DataValueField = "ProviderId";
         
 
        if (!Page.IsCallback)
        {
            lbProviders.DataBind();
        }
}
 
protected void bnView_Click(object sender, EventArgs e)
{
    if (lbProviders.CheckedItems.Count > 5)
    {
        //Do something;
    }
 }


But no matter how many items I checked in the list box, the count is always 1 in the server side. It was working before I use load on demand. Why is this?
0
Rui
Top achievements
Rank 1
answered on 15 Nov 2013, 07:17 PM
Hi Shinu, 
I just found out from one of the thread saying "the CheckBoxes feature of RadComboBox along with Load On Demand" is not supported, here is the link: 
http://www.telerik.com/community/forums/aspnet-ajax/combobox/radcombobox-with-checkbox---unable-to-set-checkbox-on-server-side.aspx

Is this the same case for RadListBox?

If so, I will go back to question of how to get the checkedItems Collections from Request.Form[RadListBox_ClientState]?

Thanks

0
Shinu
Top achievements
Rank 2
answered on 19 Nov 2013, 11:55 AM
Hi Rui,

The RadListBox CheckedItems.Count is returning Zero because it is binding on the Page_Load event. Please try the following approach to get the CheckedIndices of the RadListBox.

ASPX:
<telerik:RadListBox ID="RadListBox1" runat="server" CheckBoxes="true" EnableViewState="false"
           SelectionMode="Single">
       </telerik:RadListBox>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    RadListBox1.DataSourceID = "SqlDataSource1";
    RadListBox1.DataTextField = "OrderID";
    if (!Page.IsCallback)
    {
        RadListBox1 .DataBind();
    }
}
protected void RadButton1_Click(object sender, EventArgs e)
{
    string listbox = Request.Form[RadListBox1.ClientID + "_ClientState"];
    string[] tokens = listbox.Split(new string[] { "\"" }, StringSplitOptions.None);
    for (int i = 0; i < tokens.Length; i++)
    {
        if (tokens[i] == "checkedIndices")
        {
            string temp = tokens[i + 1];
            string[] checkedindices = new string[temp.Length];
            int k = 0;
            for (int j = 2; j < temp.Length - 1; j = j + 2)
            {
                checkedindices[k] = temp[j].ToString();
                Response.Write(checkedindices[k]);
                k++;
            }
        }
    }
}

Thanks,
Shinu.
0
Rui
Top achievements
Rank 1
answered on 19 Nov 2013, 07:00 PM
Hi Shinu, 
I was hoping for something better than string parsing, but that works.
There is one problem with your code though.

for (int j = 2; j < temp.Length - 1; j = j + 2)

This only works for single digit indices, I changed to something like this and it works:

List<string> checkededindices = new List<string>();
string listbox = Request.Form[lbProviders.ClientID + "_ClientState"];
string[] tokens = listbox.Split(new string[] { "\"" }, StringSplitOptions.None);
for (int i = 0; i < tokens.Length; i++)
{
    if (tokens[i] == "checkedIndices")
    {
        string temp = tokens[i + 1].Substring(2, tokens[i+1].Length-4);
        checkededindices = temp.Split(new char[] {','}).ToList<string>();
 
         
    }
}


Thanks
Tags
ListBox
Asked by
Rui
Top achievements
Rank 1
Answers by
Rui
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or