Accessing Text Property of FormView Control from Code Behind File




I have a TextBox in a FormView ItemTemplate that uses a function to perform
special formatting on the retrieved field content that is bound to the
control as in the following control (the GetFormattedField function is in my
code behind .cs file):

<asp:TextBox ID="txtPhoneNumber" runat="server" Text='<%#
GetFormattedField(Eval("dbField"), "txtField") %>'
Style="z-index: 122; left: 442px; position: absolute; top: 76px"
Width="100px" ReadOnly="True" ></asp:TextBox>

I'm also using an ObjectDataSource as the data source for the FormView, and
have ConflictDetection for the source set to CompareAllValues. For this to
work, I've had to change all my Eval("xxx") bindings to Bind("xxx") within
the ItemTemplate, while adding ReadOnly="True" to all the FormView controls.
I found this to be the only way that the ObjectDataSource will fill the
e.Values array so that these values can be passed in to the DeleteMethod of
the DAL class by the ObjectDataSource, which is done in the ItemDeleting
event of the FormView. Hence my problem:

The e.Values array will only be filled if the ItemTemplate uses Bind rathern
than Eval for evey control within it. However, only Eval is allowed within my
GetFormattedField function. I'm thinking that a way out might be to trap the
Delete button click and in that event replace the "<%#
GetFormattedField(Eval("dbFIeld"), "txtField") %>" string with a "<%#
Bind("dbFIeld") %>" string. However, I cannot get to that Text property to
change it.

1. Does my approach make sense?
2. If so, how do I access this property so I can changed it?
3. If not, how can I otherwise accomplish what I need to do, and that is,
retain the call to the field formatting function when retrieving the row from
the database to display in the FormView, and at the same time, preserve the
Bind call so that the CompareAllValues Delete can operate correctly by
filling up the entire e.Values array?





Here's what I did and it seems to work though any more elegant solution would
be very much appreciated.

I removed the GetFormattedField function and changed the binding in the
FormView control to the simple Bind method. I then moved the logic of the
GetFormattedField function to be invoked on the PreRender event firing. This
gives me the desired formatted look without touching the actual data.
However, it turns out that the e.Values array is filled from the displayed
text rather than the pre-formatting content, so I added an UnformatField
method that is called with the e.Values argument and returns a stripped-down
copy of the data, i.e. it performs the reverse of the GetFormattedField
method. This causes the data to match the content in the database (if no
other user has modified or deleted the row), and Delete works fine with the
CompareAllValues setting.

Even though it works, it appears a little clumsy, so any other thoughts?


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question