FormView.FindControl():对象引用错误对象、错误、FormView、FindControl

2023-09-03 01:11:49 作者:初 i

我有一个FormView有几个文本框的TR / TD的内线。我试图通过使用.FindControl方法来获取文本框,但它回来了空。 FormView控件总是在编辑模式(所以我总是在EditItemTemplate里),我试图加载查询字符串值插入文本框从previous页到来,所以我确实需要这样的事情发生在Page_Load中。我这样做是在GridView的所有的时间是这样的:

  txtFirstName =(文本框)fvGeneralInfo.FindControl(txtFirstName);
 

或者是这样的:

  txtFirstName =(文本框)fvGeneralInfo.FooterRow.FindControl(txtFirstName);
 

或者是这样的:

  txtFirstName =(文本框)fvGeneralInfo.Rows.FindControl(txtFirstName);
 
未将对象引用设置到对象的实例

怎么办?

 < ASP:FormView控件ID =fvGeneralInfo=服务器
    的DataSourceID =objInstructorDetails
    OnItemCommand =fvGeneralInfo_ItemCommand
    OnItemUpdated =fvGeneralInfo_ItemUpdated
    DefaultMode =编辑
    DataKeyNames =InstructorID>
    < EditItemTemplate中>
        <表>
            &其中; TR>
                < TD合并单元格=2级=管理,副标题为作风=填充左:10px的;>常规信息:LT; / TD>
            < / TR>
            &其中; TR>
                < TD类=管理 -  FieldLabel> ID:< / TD>
                < TD>< ASP:文本框ID =txtInstructorId=服务器的CssClass =管理,文本框只读=真正的文本='<%#绑定(InstructorID)%> />< / TD>
            < / TR>
            &其中; TR>
                < TD类=管理 -  FieldLabel>首先名称:LT; / TD>
                < TD>< ASP:文本框ID =txtFirstName=服务器的CssClass =管理,文本框的文本='<%#绑定(名字)%> />< / TD>
            < / TR>
            < /表>
        < / EditItemTemplate中>
    < / ASP:FormView控件>
 

解决方案

呼叫的DataBind(); 第一。然后的FindControl()

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>

解决方案

Call DataBind(); first. Then FindControl()