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?

Surname : <asp:TextBox ID="TextBox2" Text='<%#((MyCustomDataRow)Container.DataItem)["Surname"].EditDataString%>' runat="server" />

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

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.

It would really help to see what the data is and how it is retrieved.

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.

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).

:goof::eye::shifty:Also something that’s not part of this my son is still becoming a clone commander his number is CC-2009:lol::eek::blush:

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.

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.

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.

:smiley: Yeah, there’s a fair amount of .NET that’s not “preferred”, but it’s hard to argue when the magic comes together.

So, the data was relevant after all. :stuck_out_tongue: