DataBinding to SubProperties

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default DataBinding to SubProperties

    I have built a custom control that has, as one of its properties, a
    collection of another custom control. So controlA has property of type
    controlBCollection. At design time it looks like:

    <asp:controlA runat="server">
    <asp:controlB commandName="Save"
    commandArg="<%#DataBinder.Eval(Container, "DataItem.pkID")%>" />
    </asp:controlA>

    In a DataGrid i have a TemplateColumn with the above as the ItemTemplate
    After binding there is nothing for the commandName or commandArg properties.
    string.empty is the value for these properties.

    if i bind the same thing directly to controlA, say...ToolTip, the values
    show up.

    Somehow the values from the datasource are not getting down to the
    subproperties.

    Any help would be greatly appreciated. Let me know if you need clarification
    or even the source code and i'll be happy to provide.


    Jason Guest

  2. Similar Questions and Discussions

    1. Subproperties
      Hi, I've been learning how to create custom web controls and have been having problems with subproperties. The first problem is that you can...
    2. databinding
      have two Dropdownlist and one which is containing States and one containing Cities. I know how to do all the little databinding task in visual...
    3. Collection editor or subproperties
      I created som custom controls with subproperties like... Buttons --|-- Button1 |-- Button2 I would like to put subproperties in the...
    4. DataGrid Custom Column Error when DataBinding "does not contain a definition for 'DataBinding'"
      I am creating a custom column that inherits from DataColumnGrid. When I attempt Databind to a property of the custom column, I get the the error: ...
    5. Listbox DataBinding
      I am trying to bind data from a database to a DropDownListBox on an ..aspx page. I want to display 2 fields in listbox (listbox's .DataTextField...
  3. #2

    Default RE: DataBinding to SubProperties

    Hi,

    Thank you for your post.

    If it's possible, you can just post some key class here. Or you can send
    the repro project to me. My email address is my newsgroup account (removing
    the 'online.' part). I'll be glad to work with you on this issue.

    Regards,
    Walter Wang
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Wang [MSFT] Guest

  4. #3

    Default RE: DataBinding to SubProperties

    Hi,

    I've got your sample code and thank you for your effort on this.

    Currently I am still performing research on this issue and will get back to
    you as soon as possible. I appreciate your patience.

    Regards,
    Walter Wang (wawang@online.microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Wang [MSFT] Guest

  5. #4

    Default RE: DataBinding to SubProperties

    Hi,

    Sorry for reply so late.

    When using Data-Binding expressions (<%# ... %>) to declaratively set
    properties, it internally will use the control's DataBinding event to set
    the property. So the method DataBind() must be called internally to make
    the Data-Binding expressions work. The DataBind() call will be recursive,
    i.e., calling Page.DataBind() will cause all child controls' DataBind()
    gets called.

    When using "DataBinder.Eval(Container, ...)", the Container is a keyword
    which will be interpreted to Control.BindingContainer by the page Parser,
    you can verify this by inspecting the generated page class source code.
    Control.BindingContainer will normally return Control.NamingContainer,
    unless it implements INonBindingContainer (which is an internal interface):

    public Control BindingContainer
    {
    get
    {
    Control control1 = this.NamingContainer;
    while (control1 is INonBindingContainer)
    {
    control1 = control1.BindingContainer;
    }
    return control1;
    }
    }

    In your sample code, the ImgToolBar has attribute ParseChild(true), so that
    the ImgToolBarButtons enclosed in its tag are parsed as properties of the
    ImgToolBar rather than as child controls. Based on my research, the
    DataBind() method of ImgToolBarButton never gets called. Even the
    DataBind() method of ImgToolBarButton gets called, since its
    BindingContainer will be ImgToolBar (rather than DataGridItem when the
    binding occurred on ImgToolBar), the evaluation of "DataItem.pkID" will not
    succeed.

    I think the correct and complicate solution to this problem would be
    implementing the ImgToolBar as a data-bound control (inherit from
    ListControl or CompositeDataBoundControl).

    For now, since what you needed is the current row's record ID, one possible
    workaround would be:
    1) Bind the ID to the ImgToolBar, we can create a general purpose Tag
    property for the ImgToolBar:

    public object Tag
    {
    get { return ViewState["Tag"]; }
    set { ViewState["Tag"] = value; }
    }

    And bind the "pkID" field to this property:

    <asp:TemplateField>
    <ItemTemplate>
    <c:ImgToolBar ID="toolbar1"
    Tag=<%# DataBinder.Eval(Container, "DataItem.ProductID") %>
    runat="server">
    <c:ImgToolBarButton ID="button1" runat="server"
    OnButtonClick="OnCommandEvent" ButtonType="copy" />
    </c:ImgToolBar>
    </ItemTemplate>
    </asp:TemplateField>

    2) Then we can get this Tag from ImgToolBarButton's Click event:

    protected void OnCommandEvent(object sender, CommandEventArgs e)
    {
    if (sender is ImgToolBarButton)
    {
    ImgToolBar tb = (sender as ImgToolBarButton).Parent as
    ImgToolBar;
    int id = (int) (tb.Tag);
    string name = e.CommandName;
    }
    }

    Hope this helps. Please feel free to post here if anything is unclear.


    Regards,
    Walter Wang (wawang@online.microsoft.com, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Walter Wang [MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139