Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why can't I 'get to' my text box?

Posted on 2013-01-31
13
Medium Priority
?
352 Views
Last Modified: 2013-03-01
I've created a page with some controls on it and I'm trying to access a couple TextBox controls on the page in the code behind, which it seems as though I've been able to do this before without any problem either by referencing the control ID, or creating the container objects that the control lives in and then using the ".FindControl" method on that container object. Below are my ASP page and the snippet of the code behind that I'm trying to use to access a TextBox. For some reason I'm not able to get to the text box by directly referencing it's ID, or through the container that it lives in. What am I doing wrong or what am I missing?

ASP page
<%@ Page Title="" Language="C#" MasterPageFile="~/site.master" AutoEventWireup="true"
    CodeFile="StudentTime.aspx.cs" Inherits="StudentTime" %>

<%@ Register Src="~/UserControls/ListOfStudentsGrid.ascx" TagName="StudentsListGrid"
    TagPrefix="sttc" %>
<%@ Register Src="~/UserControls/EditTimeGrid.ascx" TagName="EditTimeGrid" TagPrefix="sttc" %>
<%@ Register Src="~/UserControls/TimeGrid.ascx" TagName="AddTimeGrid" TagPrefix="sttc" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            var updateWait = setTimeout(fadeUpdate, 6000);
            function fadeUpdate() {
                $("div.UpdatedMessage").fadeOut(1000);
            }
        });
    </script>
    <style type="text/css">
        fieldset
        {
            margin-bottom: 1em;
        }
        input
        {
            display: block;
            margin-bottom: .25em;
        }
        #print-output
        {
            width: 100%;
        }
        .print-output-line
        {
            white-space: pre;
            padding: 5px;
            font-family: monaco, monospace;
            font-size: .7em;
        }
        .studentIdInput, .prompt, .divStudentIDsTxtbx, 
        .divBtnUpdateRequiredHours, .divRequireHours, .AddEditButton
        {
            float: left;
        }
        .studentIdInput
        {
            margin: -3px 9px 0 5px;
        }
        filterControls .myWLocationDD
        {
            width: 650px;
        }
        .pnlDateRange
        {
            margin: 10px 0 10px 0;
            width: 100%;
        }
        .Newshipment
        {
            width: 100%;
        }
        .findStudents
        {
            float: left;
            margin: 15px 0 0 60px;
        }
        .timeBox
        {
            width: 62px;
        }
        .timeContainer
        {
            float: left;
            margin: -16px 0 0 10px;
        }
        .secondFilters
        {
            padding: 0 0 0 80px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="Server">
    <div id="MainContainer">
        <div class="MainContent">
            <div class="TableContainer">
                <div class="FiltersContainer clearfix">
                    <div class="Filters clearfix" runat="server" id="div4" style="float: left;">
                        <div class="filterControls" style="width: 100%;">
                            <div class="firstFilters" style="float: left; width: 650px;">
                                <strong>Choose action:</strong>
                                <div id="Div1" runat="server" style="clear: none">
                                    <ul>
                                        <li>
                                            <asp:RadioButton ID="rbtnAddTime" runat="server" AutoPostBack="true" Checked="false"
                                                GroupName="AddEdit" CssClass="Radio" Text="Add Time" OnCheckedChanged="rbtnAddTime_OnCheckedChanged" /></li>
                                        <li>
                                            <asp:RadioButton ID="rbtnEditTime" runat="server" AutoPostBack="true" Checked="false"
                                                GroupName="AddEdit" CssClass="Radio" Text="Edit / Delete Time" OnCheckedChanged="rbtnEditTime_OnCheckedChanged" /></li>
                                    </ul>
                                </div>
                                <strong>Filter by:</strong>
                                <div id="Div6" runat="server" style="clear: none">
                                    <ul class="Three">
                                        <li>
                                            <asp:RadioButton ID="rbtnStdntId" runat="server" AutoPostBack="true" GroupName="Filters"
                                                CssClass="Radio" Text="<span>Student ID</span>" Checked="false" OnCheckedChanged="rbtnStdntId_OnCheckedChanged" /></li>
                                        <li>
                                            <asp:RadioButton ID="rbtnFirstNameLike" runat="server" AutoPostBack="true" GroupName="Filters"
                                                CssClass="Radio" Text="<span>First Name Like</span>" Checked="false" OnCheckedChanged="rbtnFirstNameLike_OnCheckedChanged" /></li>
                                        <li>
                                            <asp:RadioButton ID="rbtnLastNameLike" runat="server" AutoPostBack="true" Checked="false"
                                                GroupName="Filters" CssClass="Radio" Text="<span>Last Name Like</span>" OnCheckedChanged="rbtnLastNameLike_OnCheckedChanged" /></li>
                                    </ul>
                                </div>
                                <asp:Panel runat="server" ID="pnlSearchBox" CssClass="myWLocationDD" Width="700px">
                                    <asp:TextBox ID="tbxSearchTerm" runat="server" Width="200px" CssClass="SearchOriginCity" />
                                    <asp:RequiredFieldValidator runat="server" ID="rfvSearchTearm" ControlToValidate="tbxSearchTerm"
                                        ErrorMessage="Search term cannot be blank." Display="dynamic" Text="<span class='ErrorMe'></span>"
                                        CssClass="ErrorIndicator" Enabled="true" ValidationGroup="ValidateAccountDetails" />
                                    <%--<asp:Button ID="btnFindTimeRecs" runat="server" Text="Find Time Records" CssClass="button small green"
                            OnClick="findTimeRecRecs_Click"></asp:Button>--%>
                                </asp:Panel>
                            </div>
                            <div class="secondFilters" style="float: left;">
                                <div class="teamsDropDown" style="float: left;">
                                    <div id="Div2" runat="server" style="float: left;">
                                        <strong>OR Select a team:</strong>
                                        <asp:DropDownList runat="server" ID="ddlSportTeam" Width="210px" AutoPostBack="true"
                                            CssClass="ddlSportTeam" OnSelectedIndexChanged="ddlSportTeam_OnSelectedIndexChanged">
                                        </asp:DropDownList>
                                    </div>
                                </div>
                            </div>
                            <div class="clear">
                            </div>
                            <div class="DateRange" style="float: left; width: 100%;">
                                <asp:Panel runat="server" ID="pnlDateRange" CssClass="pnlDateRange">
                                    <div class="dateRange" style="width: 100%;">
                                        <strong>Select a date range:</strong>
                                        <%--<asp:TextBox runat="server" ID="tbxStartDate"></asp:TextBox>--%>
                                        <div class="Newshipment">
                                            <div class="Panel clearfix">
                                                <div class="ShipmentLabelBox clearfix" style="width: 100%;">
                                                    <div class="LabelDiv">
                                                        <asp:Panel runat="server" ID="pnlReadyDate1" Visible="true" CssClass="TopReadyDate"
                                                            Style="float: left; margin-left: 15px;">
                                                            <label>
                                                                Start Date
                                                            </label>
                                                            <div class="Calendar">
                                                                <asp:TextBox runat="server" ID="txtReadyDate1" CssClass="required" Width="100px" /></div>
                                                        </asp:Panel>
                                                        <asp:Panel runat="server" ID="pnlToDate1" Visible="true" CssClass="TopReadyDate"
                                                            Style="float: left; margin-left: 50px;">
                                                            <label>
                                                                End Date
                                                            </label>
                                                            <div class="Calendar">
                                                                <asp:TextBox runat="server" ID="txtToDate1" CssClass="required" Width="100px" /></div>
                                                        </asp:Panel>
                                                    </div>
                                                    <div class="findStudents" style="float: left;">
                                                        <asp:Button ID="btnFindTimeRecs" runat="server" Text="Find / Add Time Records" CssClass="button small green"
                                                            OnClick="findTimeRecRecs_Click"></asp:Button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </asp:Panel>
                            </div>
                            <div class="clear">
                            </div>
                            <div class="AddEditButton">
                                <asp:Panel runat="server" ID="pnlAddEdit" CssClass="myWLocationDD" Width="700px"
                                    Visible="false">
                                    <asp:Button ID="btnAddEdit" runat="server" Text="" CssClass="button small green"
                                        OnClick="addEditTime_Click"></asp:Button>
                                    <asp:Label ID="lblNoRecordsFound" runat="server" Text="No records found" CssClass="PagerControl"
                                        Visible="false"></asp:Label>
                                </asp:Panel>
                            </div>
                        </div>
                        <div class="clear">
                        </div>
                        <div class="AddEditButton">
                            <div class="divInstrLbl" style="margin: 0 0 25px 0;">
                                <asp:Label runat="server" Style="margin: 0 0 25px 0;" ID="lblEnterStudentIDs" Text="Enter Student IDs, separated by commas, to update required hours for. Then, enter required hours, and click 'Update'."></asp:Label>
                            </div>
                            <asp:Panel runat="server" ID="pnlUpdateReqHours" CssClass="myWLocationDD" Width="700px" Visible="true">
                                <div class="divUpdateReqHours" style="width: 100%;">
                                    <div class="divStudentIDsTxtbx" style="margin: 0 25px 0 0;">
                                        <asp:TextBox ID="txtbxStudentIDs" runat="server" Rows="5" Width="200px" Height="100" TextMode="MultiLine">Enter Student IDs here.</asp:TextBox>
                                    </div>
                                    
                                    <div class="divRequireHours" style="margin: 0 25px 0 0;">
                                        <asp:TextBox ID="txtbxRequiredHours" runat="server" Width="40px" MaxLength="2"></asp:TextBox>
                                    </div>
                                    <div class="divBtnUpdateRequiredHours">
                                        <asp:Button ID="btnUpdateRequiredHours" runat="server" Text="Update" CssClass="button small green"
                                            OnClick="btnUpdateRequiredHours_Click"></asp:Button>
                                    </div>
                                </div>
                            </asp:Panel>
                        </div>
                        <div class="updateAlert" style="width: 662px;">
                            <asp:Panel runat="server" ID="pnlUpdateConfirmation" Visible="false" CssClass="UpdatedMessage Alert SavedSmall"
                                Style="width: 662px;">
                                <h3>
                                    <asp:Label runat="server" ID="lblConfirmationMessage"></asp:Label></h3>
                            </asp:Panel>
                        </div>
                    </div>
                </div>
            </div>
            <div class="Container clearfix">
                <asp:Panel runat="server" ID="pnlTimeInfoEditAdd" CssClass="ShipmentDialog">
                    <asp:Label runat="server" ID="lblStudentOrTeam"></asp:Label>
                </asp:Panel>
            </div>
            <div class="Container clearfix">
                <asp:Panel runat="server" ID="pnlAddInOutRecords" CssClass="ShipmentDialog" Visible="false">
                    <div class="LabelDiv">
                        <asp:Panel runat="server" ID="pnlReadyDate" Visible="true" CssClass="TopReadyDate"
                            Style="float: left; margin-left: 15px; width: 250px;">
                            <label>
                                Start Date
                            </label>
                            <div class="Calendar" style="float: left;">
                                <asp:TextBox runat="server" ID="txtReadyDate" CssClass="required" Width="100px" /></div>
                            <div class="timeContainer">
                                <label>
                                    Start Time
                                </label>
                                <asp:TextBox ID="tbxTimeIn" runat="server" CssClass="timeBox"></asp:TextBox>
                                <act:MaskedEditExtender ID="txtdt_MaskedEditExtender" runat="server" AcceptAMPM="true"
                                    Enabled="True" TargetControlID="tbxTimeIn" ErrorTooltipEnabled="true" Mask="99:99"
                                    MaskType="Time" ClearMaskOnLostFocus="false" AutoComplete="true">
                                </act:MaskedEditExtender>
                            </div>
                        </asp:Panel>
                        <asp:Panel runat="server" ID="pnlToDate" Visible="true" CssClass="TopReadyDate" Style="float: left;
                            width: 250px;">
                            <label>
                                End Date
                            </label>
                            <div class="Calendar" style="float: left;">
                                <asp:TextBox runat="server" ID="txtToDate" CssClass="required" Width="100px" /></div>
                            <div class="timeContainer">
                                <label>
                                    End Time
                                </label>
                                <asp:TextBox ID="tbxTimeOut" runat="server" CssClass="timeBox"></asp:TextBox>
                                <act:MaskedEditExtender ID="MaskedEditExtender1" runat="server" AcceptAMPM="true"
                                    Enabled="True" TargetControlID="tbxTimeOut" ErrorTooltipEnabled="true" Mask="99:99"
                                    MaskType="Time" ClearMaskOnLostFocus="false" AutoComplete="true">
                                </act:MaskedEditExtender>
                            </div>
                        </asp:Panel>
                        <asp:Panel runat="server" ID="pnlNotes" Visible="true" CssClass="TopReadyDate" Style="float: left;
                            margin-left: 50px;">
                            <label>
                                Notes
                            </label>
                            <asp:TextBox ID="tbxsigninoutnotes" runat="server" Width="150px" Rows="2" TextMode="MultiLine"></asp:TextBox>
                        </asp:Panel>
                        <asp:Panel runat="server" ID="pnlProgramAttended" Visible="true" CssClass="TopReadyDate"
                            Style="float: left; margin-left: 50px;">
                            <label>
                                Program Attended
                            </label>
                            <asp:TextBox ID="tbxProgramAttended" runat="server" Width="150px" Rows="2" TextMode="MultiLine"></asp:TextBox>
                        </asp:Panel>
                    </div>
                </asp:Panel>
                <div>
                    <sttc:StudentsListGrid ID="StudentListGrid" runat="server" ResultType="1" ucAccessLevel='<%# Eval("Page.AccessLevel") %>' />
                </div>
                <div>
                    <sttc:AddTimeGrid ID="AddTimeGrid" runat="server" ResultType="1" ucAccessLevel='<%# Eval("Page.AccessLevel") %>' />
                </div>
            </div>
        </div>
    </div>
</asp:Content>

Open in new window


C# code behind
    protected void btnUpdateRequiredHours_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder cph = new ContentPlaceHolder();
        cph = (ContentPlaceHolder)this.Page.FindControl("MainContent");
        Panel pnlUpdateReqHours = cph.FindControl("pnlUpdateReqHours") as Panel;

        TextBox txtbxRequiredHours = pnlUpdateReqHours.FindControl("txtbxRequiredHours") as TextBox;

        ResetFilter();
    }

