我試圖根據相關的文本框來控制按鈕狀態。名稱與前綴不同。文本框和按鈕位於頁面的表格中。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>
文本框是由dataretrieval填充,並且按鈕的狀態,然後通過所調用的方法設置如下:
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
{
}
}
但是,儘管字符串完美匹配按鈕的名稱,控件返回爲空。 可以做些什麼來解決這個問題?
你有沒有使用調試器通過它? – horHAY
如果你做'對象obj = FindControl(btnName);',是否返回null? –
搜索對象也返回null – nickson104