Migration from DataView to ListView


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Migration from DataView to ListView

  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Question Migration from DataView to ListView

    I'm working on converting a DataView object to a ListView object. What I had previously was working but the source data became so long so I decided to move to ListView so I could do multiple columns.

    I'm running into a few problems though. When I was working with DataView I was able to set each Item's checkbox by using OnItemDataBound. The same method for ListView is not working:

    Code:
        protected void lv1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                try
                {
                    CheckBox chkBox = (CheckBox)e.Item.FindControl("chkSelect");
                    using (ListViewDataItem lvdi = (ListViewDataItem)e.Item)
                    {
                        if (lvdi != null)
                        {
                            chkBox.ID = "dg1_" + DataBinder.Eval(lvdi.DataItem, "ServerID");
                            chkBox.Text = (string)DataBinder.Eval(lvdi.DataItem, "Server_Name");
                            chkBox.TabIndex = (short)(lvdi.DataItemIndex + 6);
                        }
                    }
                }
                catch { }
            }
        }
    Upon debugging, I can see chkBox.ID getting set to the new value "dg1_1","dg1_2", etc.
    When I look in the source of the page, the first checkbox is name="lv1$ctrl0$ctl00$dg1_1"
    However, when I do a postback using a button onClick="doPostBack"

    Code:
        protected void doPostBack(object sender, EventArgs e)
        {
            ListView tdg1 = (ListView)this.FindControl("inputform1").FindControl("lv1");
    ...
            foreach (ListViewItem dgi in tdg1.Items)
            {
                counta += dgi.Controls.Count;
                foreach (Control cc in dgi.Controls)
                {
                    debug += cc.ID;
                    if (cc.GetType() == typeof(CheckBox))
                    {
                        CheckBox cb = (CheckBox)cc;
                        if (cb.Checked == true)
                        {
                            Regex r = new Regex("dg1_(?<id>[0-9]+)");
                            Match m = r.Match(cb.ID);
                            if (m.Success)
    ...
    cb.ID is always chkSelect, and not what I specified in the ItemDataBound.

    Has anyone experienced this before? Am I completely insane doing it this way?

  2. #2
    Join Date
    Oct 2009
    Posts
    3
    An update to this, I have also tried the DataList control, but I can't seem to get anything to stick.

    I set both ID and Attributes["sid"] in the ItemDataBound function, and when its posted back both are not keeping their values.

    Another note: If I remove the CheckBox's ID tag from the aspx file, in the postback it returns null, which I thought was impossible. *sigh*

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Heh, Ooops. I took another look at the debugging for the assignment of chkBox.ID, it was failing due to a missing cast.

    int d = (int)DataBinder.Eval(dli.DataItem, "ServerID");
    c.ID = "dg1_" + d.ToString();

    Unfortunately, that still didn't work as the ID still shows up as "chkBox" in the postback. However, now that it is getting to where it assigns the attribute, I can now pick up cb.Attributes["sid"] from the postback and all is well.

Similar Threads

  1. Replies: 1
    Last Post: 09-16-2006, 08:37 PM
  2. ListView Control and Custom Sorting
    By Birhanu in forum VB Classic
    Replies: 0
    Last Post: 11-09-2001, 04:26 PM
  3. ListView Control and Custom Sorting
    By Birhanu in forum VB Classic
    Replies: 0
    Last Post: 11-09-2001, 04:26 PM
  4. Listview boxes
    By Kenneth in forum VB Classic
    Replies: 0
    Last Post: 04-24-2001, 12:04 PM
  5. Re: Listview Item structure HELP.
    By Diego in forum VB Classic
    Replies: 1
    Last Post: 08-28-2000, 09:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center