Open in new window

0
Comment
Question by:Michael Sterling
13 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 1000 total points
ID: 38839840
 TextBox txtbxRequiredHours = pnlUpdateReqHours.FindControl("txtbxRequiredHours") as TextBox;

Open in new window



is this line returns null?
did u get valid panel?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38840023
@sedgwick: yes that's the problem, for some reason, it's not finding my panel. the panel is there in the ASP code so i'm not sure why it won't / can't find it.
0
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 1000 total points
ID: 38840273
Can't you just access the textbox by

txtbxRequiredHours.Visible = false; ? I believe it's the same code-behind.

Is it necessary to do a findcontrol here?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Michael Sterling
ID: 38840430
@informaniac: I've tried that, since that's seemed to work for me almost every time in the past. but when i try to do that, intellisence doesn't find it or if i forcefully put it in, of course I get an error at compile time.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38840631
@informaniac & sedgwick: I found my problem. I apparently had to go through the master page (for some reason unbeknownst to my self) to find the control(s) / container(s). so instead of:

cph = (ContentPlaceHolder)this.Page.FindControl("MainContent");

i needed to use:

cph = (ContentPlaceHolder)this.Page.Master.FindControl("MainContent");

if either of you can exlpain why this was the case, i'll gladly award you the points.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38840667
Is the textbox inside the aspx page? And is the event written inside the codebehind of the aspx page?

