c# - FindControl 返回 Null

标签 c# asp.net null findcontrol

我正在尝试根据相关文本框来控制按钮状态。除前缀外,名称相同。文本框和按钮位于页面上的表格中。

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

文本框由数据检索填充,然后按钮的状态由调用的方法设置如下:

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

此方法将文本框名称修改为按钮名称,然后尝试找到按钮以启用/禁用它。

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

但是,尽管字符串与按钮的名称完全匹配,控件仍返回 null。 可以做些什么来处理这个问题?

最佳答案

感谢Matthew Watson ,FindControl 在使用母版页的项目中存在问题。为了在页面中找到控件,必须首先手动向下钻取母版页及其内容:

这个:

  Button btn = FindControl(btnName) as Button;

必须采用以下格式:

  Button btn = this.Master.FindControl("MainContent").FindControl(btnName) as Button;

关于c# - FindControl 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29488363/

相关文章:

c# - 使用 Global.asax 添加并读取列表<t>

c# - 从代码后面引用 jquery 库

c# - 如何在谷歌电子表格中间插入一行

c# - 禁用 Azure Insights 采样不起作用

javascript - html2canvas打印截图

java - 如何对空参数进行方法重载?

c++ - sprintf 为空字符字符串时的预期行为

c# - 空运算符 c#

c# - 如何使用多个条件过滤 Directory.EnumerateFiles?

c# - 在 EF 的 DBSet 中设置返回对象的条件