Showing posts with label preserve. Show all posts
Showing posts with label preserve. Show all posts

Monday, March 26, 2012

ListBox does not preserve selected items after using UpdatePanel

Hi there,

I have a ListBox with Multiple Selection feature. After I click the Button on the form, I should process the selected items in the ListBox. The code works fine but after I add the ListBox in an UpdatePanel, it does not preserve selected items.

Any suggestions?

Thanks,
Mosh

use hidden fields.
function addItemstoHiddenField(){
var lbPlantDest = document.getElementById("lbPlantDest");
var lbSalesDest = document.getElementById("lbSalesDest");
var lbPlantDest = document.getElementById("lbPlantSrc");
var lbSalesDest = document.getElementById("lbSalesSrc");
var hdnPlantAccess = document.getElementById('hdnPlantAccess');
var hdnSalesAccess = document.getElementById('hdnSalesAccess');
var hdnPlantAccess = document.getElementById('hdnSrcPlantAccess');
var hdnSalesAccess = document.getElementById('hdnSrcSalesAccess');
for (i = lbPlantDest.options.length - 1; i >= 0; i--) {
hdnPlantAccess.value = hdnPlantAccess.value + lbPlantDest.options[i].value + ',';
}
for (i = lbSalesDest.options.length - 1; i >= 0; i--) {
hdnSalesAccess.value = hdnSalesAccess.value + lbSalesDest.options[i].value + ',';
}
for (i = lbPlantSrc.options.length - 1; i >= 0; i--) {
hdnSrcPlantAccess.value = hdnSrcPlantAccess.value + lbPlantSrc.options[i].value + ',';
}
for (i = lbSalesSrc.options.length - 1; i >= 0; i--) {
hdnSrcSalesAccess.value = hdnSrcSalesAccess.value + lbSalesSrc.options[i].value + ',';
}
}

ListSearch does not preserve ViewState

I just downloaded the latest version of the Toolkit today hoping that the 3 or so issue requests for the ListSearch would have corrected everything.

My problem is that after a item is selected in a DropDownList the and I either hit the tab key or press enter (with the autopostback set on the ddl) the postback occurs (that part was fixed from the last version) but when the page comes back the ddl is reset. It does not contain the item I selected even though enableviewstate is set to true. Does anyone else have this problem or know how to correct it?

Thanks

I've just run a small test and it worked fine for me -- if you change the item simply by clicking, does the selected item remain?

Thanks,

Damian


FWIW if you try enabling the AutoPostBack on the example in the toolkit, then it won't work because the control's contents are always rebound on page load, instead of just once when the page is initially loaded.

Regards,

Damian (ListSearch author)


No it does not, I have tried it both in a update panel and with the controls by themselves (not that that should matter). For my testing I took the listsearch page from the new sample website and just added a label to it. Then in the selectedindexchanged event of the ddl I set the label text to the current datetime just to test the postback funtionality I want to use. As far as I know I have left the ddl and the extender alone with the exception of setting the autopostback.


Hmmm. So is there a way then to cancel the binding of the controls contents on the postback? I'm guessing check the Page.IsPostBack and Page.IsCallBack properties, but where would I do that?

Thanks,

BTW. Below are the pieces of code I am working with.

<script runat="server"> protected override void OnLoad(EventArgs e) { base.OnLoad(e); ListBox1.DataSource = GetWordListText(); ListBox1.DataBind(); DropDownList1.DataSource = GetWordListText(); DropDownList1.DataBind(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToLongTimeString(); }</script><asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:DropDownList ID="DropDownList1" runat="server" Width="100px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" /><asp:Label ID="Label1" runat="server" Text="Label" Width="125px"></asp:Label><ajaxToolkit:ListSearchExtender ID="ListSearchExtender2" runat="server" TargetControlID="DropDownList1" PromptCssClass="ListSearchExtenderPrompt"> </ajaxToolkit:ListSearchExtender> </ContentTemplate></asp:UpdatePanel>

If you do do something like this it should work:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if(!IsPostBack ) {
ListBox1.DataSource = GetWordListText();
ListBox1.DataBind();
DropDownList1.DataSource = GetWordListText();
DropDownList1.DataBind();

}
}


Thanks Damian, that did it. Also, thanks so much for you work with the toolkit.