2015-04-07 141 views
3

我試圖根據相關的文本框來控制按鈕狀態。名稱與前綴不同。文本框和按鈕位於頁面的表格中。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 
     { 
     } 
    } 

但是,儘管字符串完美匹配按鈕的名稱,控件返回爲空。 可以做些什麼來解決這個問題?

+1

你有沒有使用調試器通過它? – horHAY

+1

如果你做'對象obj = FindControl(btnName);',是否返回null? –

+0

搜索對象也返回null – nickson104

回答

9

感謝Matthew Watson,FindControl在使用母版頁的項目中存在問題。爲了在網頁中找到能夠控制,必須首先通過母版頁深入和手動內容:

此:

Button btn = FindControl(btnName) as Button; 

一定的格式爲:

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

如果您願意,您可以將自己的答案標記爲答案(它可以幫助其他人搜索類似問題的答案)。 –

0

嘗試 Button btn =(Button)Table1.FindControl(「btnName」);

編輯:

當你發現你需要做上述控制內的控制。

+0

這將無濟於事......它要麼仍然返回null,要麼會拋出異常。 –

+0

您需要按鈕名稱周圍的語音標記。你有沒有試過通過帶斷點的調試器來運行它? –

+0

此外,頁面上的按鈕在哪裏? –

0

的工作我...

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