我正在尝试根据相关文本框控制按钮的状态。除了前缀之外,它们的名称相同。文本框和按钮位于页面上的表格中。
<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。那么如何处理这个问题?
object obj = FindControl(btnName);
,那么它会返回 null 吗? - Matthew Watson