FindControl Returning Null
Asked Answered
L

4

9

I am trying to contol a buttons state depending on a relevant text box. The names are the same other than the prefixes. The text boxes and buttons are located in a table on the page.

<asp:Table ID="Table1" runat="server" CssClass="table">
            <asp:TableRow>
                <asp:TableCell Width="15%">
                    <asp:Label ID="lblRequestHeader" runat="server" Text="Requested" CssClass="bold text-center"
                        Width="90%"></asp:Label>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Label ID="lblApprovalHeader" runat="server" Text="Approval" CssClass="bold text-center"
                        Width="90%"></asp:Label>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Label ID="lblApprovalTimeHeader" runat="server" Text="Date/Time of Approval"
                        CssClass="bold text-center" Width="90%"></asp:Label>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Label ID="lblReadyHeader" runat="server" Text="Ready To Pick Up" CssClass="bold text-center"
                        Width="90%"></asp:Label>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Label ID="lblCollectedHeader" runat="server" Text="Collected By TestHouse" CssClass="bold text-center"
                        Width="90%"></asp:Label>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Label ID="lblDeliveredHeader" runat="server" Text="Delivered From TestHouse"
                        CssClass="bold text-center" Width="90%"></asp:Label>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell Width="15%">
                    <asp:TextBox ID="txtRequestTime" runat="server" Width="90%"> </asp:TextBox>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:TextBox ID="txtApproval" runat="server" Width="90%"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:TextBox ID="txtApprovalTime" runat="server" Width="90%"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:TextBox ID="txtReadyTime" runat="server" Width="90%"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:TextBox ID="txtCollectedTime" runat="server" Width="90%"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:TextBox ID="txtDeliveredTime" runat="server" Width="90%"></asp:TextBox>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell Width="15%">
                </asp:TableCell>
                <asp:TableCell Width="15%">
                </asp:TableCell>
                <asp:TableCell Width="15%">
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Button ID="btnReadyTime" runat="server" Text="Ready To Collect" Width="90%" />
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Button ID="btnCollectedTime" runat="server" Text="Collected" Width="90%" />
                </asp:TableCell>
                <asp:TableCell Width="15%">
                    <asp:Button ID="btnDeliveredTime" runat="server" Text="Delivered" Width="90%" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>

The textbox is populated by a dataretrieval, and the state of the button is then set by the called method as follows:

txtReadyTime.Text = slabdetails.ReadyTimestamp.ToString();
textboxenabled(txtReadyTime);

This method modifies the textbox name to a button name, then attempts to find the button to enable/disable it.

 public void textboxenabled(TextBox box)
    {
       string btnName = box.ID.Replace("txt", "btn");
        try
        {
            Button btn = FindControl(btnName) as Button;
            if (box.Text == "")
                btn.Enabled = true;
            else
                btn.Enabled = false;
        }
        catch
        {
        }
    }

However, despite the string matching the names of the buttons perfectly, the control returns as null. What can be done to deal with this issue?

Later answered 7/4, 2015 at 9:34 Comment(6)
Have you stepped through it using the debugger?Lucielucien
if you do object obj = FindControl(btnName);, is that returning null?Amalita
searching for object also returns nullLater
Ok, so the control you're searching for does not exist. Could it be in a container within the page you're searching? Perhaps you need to do a recursive search, as shown here.Amalita
thats the key! Thanks Matthew, care to place it as an answer so i can select it? The solution: Button btn = this.Master.FindControl("MainContent").FindControl(btnName) as Button;Later
I wouldn't have guessed that you have to drill down from the masterpage manually for something displayed on the page, surely 'this' would refer to that drilldown automatically?Later
L
25

With thanks to Matthew Watson, the FindControl has issues in projects using master pages. In order to find controls within a page, one must first drill down through the master page and its content manually:

This:

  Button btn = FindControl(btnName) as Button;

Must take the format:

  Button btn = this.Master.FindControl("MainContent").FindControl(btnName) as Button;
Later answered 7/4, 2015 at 11:14 Comment(3)
You can mark your own answer as the answer if you want (it will help other people searching for answers to similar questions).Amalita
a very proper solution searched many sites and finally found the solution for nested controlsSnowbound
It helps to be noted that "MainContent" refers to the ID of a ContentPlaceholder control inside the master page's .master file and not to the ID of a Content control inside the page's .aspx file.Tumescent
R
0

Try Button btn = (Button)Table1.FindControl("btnName");

EDIT:

As you are finding a control within a control you need to do above mentioned.

Riggall answered 7/4, 2015 at 9:39 Comment(7)
That won't help... It'll either still return null, or it will throw an exception.Amalita
You need the speech marks around the button name. Have you tried running it through a debugger with break points?Riggall
Also where is the button on the page?Riggall
tried to using speech marks around the name, it didnt work sorry: Button btn = FindControl('"' + btnName + '"') as Button;Later
Not like that like this FindControl("btnName") as Button;Riggall
oh i see, forcibly inject a known value to test. Reading that, I thought it was bound to work, unfortunately still returns nullLater
Then I'd suggest going with what Matthew said in his comment, perform a recursive search. Should workRiggall
C
0

Working for me...

protected void Page_Load(object sender, EventArgs e)
{

   // txtReadyTime.Text =""; //Button will be enabled
    txtReadyTime.Text =DateTime.Now.ToShortTimeString(); //Button will be enabled
    textboxenabled(txtReadyTime);


    //Button btn = this.FindControl("btnReadyTime") as Button;
    //Title = btn.Text;
}

public void textboxenabled(TextBox box)
{
    string btnName = box.ID.Replace("txt", "btn");
    try
    {
        Button btn = FindControl(btnName) as Button;
        if (box.Text == "")
            btn.Enabled = true;
        else
            btn.Enabled = false;
    }
    catch
    {
    }
}
Cleave answered 7/4, 2015 at 12:10 Comment(0)
J
0
please see you : https://mousavi6622.ir/en/


using System.Web;   
using System.Web.UI;

    public class FindControlService
    {
        Page page;
        public FindControlService(string id) {
            page = HttpContext.Current.Handler as Page;
        }
        public  static Control get(string id) {
            FindControlService fcr = new FindControlService(id);
            return fcr.FindControlRecursive(fcr.page, id);
        }
        private Control FindControlRecursive(Control root, string id)
        {
            if (root.ID == id)
            {
                return root;
            }

            foreach (Control c in root.Controls)
            {
                Control t = FindControlRecursive(c, id);
                if (t != null)
                {
                    return t;
                }
            }

            return null;
        }
    }
-----------------------

using : 

   

if (FindControlService.get("txtCountry")!= null)
{
   //
}
Janicejanicki answered 7/6, 2023 at 9:38 Comment(1)
You should add explanation.Lucy

© 2022 - 2024 — McMap. All rights reserved.