2012-08-22 73 views
0

您可以讓我知道爲什麼以下代碼不工作,當調試器通過變量「strStatus」時,我收到錯誤消息。錯誤消息是:「未將對象引用設置爲對象的實例。」你能幫忙嗎?謝謝 - 雅格亞在ASP.Net中出現錯誤頁面

protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (Y0130_chkNew.Checked == true) 
     { 
      bool isChecked = true; // This is required for later retrieval. 

      string strAction = "Reporting"; 
      string strFromRole = ddSelectRole.SelectedValue; 

      string TxtBoxID = myProject.getTextBox(strAction, strPath); 
      TextBox txtb = new TextBox(); 
      txtb = (TextBox)Page.FindControl(TxtBoxID); 
      string strStatus = txtb.Text; 

      string ddID = myProject.getDropDown(strAction, strPath); 
      DropDownList ddLst = new DropDownList; 
      ddLst = (DropDownList)Page.FindControl(ddID); 
      string strForwardRole = ddLst.SelectedValue; 

      // Call the function now 
      my.updateXML(strFromRole, strAction, strStatus, strForwardRole, strPath); 

     } 

    } 
+2

什麼'字符串TxtBoxID = myProject.getTextBox(strAction,strPath);'?你能證明這種方法嗎?除此之外,'Page.FindControl(TxtBoxID);'返回null你的異常的原因是什麼。 FindControl不會搜索控件,只會在給定的'NamingContainer'中進行遞歸搜索。 –

回答

0

Page.FindControl(TxtBoxID);返回null什麼是您的例外的原因。 FindControl不遞歸搜索控件,只在給定的NamingContainer中。

如果沒有嵌套在另一個NamingContainer控制的頁面(FE一GridViewRow)上,我們沒有理由在所有使用FindControl因爲你可以直接引用它:如果您使用MasterPages

string strStatus = TextBox1.Text; // assuming TextBox1 is declared on the aspx 

ContentPage控制NamingContainer不是一頁,但ContenPlaceHolder

Cannot find TextBox on Page

+0

嗨 - 函數getTextBox從給定參數的XML文件中檢索文本框的ID。我再次檢查該ID是否檢索成功並存在於網頁中。另請注意,控件的ID在整個項目中都是唯一的。不知道這裏有什麼問題。我僅使用母版頁來保持一致的外觀和用於網站導航的目的。你有一點在這裏。讓我檢查在母版頁中使用內容佔位符是否解決了這個問題。欣賞你的投入。 –

+0

最後它通過使用以下語句: txtb =(TextBox)Page.Master.FindControl(「ContentPlaceHolder1」)。FindControl(TxtBoxID); –

0

您使用的是功能找到一個控制和可能的返回文本框的id其犯規existin頁面上。請嘗試調試並查看您從myProject.getTextBox函數獲得的文本框ID,以及它是否存在於頁面上。

0

最終代碼: 如果(Y0130_chkNew.Checked == TRUE)
{
串TxtBoxID = 「Y0140_txtStatus」; TextBox txtb = new TextBox();
txtb =(TextBox)Page.Master.FindControl(「ContentPlaceHolder1」)。FindControl(TxtBoxID);
string strStatus = txtb.Text;

  string ddID = "Y0150_ddOwnerRoleAssignment"; 
      DropDownList ddLst = new DropDownList();    
      ddLst = (DropDownList)Page.Master.FindControl("ContentPlaceHolder1").FindControl(ddID);    
      string strForwardRole = ddLst.SelectedValue;      
     }