无法从后面的代码中找到控件,但能够找到其他控件

时间:2010-11-03 17:20:55

标签: c# asp.net findcontrol

我能够找到所有这些Lables和TextBoxes的控件接受edateTB。这对我来说很奇怪,因为它与sdateTB在同一视图中。那么,当我尝试在其上使用FindControl并且sdateTB返回正确的控件时,为什么edateTB会返回NULL?

在预先感谢,这一次让我感到沮丧。

以下是我收到此错误的代码隐藏。这里有更多的代码,但我只是想将所有内容都包含在我得到NULL错误的部分。

 protected void ReservationDetail_DataBound(object sender, EventArgs e)
{


    TextBox idBox = (TextBox)dvReservationDetail.FindControl("tbID");
    int id = int.Parse(idBox.Text);
    Label specDays = (Label)dvReservationDetail.FindControl("DaysHdr");
    Label weekdays = (Label)dvReservationDetail.FindControl("WeekdaysHdr");
    ListBox dates = (ListBox)dvReservationDetail.FindControl("DatesList");
    Label days = (Label)dvReservationDetail.FindControl("WeekdaysLbl");
    Label sdate = (Label)dvReservationDetail.FindControl("startDateLbl");
    TextBox sdateTb= (TextBox)dvReservationDetail.FindControl("startDateTB");
    Label eDate = (Label)dvReservationDetail.FindControl("endDateLbl");
    TextBox edateTB = (TextBox)dvReservationDetail.FindControl("endDateTB");
}

以下是标签和文本框所在的详细信息视图。

<asp:DetailsView ID="dvReservationDetail" runat="server" DataSourceID="SqlDetail"
                    OnDataBound="ReservationDetail_DataBound" CssClass="detailgrid" GridLines="None"
                    DefaultMode="Edit" AutoGenerateRows="false" Visible="false" Width="100%" OnItemUpdating="ReservationDetail_Updating">
                    <Fields>
                        <asp:TemplateField HeaderText="ID">
                            <EditItemTemplate>
                                <asp:TextBox ID="tbID" runat="server" Text='<%# Bind("id") %>' ReadOnly="true" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField HeaderText="LabName" DataField="labName" />
                        <asp:BoundField HeaderText="Class" DataField="class" />
                        <asp:BoundField HeaderText="Class Size" DataField="classSize" />
                        <asp:TemplateField HeaderText="Software">
                            <EditItemTemplate>
                                <asp:ListBox ID="software" Width="150" runat="server" ReadOnly="true" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Start Time">
                            <EditItemTemplate>
                                <asp:TextBox ID="startTime" runat="server" Text='<%# Bind("startTime") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="End Time">
                            <EditItemTemplate>
                                <asp:TextBox ID="endTime" runat="server" Text='<%# Bind("endTime") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:Label ID="WeekdaysHdr" runat="server" Visible="false" Text="Days Reserved By Week"></asp:Label>
                                <asp:Label ID="DaysHdr" runat="server" Visible="false" Text="Specific Days Reserved"></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:ListBox ID="DatesList" runat="server" Visible="false"></asp:ListBox>
                                <asp:Label ID="WeekdaysLbl" runat="server" Text='<%# Bind("Weekday") %>' Visible="false"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:Label ID="startDateLbl" runat="server" Visible="false" Text="Start Date"></asp:Label></HeaderTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="startDateTB" runat="server" Visible="false" Text="Test"></asp:TextBox></ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:Label ID="endDateLbl" runat="server" Visible="false" Text="End Date"></asp:Label></HeaderTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="endDateTB" runat="server" Visible="false" Text=""></asp:TextBox></ItemTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="repeatTypeTB" runat="server" Visible="false" Text='<%# Bind("repeats") %>'></asp:TextBox></ItemTemplate>
                        </asp:TemplateField>
                        <%-- <asp:CommandField ShowEditButton="true" />--%>
                    </Fields>
                </asp:DetailsView>

2 个答案:

答案 0 :(得分:4)

您的上一个TemplateField标记中看起来有2个ItemTemplate标记。注意:

<asp:TemplateField> 
    <HeaderTemplate> 
        <asp:Label ID="endDateLbl" runat="server" Visible="false" Text="End Date"></asp:Label></HeaderTemplate> 
    <ItemTemplate> 
        <asp:TextBox ID="endDateTB" runat="server" Visible="false" Text=""></asp:TextBox></ItemTemplate> 
    <ItemTemplate> 
        <asp:TextBox ID="repeatTypeTB" runat="server" Visible="false" Text='<%# Bind("repeats") %>'></asp:TextBox></ItemTemplate> 
</asp:TemplateField> 

可能会忽略包含endDateTB的第一个。

应该看起来像:

<asp:TemplateField> 
    <HeaderTemplate> 
        <asp:Label ID="endDateLbl" runat="server" Visible="false" Text="End Date"></asp:Label></HeaderTemplate> 
    <ItemTemplate> 
        <asp:TextBox ID="endDateTB" runat="server" Visible="false" Text=""></asp:TextBox></ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField> 
    <HeaderTemplate> 
        <asp:Label ID="repeatTypeLbl" runat="server" Visible="false" Text="Repeat"></asp:Label></HeaderTemplate> 
    <ItemTemplate> 
        <asp:TextBox ID="repeatTypeTB" runat="server" Visible="false" Text='<%# Bind("repeats") %>'></asp:TextBox></ItemTemplate> 
</asp:TemplateField> 

答案 1 :(得分:0)

所有其他行都是(标签)dvreservationdetail ......

最后一行是(label)this.dvreservationdetail ...