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

Load on Demand and Templates

8 Answers 151 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 22 May 2008, 01:29 PM
I'm trying to create a control based on RadComboBox that uses LoD and uses templates to display multicolumn data.

I can't figure out the proper place for the call to the DataBind() method.

I'm creating the control programmatically.
namespace TestBed { 
  public partial class WebForm5 : System.Web.UI.Page { 
    DataSet _Data; 
    public DataSet Data { 
      get { 
        if(_Data == null || _Data.Tables.Count == 0) { 
          _Data = new DataSet(); 
 
          XmlDataDocument doc = new XmlDataDocument(); 
          doc.Load(System.Web.HttpContext.Current.Server.MapPath("App_Data/lstransact.xml")); 
          StringReader sr = new StringReader(doc.SelectSingleNode("//data").OuterXml); 
          XmlTextReader r = new XmlTextReader(sr); 
          _Data.ReadXml(r); 
        } 
        return _Data; 
      } 
    } 
    protected void Page_Load(object sender, EventArgs e) { 
      RadComboBox cb = CreateCombo(); 
      AjaxPanel.Controls.Add(cb); 
    } 
 
 
    RadComboBox CreateCombo() { 
      RadComboBox cb = new RadComboBox(); 
      cb.ID = "combo"
      cb.AllowCustomText = true
      cb.EnableLoadOnDemand = true
      cb.EnableVirtualScrolling = true
      cb.ShowMoreResultsBox = true
      cb.ShowToggleImage = true
      cb.HighlightTemplatedItems = true
      cb.MarkFirstMatch = false
      cb.Width = Unit.Pixel(250); 
      cb.Height = Unit.Pixel(200); 
 
      cb.HeaderTemplate = new HeaderTemplate(); 
      cb.ItemTemplate = new ItemTemplate(); 
 
      cb.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(cb_ItemsRequested); 
      return cb; 
    } 
 
    void cb_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e) { 
      RadComboBox cb = (RadComboBox) o; 
      int offset = e.NumberOfItems; 
      DataRow[] rs = Data.Tables[0].Select(String.Format("trannum>={0}", e.Text == String.Empty ? "0" : e.Text)); 
      DataTable data = new DataTable(); 
      data = Data.Tables[0].Clone();  
      foreach(DataRow r in rs) { 
        data.ImportRow(r); 
      } 
      int itemsPerRequest = 20; 
      int endOffset = offset + itemsPerRequest; 
      if(endOffset > data.Rows.Count) { 
        endOffset = data.Rows.Count; 
      } 
      if(endOffset == data.Rows.Count) { 
        e.EndOfItems = true
      } 
      else { 
        e.EndOfItems = false
      } 
 
      for(int i = offset;i < offset + itemsPerRequest;i++) { 
        RadComboBoxItem itm = new RadComboBoxItem(data.Rows[i]["trannum"].ToString()); 
        cb.Items.Add(itm); 
      } 
      if(data.Rows.Count > 0) { 
        e.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString()); 
      } 
      else { 
        e.Message = "No matches"
      } 
    } 
  } 
  public class HeaderTemplate : ITemplate { 
    void ITemplate.InstantiateIn(Control container) { 
      Table t = new Table(); 
      TableRow r = new TableRow(); 
      TableCell c; 
 
      c = new TableCell(); 
      c.Width = Unit.Pixel(50); 
      c.Text = "Audit No"
      r.Cells.Add(c); 
 
      c = new TableCell(); 
      c.Width = Unit.Pixel(70); 
      c.Text = "Date"
      r.Cells.Add(c); 
 
      c = new TableCell(); 
      c.Width = Unit.Pixel(270); 
      c.Text = "Tenant name"
      r.Cells.Add(c); 
 
      t.Rows.Add(r); 
      container.Controls.Add(t); 
 
    } 
  } 
 
  public class ItemTemplate : ITemplate { 
    void ITemplate.InstantiateIn(Control container) { 
      Table t = new Table(); 
      TableRow r = new TableRow(); 
      TableCell c; 
 
      c = new TableCell(); 
      c.Width = Unit.Pixel(50); 
      c.Text = (string) DataBinder.Eval((container as RadComboBoxItem).DataItem, "trannum"); 
      r.Cells.Add(c); 
 
      c = new TableCell(); 
      c.Width = Unit.Pixel(70); 
      c.Text = (string) DataBinder.Eval((container as RadComboBoxItem).DataItem, "trandate"); 
      r.Cells.Add(c); 
 
      c = new TableCell(); 
      c.Width = Unit.Pixel(270); 
      c.Text = (string) DataBinder.Eval((container as RadComboBoxItem).DataItem, "toDbTenant-name");  
      r.Cells.Add(c); 
 
      t.Rows.Add(r); 
      container.Controls.Add(t); 
    } 
  } 


I've tried calling DataBind() on each RadComboBoxItem a couple of places in the Page_Load event and in the ItemsRequested event to no avail.

What should I be doing?

--
Stuart


8 Answers, 1 is accepted

Sort by
0
Dan
Top achievements
Rank 1
answered on 22 May 2008, 02:03 PM
Perhaps I should have said; what I see is an empty dropdown. It is populated with a table and there is a row for every item added to the ComboBox, it's just that there is no visible data.

Funnily enough, clicking on one of the empty rows gives the ComboBox the value you might expect.

Oh, and my tests suggest that the value of
(container as RadComboBoxItem).DataItem 

is always null, regardless of when I call DataBind() even if I can see it is being called before the template is being invoked.

--
Stuart
0
Dan
Top achievements
Rank 1
answered on 22 May 2008, 02:05 PM
Funnily enough, clicking on one of the empty rows gives the ComboBox the value you might expect.
Obviously, there's nothing mysterious about this; I'm creating the RadComboBoxItem by passing this value. <sigh/>.

--
Stuart
0
Dan
Top achievements
Rank 1
answered on 23 May 2008, 12:27 PM
has anyone else seen this? Or is it just me?

--
Stuart
0
Juan
Top achievements
Rank 1
answered on 26 May 2008, 02:29 AM
Me too, it worked on rad controls for asp.net, but not in prometheus, I dont know what the differences is
0
Veselin Vasilev
Telerik team
answered on 26 May 2008, 03:07 PM
Hi,

Have you tried calling cb.DataBind() method in the ItemsRequested event handler?

Also, if you could isolate the problem in a small and running project will help us find a solution.

Thanks

All the best,
Veskoni
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Dan
Top achievements
Rank 1
answered on 27 May 2008, 07:17 AM
Have you tried calling cb.DataBind() method in the ItemsRequested event handler?
If you read my original message again, you'll see I've already answered this.

Also, if you could isolate the problem in a small and running project will help us find a solution.
You don't think the posted code is enough?

--
Stuart
0
Dan
Top achievements
Rank 1
answered on 27 May 2008, 08:12 AM
See call 140283.

--
Stuart
0
Accepted
Veselin Vasilev
Telerik team
answered on 27 May 2008, 02:59 PM
Hi Stuart,

Thank you for the provided project in the support ticket.

DataItem property is available only during databinding. That is why it is null in your case.

I have made some changes in the ItemsRequested event handler and the InstantiateIn method as well.
Attached, please download the file and give it a try.

I hope this helps.


Best wishes,
Veskoni
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
ComboBox
Asked by
Dan
Top achievements
Rank 1
Answers by
Dan
Top achievements
Rank 1
Juan
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or