FindControl返回空值 [英] FindControl Returning Null
问题描述
我试图根据相关文本框来控制按钮状态.名称与前缀相同.文本框和按钮位于页面上的表格中.
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
{
}
}
但是,尽管字符串与按钮的名称完全匹配,但控件仍返回null.该如何处理?
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?
推荐答案
感谢 Matthew Watson ,FindControl在使用母版页的项目中存在问题.为了在页面中查找控件,必须首先手动向下浏览母版页及其内容:
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:
此:
Button btn = FindControl(btnName) as Button;
必须采用以下格式:
Button btn = this.Master.FindControl("MainContent").FindControl(btnName) as Button;
这篇关于FindControl返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!