FormView.FindControl():对象引用错误 [英] FormView.FindControl(): object reference error

查看:140
本文介绍了FormView.FindControl():对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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()

这篇关于FormView.FindControl():对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