FindControl 返回 Null
FindControl Returning Null
我正在尝试根据相关文本框来控制按钮状态。除前缀外,名称相同。文本框和按钮位于页面上的 table 中。
<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);
此方法将文本框名称修改为按钮名称,然后尝试找到按钮 enable/disable 它。
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
{
}
}
然而,尽管字符串与按钮的名称完全匹配,但控件 returns 为空。
可以做些什么来处理这个问题?
试试看
按钮 btn = (按钮)Table1.FindControl("btnName");
编辑:
当您在一个控件中找到一个控件时,您需要执行上述操作。
感谢 Matthew Watson,FindControl 在使用母版页的项目中存在问题。为了在页面中找到控件,必须首先手动向下钻取母版页及其内容:
这个:
Button btn = FindControl(btnName) as Button;
必须采用以下格式:
Button btn = this.Master.FindControl("MainContent").FindControl(btnName) as Button;
为我工作...
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
{
}
}
我正在尝试根据相关文本框来控制按钮状态。除前缀外,名称相同。文本框和按钮位于页面上的 table 中。
<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);
此方法将文本框名称修改为按钮名称,然后尝试找到按钮 enable/disable 它。
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
{
}
}
然而,尽管字符串与按钮的名称完全匹配,但控件 returns 为空。 可以做些什么来处理这个问题?
试试看 按钮 btn = (按钮)Table1.FindControl("btnName");
编辑:
当您在一个控件中找到一个控件时,您需要执行上述操作。
感谢 Matthew Watson,FindControl 在使用母版页的项目中存在问题。为了在页面中找到控件,必须首先手动向下钻取母版页及其内容:
这个:
Button btn = FindControl(btnName) as Button;
必须采用以下格式:
Button btn = this.Master.FindControl("MainContent").FindControl(btnName) as Button;
为我工作...
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
{
}
}