3

我有一个在 tr/td 中有几个文本框的 formview。我正在尝试使用 .FindControl 方法获取文本框,但它返回 null。FormView 始终处于编辑模式(因此我始终处于 EditItemTemplate 中),并且我正在尝试将查询字符串值加载到来自上一页的文本框中,因此我确实需要在 page_load 上发生这种情况。我一直在 Gridviews 上这样做:

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

或像这样:

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

或像这样:

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

是什么赋予了?

<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>
4

2 回答 2

3

abatishchev 的回答是正确的,尽管我发现这种变体更简洁一些:它避免了显式调用 DataBind() 。

<asp:FormView ID="fvMember" runat="server" DataSourceID="tblMembers" DefaultMode="Insert" OnDataBound="DataBound">...</asp:FormView>

protected void DataBound(object sender, EventArgs e)
{
    if (fvMember.CurrentMode == FormViewMode.Edit)
    {
        Label lblSubmit = fvMember.FindControl("lblSubmit") as Label;
        ...
    }
}
于 2011-11-24T16:31:25.150 回答
2

先打电话DataBind();。然后FindControl()

于 2010-01-31T11:04:31.910 回答