无法从代码隐藏中找到控件,但能够找到其他控件

发布于 2024-09-30 15:24:00 字数 5381 浏览 3 评论 0原文

我能够找到所有这些标签和文本框的控件都接受 edateTB。这对我来说很奇怪,因为它与 sdateTB 具有相同的视图。那么,当我尝试在 edateTB 上使用 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>

I am able to find the controls of all of these Lables and TextBoxes accept the edateTB. This is strange to me because it is in the same view as the sdateTB. So why would edateTB return NULL when I try to use FindControl on it and sdateTB return the correct control?

Thanks in Advance, this one has been frustrating me for awhile.

Here is the codebehind where I am getting this error. There is more code in this, but I just thought I would include everything up to the part where I get the NULL error.

 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");
}

Here is the Detailsview where the labels and Textboxes are.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-10-07 15:24:00

您的最后一个 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> 

It looks like you have 2 ItemTemplate tags in your last TemplateField tag. Notice:

<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> 

It is possible that the first one that contains endDateTB is being ignored.

It should look like:

<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> 
江南烟雨〆相思醉 2024-10-07 15:24:00

所有其他行都是 (label)dvreservationdetail...

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

all the other lines are (label)dvreservationdetail...

the last line is (label)this.dvreservationdetail...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文