SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Custom Bind() implementation

    When using a databound control the Bind() method does some magic to first extract the current value from the datasource and then pass an update back to the datasouce when an update is performed.

    Now I have created a custom datasource and am supplying my own custom data which all works fine. For read only the following code works a treat. As you can see I am accessing both a named index into the collection and a property.

    Now when using Bind() I cannot find a way to accomplish this. Bind() would appear to look for either an object property or a database row column (not sure how it does this) and I cannot find a way to change this.

    It shouldn't be that difficult because the hard part is obtaining the data, which my code below can already do. The changes are passed back as a dictionary which should be simple. But I need a way to write my own bind method and I haven't managed to find out how.

    Any ideas?

    Code:
    Surname : <asp:TextBox ID="TextBox2" Text='<%#((MyCustomDataRow)Container.DataItem)["Surname"].EditDataString%>' runat="server" />
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,869
    Mentioned
    38 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I am not sure I understand your question correctly, but it sounds like you want the OnDataBound event.

    Then you code would look like this:

    Surname : <asp:TextBox ID="TextBox2" Text="" runat="server" />

    Then in your OnItemDataBound method, you have this:
    MyCustomDataRow = e.Item.DataItem as MyCustomDataRow;
    TextBox txt = e.Item.FindControl("TextBox2") as TextBox;

    //DO WHATEVER YOU NEED TO DO HERE
    txt.Text = "whatever you want here";

    I hope that is what you were looking for

  3. #3
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks NightStalker - I think what you're described in the manual implementation of DataBinder.Eval() which I have got working.

    I need Bind() to work so that the data becomes available for an update after the user has modified it.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  4. #4
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dale_burrell View Post
    ... I need Bind() to work so that the data becomes available for an update after the user has modified it.
    It would really help to see what the data is and how it is retrieved.

  5. #5
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the data itself is not that relevant and is far too complex to try and express here.

    Its the mechanism I an trying to understand and see if there is an alternative to the provided Eval() & Bind() methods.

    Eval() & Bind() both appear to assume that the data either resides in a property on an object or a column in a DataTable.

    Also Bind() seems to carry out all sorts of magic which then results in the values from the controls being placed into a dictionary which is then passed the the data source to carry out its update.

    If there was a way to do what Bind() does but be able to be modified then that would solve my problem. Alternatively if there was a way to tell Bind() to look in a non-default location for the data that also would solve my problem.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  6. #6
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on how your custom object is implemented. It needs a function that you raise manually when the user interacts with the UI (like a save button after they change data), or an event which is triggered automatically when changes are made to the data. If there is implementation for either of these scenarios from an object that you're inheriting from, you can just use it (while I find inheritance tends to be overused and can make things confusing, for those people who enjoy it, THIS is exactly the reason it's valuable because it eliminates a lot of coding for duplicate functionality). Otherwise, you can provide custom implementation, but the code you write to execute what you want to do is the relatively trivial part of the process. The more important thing is determining how to trigger that code (again, either by attaching a function call to a portion of your UI, or by using an event to trigger your code).

    Also something that's not part of this my son is still becoming a clone commander his number is CC-2009
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  7. #7
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Chroniclemaster1 - but I think you're referring to something quite different. I am using a FormView and have written a custom DataSource (which is more similar to the SqlDataSource then the ObjectDataSource). My Custom DataSource works just fine against the model I use behind that.

    However I cannot make the FormView bind my data using Bind() because it appears to assume a structure that I am not using.

    Bind() appears to carry out a bit of magic that most people take for granted, and I have yet to find an example of anyone changing that behaviour. I could in theory use the OnItemUpdating event of the form view to read out the data from my control and pass the values to the datasource but there are no clues as to how I would identify which control related to which field as that seems to be something controlled by Bind().

    Hope that makes more sense.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  8. #8
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification Dale. I think I understand the problem though I don't have a solution for it. As you put so nicely, a number of the .NET components perform "magic" that is not well documented (or worse overdocumented... MSDN can on occasions be a life saver, but you can also find multiple articles on one topic which are directly contradictory or highly misleading). If there is a way to reprogram the Bind() action, I'm not familiar with it. .NET components are a convenient way to accelerate construction when you want to do *exactly* what they are designed to do. However, they frequently aren't usable when you need custom requirements like this. Hopefully, someone will show up who's tested a solution that works.

    Otherwise you can spend weeks trying various syntaxes randomly in the hopes that something may work (though you usually won't understand why it works or why all the others didn't, and a common end is that you ultimately give up with the assumption that no working syntax exists).

    While I like C# as my preferred programming language, I tend to avoid most of the BCL for the above reasons because I find it "magically" difficult to create most types of custom functionality. While the BCL covers a lot of common tasks, I find it all too commonly falls flat in scenarios similar to this and so I write most of my projects using good old-fashioned custom OOP code that's been around for decades just the way that a lot of PHP and Java programmers do. While it results in longer construction times for some functions, the time savings are not that significant and there's nothing "magical" about good old-fashioned code. You know exactly how it works, exactly how to tweak and customize it, and most importantly how to maintain it, which usually more than makes up any time "lost" by performing construction yourself. At least that's been my experience. MS spends a lot of time building and bragging about features that let you do more with fewer lines of code. However, as you're discovering that comes at the price of assuming what it is that you want to do and it's a royal pain in the @#$^ when that's not what you want to do.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  9. #9
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers dude... in the end the way I worked around it was to build a datatable structure using asp.net classes to represent my data temporarily while data binding. Its extra overhead and not my preferred way but all the magic works as it should now.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  10. #10
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, there's a fair amount of .NET that's not "preferred", but it's hard to argue when the magic comes together.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  11. #11
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dale_burrell View Post
    Cheers dude... in the end the way I worked around it was to build a datatable structure using asp.net classes to represent my data temporarily while data binding. Its extra overhead and not my preferred way but all the magic works as it should now.
    So, the data was relevant after all.


Bookmarks

Posting Permissions

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