This week I did alot of work with FormView and of course databinding when I encountered strange cases of data not being posted back to the business objects that it was bound to. It did give me a hard time but in the end the answer was simple. Take a look below:


<asp:formview ID=”frmAddress” runat=”server” DataSourceID=”odsAddress”>

     <ItemTemplate>

        <table cellspacing=”10″>

            <tr runat=”server” id=”trMainAddress”>

                <td style=”width:200px;” valign=”top”>

                    <b style=”font-size:large”>Main address:</b>

                </td>

                <td valign=”top”>

                    <asp:Textbox ID=”txtAddress” runat=”server”

                         Text=’<%# Eval(”AddressAsString”) %>‘>

                   </asp:Textbox >

                </td>

            </tr>

        </table>

    </ItemTemplate>

</asp:formview>

The reason was the striked out code at line 4. Because the table row has a runat attribute and an ID,  it is regarded as a server control.

An example on what happens can be easily understood by the syntax change for getting a refference to the textbox txtAddress:


// table row does not have runat and id

TextBox txt = (TextBox)frmLabelOnly.FindControl(“txtAddress”);

 

// table row has runat and id set

TextBox txt =

   (TextBox)(frmLabelOnly.FindControl(“trMainAddress”).FindControl(“txtAddress”));

 

In the case of the row having an id and runat attribute, the formview simply does not see the textbox because it’s on a level below the controls list. and cannot thus gather the information entered by the user and cannot send this data to the dataobject that it was bound to. And of course it can give you a massive headache :)

Cheers.

Leave a Reply