在ASP.Net页面中获取错误

时间:2012-08-22 21:32:00

标签: asp.net

你能告诉我为什么下面的代码不起作用,当调试器超过变量“strStatus”时,我收到错误消息。错误消息是:“对象引用未设置为对象的实例。”你能帮忙吗?谢谢 - Yagya

    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);

        }

    }

3 个答案:

答案 0 :(得分:0)

您正在使用某个函数找到一个控件,并且可能会返回一个不存在于页面上的文本框ID。请尝试调试并查看从myProject.getTextBox函数获取的文本框ID以及页面上是否存在。

答案 1 :(得分:0)

Page.FindControl(TxtBoxID);返回null您的异常原因是什么。 FindControl不会以递归方式搜索控件,仅在给定的NamingContainer

如果控件未嵌套在页面上的另一个NamingContainer中(仅限GridViewRow),则完全没有理由使用FindControl,因为您可以直接引用它:

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

如果您使用MasterPages,则NamingContainer中的ContentPage个控件不是页面,而是ContenPlaceHolder

Cannot find TextBox on Page

答案 2 :(得分:0)

最终守则:             if(Y0130_chkNew.Checked == true)
            {
                string 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;                       
        }