在usercontrol VB.Net上更改文字文本

时间:2016-05-19 08:04:47

标签: asp.net vb.net user-controls findcontrol

美好的一天

我有一个VB.Net项目。 我的主页是Display.aspx 我有各种使用此页面的用户控件(ascx)。 在其中一个控件上,我有一个Literal,lblCatQuestion。 在控件背后的代码中

 Protected WithEvents lblCatQuestion As Literal

这个文字的基本功能是它加载了一些文本和单选按钮。单击单选按钮时,会弹出一个Jquery表单。完成表单并保存和关闭后,此文字现在应显示一条消息。 该消息来自显示页面的主要代码

出于此目的,在Display.aspx.vb

 Session("CatMessage") = "Completed"
    Dim label As System.Web.UI.WebControls.Literal = New System.Web.UI.WebControls.Literal
    label = DirectCast(Page.FindControl("lblCatQuestion"), System.Web.UI.WebControls.Literal).Text = Session("CatMessage")

问题是,当我调试时,我总是在尝试将文本分配给文字时得到nullreferenceexception。 当我看,这里的页面被描述为

  

display_aspx   enter image description here

但是在我的网址上,页面是

  

Display.aspx?PID = 4

这是否可以引发nullreferenceexception?由于页面不再是Display.aspx,而是Display.aspx?pid = 4 enter image description here

我现在如何更改文字的文字? 我如何获得确切的页面,以便我可以更改文本值?

感谢您的帮助

修改

这是在

后面的控件代码中声明的
 If Not Session("CatMessage") Is Nothing Then
            lblCatQuestion.Text = Session("CatMessage")
        End If

但文字没有改变。因此,为什么我要研究不同的方法

1 个答案:

答案 0 :(得分:0)

如果Literal在用户控件中,则必须在那里查看,而不是在页面内。

{{1}}