If that is the case, then i don't think this is the proper way to do it. Did u check the designer file of the aspx page?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38840865
@informaniac: i don't have a designer file. Just the aspx page and the .cs file. The text box is on / inside the aspx page and the event is the OnClick event of the button on the page so that code lives in the C# (.cs) file that is tied to the aspx page see the following at the top of the aspx file I included in my original post.

CodeFile="StudentTime.aspx.cs"
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38840938
Well in that case, you should be able to get intellisense for the textbox in your code-behind. That is what is baffling me. Because I am able to get intellisense which is normal.
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38840952
@informaniac: exactly the quandary, bewilderment, and puzzlement i find myself in. in that same .cs file i have no problem accessing other controls in that same way, so i'm quite baffled as well. i don't understand why I had to first find the container (ContentPlaceholder), the panel and then finally the textbox, i don't have to do that else where.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38842719
So you are saying, it is only this textbox that is causing the issue?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 38843986
@informaniac: yes. well that the txtboxes in that panel. there are two of therm there.
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38872206
You can often find the control by using findcontrol off the sender object.

sender.findconrol...
sender.parent.findcontrol...
sender.parent.parent.findcontrol...

Helps to interrogate the sender using intellisense, to locate the control relative to the sender hierarchy.

Alan
0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 38944246
thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question