Problem with binded datalist and control rendering

Last post 03-04-2008 9:39 AM by marko_vucinic. 12 replies.

Sort Posts:

  • Problem with binded datalist and control rendering

    03-03-2008, 4:31 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

    Hi guys,

     I have problem with mvc framework and control rendering...

     
    The problem is that I have binded datalist. Datalist control is binded to list of custom objects.

    Every element in the list showing data from one object in the list. For showing I am using user controls which I load dynamically in Page_Load with: ItemTemplate=Page.LoadTemplate(Controlname)

    On the net I found examples of showing data with Eval method. It works fine for single values in objects - Eval("property name"), but how to show or get values putet in array. My custom objects have two or three properties which are arrays. So when control is loaded and binded, I have to get array, read or elements and generate one string with coma separeted values. I can't do that with eval because I dont't know how many elements I have in array. I tried with casting with Eval, but compiler throw an excepction because he don't recognize Generics list(code: List<CustomClass> list=Eval("propertynameofarray")...

    I try this with declaration of control ViewUserControl<CustomObjectType>, but control doesn't recognize data. 

     Help please!!! :)

     Marko

     

     

     

     

    The problem is that I cannot find a way to parse values in array.


     

    Marko
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 4:16 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

     anyone? :(

    Marko
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 4:31 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    You can get the properties of an arbitrary object  via reflection: 

    class PripertyNameValue {
    public string Name { get; set; }
    public object Value { get; set; }
    }
    public static IEnumerable<PropertyNameValue> GetAllPropertyValues(this object target) {
    var props = target.GetType().GetProperties()
    .Where(p => p.CanRead && 0 == p.GetIndexParameters().Length)
    .Select(p => new PropertyNameValue { Name = p.Name, Value = p.GetValue(target, null) });
    foreach (var prop in props) {
    yield return prop;
    }
    }

    This is created as an extension to object (so needs to be a member of some static class).

    Note, the second part of the where clause is there to avoid indexers (parameterised properties). Different overloads of Type.GetProperties can be used to include non-public or inherited properties.

    Richard
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 5:33 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

     thank you...

    I was thinking that I don't have to use reflection. In some way, it doesn't make sense, I have list of objects and datalist binded with that list. So, every element of datalist get one element from list, one object. Why is so hard to manipulate these values from given object. And why I don't have something like DataItem accessible property, so I can put and manipulate properties of object in easy way manner...

    In a simple words saying: I need the way to user control knows(or to be aware of) object that is passed to that control, and that stands for whole object not just for object properties(Eval and Bind methods)...

    Marko
    Marko
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 5:47 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    marko_vucinic:
    In a simple words saying: I need the way to user control knows(or to be aware of) object that is passed to that control, and that stands for whole object not just for object properties(Eval and Bind methods)...
     

    If you want a control to be aware of the specifics of your object, you have to create a control that knows the specifics of your object (there is no magic ability for MS to create something that will know about your types).

    OTOH you could be thinking of databinding expressions in the display templates supported by Repeater and similar controls?

    Richard
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 5:56 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

    "OTOH you could be thinking of databinding expressions in the display templates supported by Repeater and similar controls?"

     yes

    In the scenario when rendering control manually with RenderControl("pathToControl", data), I have control inherited from ViewUserControl<MyObjectType> and that is not problem.

    But I have DataList which is binded to list of objects so I don't know how control in this case receive that object and how I can get it.

    Marko

     p.s. Thank you Richard for trying to help me
     

    Marko
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 6:15 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    marko_vucinic:
    But I have DataList which is binded to list of objects so I don't know how control in this case receive that object and how I can get it.
     

    Assign you object collection to the control's DataSource property and then call its DataBind method in the code behind (often done in Page_Load). 

    Richard
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 6:27 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

    "Assign you object collection to the control's DataSource property and then call its DataBind method in the code behind (often done in Page_Load)."

     

    Sorry, I didn't understand this, when you say "to the control's DataSource" you mean "to the DataList DataSource"?

    If that so, I already did that, and my problem is, when user control receive data(in this case MyCustomObject), how I receive that object and manipulate his values(properties)

    thank you 

    Marko
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 6:35 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444
    marko_vucinic:
    Sorry, I didn't understand this, when you say "to the control's DataSource" you mean "to the DataList DataSource"?

     I said "to the control's DataSource property": 

    theControl.DataSource = myData;

     

    marko_vucinic:
    when user control receive data(in this case MyCustomObject),

     

    Please expand, what do you mean by "receive data"? Is this user interaction, or when you assign a data source, or something else? 

    Richard
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 7:05 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

    If you mean that theControl is UserControl or ViewUserControl, they don't have property DataSource. If you mean DataList.DataSource, I did that.

    Can you please explain above example with theControl, still I don't get it.

    Expanded receive:

    With "receive" I mean when binding is evaluated(to say like that) and every element in datalist get his data, in this case MyCustomObject object. Or binding under the hood works different?

    Problem with user control here is because dynamically loading. If control is static(hard coded to the page), there will be no problem. But in runtime I decide with which data I fill my list of Objects(they all inherited from same class) and then decide which user control I am loading in the list. Second problem is that in those objecst I have arrays, so because of that I need to user control knows about object.Because of that I cannot put my array data(from object) in ViewData for example, because every object in the list have his own properties-arrays.

    I'm trying best I can to describe you the problem, I hope so I am doing right.

     
    Marko

     

    Marko
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 8:14 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    Would this summary be correct: you want to create a custom control that supports data binding to a collection? If yes then "Developing Custom Data-Bound Web Server Controls for ASP.NET 2.0" is probably a good start.

    marko_vucinic:
    Problem with user control here is because dynamically loading. If control is static(hard coded to the page), there will be no problem. But in runtime I decide with which data I fill my list of Objects(they all inherited from same class) and then decide which user control I am loading in the list.
     

    This should be no problem, you can dynamically create an instance and add it to the form's Controls property (or the Controls property of the applicable composite control -- a placeholder div will allow you to specify the positioning and easily add). 

    Richard
  • Re: Problem with binded datalist and control rendering

    03-04-2008, 8:34 AM
    • Member
      394 point Member
    • JontyMC
    • Member since 05-17-2006, 4:05 PM
    • Posts 144
    Why not do away with the data list and just use a foreach? eg:
    <ul>
    <% foreach (MyItem item in myData) { %>
    <li>
    <h3><%= item.MyProperty %></h3>
    <ul>
    <% foreach (MyChildItem childItem in item.ChildItems) { %>
    <li><%= childItem.ChildProperty %></li>
    <% } %>
    </ul>
    </li>
    <% } %>
    </ul>

     

  • Re: Problem with binded datalist and control rendering

    03-04-2008, 9:39 AM
    • Member
      180 point Member
    • marko_vucinic
    • Member since 03-03-2008, 4:19 AM
    • Belgrade
    • Posts 52

     hey Jonty, that works perfect, thank you!! :)

    anyhow, anyone, solution for databinding scenario? :)

    many thanx

     marko
     

    Marko
Page 1 of 1 (13 items)