asp.net 对象引用未设置错误

发布于 2024-10-17 10:02:29 字数 290 浏览 3 评论 0原文

我试图从 aspx 页面找到标签控件。

Label labelmessageupdate;
          labelmessageupdate = (System.Web.UI.WebControls.Label )FindControl("updateMessage1");

如果我设置 labelmessageupdate.Text ="something"

它会返回对象引用异常。

并且标签控件位于更新面板内,这可能是问题所在。

I am trying to find the label control from aspx page.

Label labelmessageupdate;
          labelmessageupdate = (System.Web.UI.WebControls.Label )FindControl("updateMessage1");

if i set labelmessageupdate.Text ="something"

it returns object reference exception.

and the label control is within the update panel could that be the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷︶言冷语的世界 2024-10-24 10:02:29

只需检查是否为空。始终检查 null 条件,以免最终显示对象引用错误。

if (labelmessageupdate != null)
{
     labelmessageupdate.Text ="something"
}

Just check for null. Always check null condition so that it doesn't end up showing Object Reference error.

if (labelmessageupdate != null)
{
     labelmessageupdate.Text ="something"
}
要走干脆点 2024-10-24 10:02:29

我认为它无法找到您指定的标签控件

if(FindControl("updateMessage1") is Label)
{
    labelmessageupdate = FindControl("updateMessage1") as Label;
    labelmessageupdate.Text="This shoould work if available";
}

I think its not able to find the label control you specified

if(FindControl("updateMessage1") is Label)
{
    labelmessageupdate = FindControl("updateMessage1") as Label;
    labelmessageupdate.Text="This shoould work if available";
}
若沐 2024-10-24 10:02:29

试试这个,您尝试查找的控件可能位于另一个用户控件中。

使用

Label updateMessage = FindChildControl<Label>(base.Page, "updateMessage1");
if (updateMessage!=null) 
{
   updateMessage.Text = "new text";
}

/// <summary>     
/// Similar to Control.FindControl, but recurses through child controls.
/// Assumes that startingControl is NOT the control you are searching for.
/// </summary>
public static T FindChildControl<T>(Control startingControl, string id) where T : Control
{
    T found = null;

    foreach (Control activeControl in startingControl.Controls)
    {
        found = activeControl as T;

        if (found == null || (string.Compare(id, found.ID, true) != 0))
        {
            found = FindChildControl<T>(activeControl, id);
        }

        if (found != null)
        {
            break;
        }
    }

    return found;
} 

Try this, and the control your trying to find could be in a another user control.

To use

Label updateMessage = FindChildControl<Label>(base.Page, "updateMessage1");
if (updateMessage!=null) 
{
   updateMessage.Text = "new text";
}

/// <summary>     
/// Similar to Control.FindControl, but recurses through child controls.
/// Assumes that startingControl is NOT the control you are searching for.
/// </summary>
public static T FindChildControl<T>(Control startingControl, string id) where T : Control
{
    T found = null;

    foreach (Control activeControl in startingControl.Controls)
    {
        found = activeControl as T;

        if (found == null || (string.Compare(id, found.ID, true) != 0))
        {
            found = FindChildControl<T>(activeControl, id);
        }

        if (found != null)
        {
            break;
        }
    }

    return found;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文