简体   繁体   English

在DetailsView模板字段中找不到控件

[英]Unable to find control in detailsview template field

I have encountered this error "Object reference not set to an instance of an object." 我遇到了此错误“对象引用未设置为对象的实例”。 when i trying to set a value which i had session from the previous page and set it on a template field. 当我尝试设置上一页中进行会话的值并将其设置在模板字段上时。 when I debugged it, the session value was there(successfully session over). 当我调试它时,会话值在那里(会话成功结束)。 what i am trying to do is to display the value of what I had session on detailsview. 我想做的是在detailsview上显示我的会话的值。

aspx : aspx:

 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False">

    <Fields>
        <asp:TemplateField HeaderText="Order Number " SortExpression="poNum">

            <ItemTemplate>
                <asp:Label ID="test" runat="server" Text='<%# Bind("poNum") %>'></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>

       </Fields>
    </asp:DetailsView>

code behind : 后面的代码:

  protected void Page_Load(object sender, EventArgs e)
  {

    ((Label)DetailsView1.FindControl("test")).Text = Session["poNum"].ToString();
   } 

I was wondering why I was unable to get the control from detailsview?? 我想知道为什么我无法从detailsview获取控件?

EDIT 编辑

    Label aaa = new Label();
    aaa.Text = Session["poNum"].ToString();

    Label orderNum = (Label)DetailsView1.FindControl("test"); // orderNum was null here 
    orderNum.Text = aaa.Text; 

您可以在DetailsView的DataBound事件中访问Label。

Are you calling FindControl before data binding the DetailsView? 在数据绑定DetailsView之前要调用FindControl吗? If so, it will return null - nothing there to find yet. 如果是这样,它将返回null-找不到任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM