Accessing Text Property of FormView Control from Code Behind File

Discussion in 'ASP .Net Web Controls' started by WJB, Dec 1, 2007.

  1. WJB

    WJB Guest

    Hi,

    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?

    Thanks
     
    WJB, Dec 1, 2007
    #1
    1. Advertising

  2. WJB

    WJB Guest

    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?

    Thanks

    "WJB" wrote:

    > Hi,
    >
    > 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?
    >
    > Thanks
    >
     
    WJB, Dec 3, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    594
    Alphonse Giambrone
    Jun 28, 2003
  2. Ric
    Replies:
    9
    Views:
    5,491
    ric carrasquilla
    Mar 3, 2004
  3. =?Utf-8?B?QWxp?=

    accessing Input text values from my code behind

    =?Utf-8?B?QWxp?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    8,459
    Bruce Barker
    Mar 29, 2005
  4. Mia Williams via .NET 247

    accessing Datalist control from Code-behind file

    Mia Williams via .NET 247, Apr 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    603
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Apr 7, 2005
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    491
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page