views:

1664

answers:

2

I have a formview that has several textboxes inside of tr/td's. I'm trying to get the textboxes by using the .FindControl method but it's coming back null. The FormView is always in Edit mode (so I'm always in the EditItemTemplate) and i'm trying to load querystring values into the textboxes coming from the previous page so I do need this to happen on page_load. I do this on Gridviews all the time like this:

txtFirstName = (TextBox)fvGeneralInfo.FindControl("txtFirstName");

or like this:

txtFirstName = (TextBox)fvGeneralInfo.FooterRow.FindControl("txtFirstName");

or like this:

txtFirstName = (TextBox)fvGeneralInfo.Rows.FindControl("txtFirstName");

What gives?

    <asp:FormView ID="fvGeneralInfo" runat="server" 
        DataSourceID="objInstructorDetails"
        OnItemCommand="fvGeneralInfo_ItemCommand"
        OnItemUpdated="fvGeneralInfo_ItemUpdated"  
        DefaultMode="Edit"
        DataKeyNames="InstructorID" >
        <EditItemTemplate>
            <table>
                <tr>
                    <td colspan="2" class="Admin-SubHeading" style="padding-left:10px;">General Info:</td>
                </tr>
                <tr>
                    <td class="Admin-FieldLabel">ID:</td>
                    <td><asp:TextBox ID="txtInstructorId" runat="server" CssClass="Admin-Textbox" ReadOnly="true" Text='<%# Bind("InstructorID") %>' /></td>
                </tr>
                <tr>
                    <td class="Admin-FieldLabel">First Name:</td>
                    <td><asp:Textbox ID="txtFirstName" runat="server" CssClass="Admin-Textbox" Text='<%# Bind("FirstName") %>' /></td>
                </tr>
                </table>  
            </EditItemTemplate>
        </asp:FormView>
A: 

you need to use formview Databound event instead of load event to set values, try using this

 protected void frm_DataBound(object sender, EventArgs e)
{
    if (frm.CurrentMode == FormViewMode.Edit)
    {
        txtFirstName = (TextBox)fvGeneralInfo.FindControl("txtFirstName");
    }
}
Muhammad Akhtar
Didn't work for me. I also tried overriding OnPreRender..
Tone
plz check now.., it should work.
Muhammad Akhtar
Hmm... still not working. It is indeed in EditMode as if falls in the if statement, but still coming back null. I also tried a TextBox in a regular <ItemTemplate>, but got the same results.
Tone
Could you email me your code on [email protected], will look into your code.
Muhammad Akhtar
A: 

Call DataBind(); first. Then FindControl()

abatishchev