【问题标题】:FormView.FindControl(): object reference errorFormView.FindControl():对象引用错误
【发布时间】:2010-12-01 21:58:07
【问题描述】:

我有一个在 tr/td 中有几个文本框的表单视图。我正在尝试使用 .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>

【问题讨论】:

  • 您没有找到任何正确的答案吗?

标签: c# asp.net .net formview findcontrol


【解决方案1】:

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;
        ...
    }
}

【讨论】:

    【解决方案2】:

    请先致电DataBind();。然后FindControl()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2014-06-05
      • 2013-12-05
      相关资源
      最近更新 更多