C# .NET - ListBox's ListItem does not preserve its attributes on postback

Asked By deepi singh on 13-Jul-12 08:03 AM
Peter Bromberg replied to deepi singh on 13-Jul-12 08:10 AM
What "attributes" are you referring to? Can you post some sample code?
deepi singh replied to Peter Bromberg on 13-Jul-12 08:13 AM
 ListBox's ListItem does not preserve its attributes on postback.....

iam doing this below code when page is not postback......

ListItem


lItem = new ListItem("Test1", "1");

lItem.Attributes.Add(

"D", "1");----------------attributes of listbox listitem not holding on postback

listbox.Items.Add(lItem);



i have two listboxes with add and remove buttons.......

In Page_Load event iam binding listitem and attributes to listbox when page is not postback.....

when user is trying to move selected item from left side listbox to right side listbox by clicking add button..at tht time page is getting postback and not able to preserve listbox listitem attributes.....

how to preserve
ListBox's ListItem  attributes on postback.....

pls help me out.......
Chintan Vaghela replied to deepi singh on 13-Jul-12 08:14 AM
Added attribute on Page_Prerender event instead of Page_Load event
deepi singh replied to Chintan Vaghela on 13-Jul-12 08:29 AM
actually iam adding attribute to listbox and binding to listbox in loop in page_load event....

DataTable dt = businessfascade.GetFilteredList(sParams, ref _error).Tables[0];

for (int i = 0; i < dt.Rows.Count; i++)

{


string strName = dt.Rows[i]["Name"].ToString();

string strID = dt.Rows[i]["ID"].ToString();

string strModified = dt.Rows[i]["IsModified"].ToString();

ListItem li = new ListItem(strName, strID);

li.Attributes.Add(

"C", strModified);

if (!lbSelected.Items.Contains(li))

{

lbAvailable.Items.Add(li);

}

}



Jitendra Faye replied to deepi singh on 13-Jul-12 08:38 AM
Refer this-

http://www.eggheadcafe.com/community/vb/14/10463772/listboxs-listitem-not-able-to-preserve-its-value-on-postback.aspx
Chintan Vaghela replied to deepi singh on 14-Jul-12 04:03 AM
Hello 

Try to make your logic as folloiwng way

Concate ID and IsModified , assign to Value field

for (int i = 0; i < dt1.Rows.Count; i++)
       {
         string strName = dt1.Rows[i]["Name"].ToString();
         string strID = dt1.Rows[i]["ID"].ToString() + "|" + dt1.Rows[i]["IsModified"].ToString();
         ListItem li = new ListItem(strName, strID);
         if (!lbSelected.Items.Contains(li))
         {
           lbAvailable.Items.Add(li);
         }
       }


Now on page Prerender event , Split this Value and assign attribute

protected void Page_PreRender(object sender, EventArgs e)
   {
     for (int j = 0; j < lbAvailable.Items.Count; j++)
     {
       string AppliedValue = lbAvailable.Items[j].Value.Split('|')[1].ToString();
       lbAvailable.Items[j].Attributes.Add("C", AppliedValue);
     }
     for (int j = 0; j < lbSelected.Items.Count; j++)
     {
       string AppliedValue = lbSelected.Items[j].Value.Split('|')[1].ToString();
       lbSelected.Items[j].Attributes.Add("C", AppliedValue);
     }
 
   }



deepi singh replied to Chintan Vaghela on 14-Jul-12 11:51 AM
As per ur logic there was a problem if i concate id and is modified in one string...while saving iam trying to save only id..........mine is exisiting code...if i want to do in tht way i mean concating id and ismodified in value field ..i have to change lot of code .......each and every place of value field  i have to split it and use....so many places i have to chnage..is there any other way to hold listbox listitem attributes on postback?
deepi singh replied to Chintan Vaghela on 19-Jul-12 07:11 AM
yeah thanks for ur rly.....the code which u gave is working fine ...can pls work out on the code which i had given in below link...pls helpme out

http://www.eggheadcafe.com/community/csharp/2/10464073/index-was-out-of-range-must-be-nonnegative-and-less-than-the-size-of-the-collection.aspx
deepi singh replied to Chintan Vaghela on 19-Jul-12 07:11 AM
yeah thanks for ur rly.....the code which u gave is working fine ...can pls work out on the code which i had given in below link...pls helpme out

http://www.eggheadcafe.com/community/csharp/2/10464073/index-was-out-of-range-must-be-nonnegative-and-less-than-the-size-of-the-collection.aspx