Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / June 2006

Tip: Looking for answers? Try searching our database.

DataBinding to SubProperties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason - 21 Jun 2006 22:16 GMT
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.
Walter Wang [MSFT] - 22 Jun 2006 08:14 GMT
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] - 26 Jun 2006 03:29 GMT
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] - 27 Jun 2006 06:40 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.