【问题标题】:Change Literal text on usercontrol VB.Net更改用户控件 VB.Net 上的文字
【发布时间】:2016-09-15 23:41:12
【问题描述】:

美好的一天

我有一个 VB.Net 项目。 我的主页是 Display.aspx 我有各种使用此页面的用户控件(ascx)。 在其中一个控件上,我有一个文字 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

但在我的网址上,页面是

Display.aspx?pid=4

这会导致 nullreferenceexception 吗?由于页面不再完全是 Display.aspx,而是 Display.aspx?pid=4

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

感谢您的帮助

编辑

这是在控件的代码中声明的

 If Not Session("CatMessage") Is Nothing Then
            lblCatQuestion.Text = Session("CatMessage")
        End If

但文字没有改变。因此,我为什么要研究另一种方法

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我试过这个。仍然没有工作。我之前所做的是 If Not Session("CatMessage") Is Nothing Then lblCatQuestion.Text = Session("CatMessage") End If 但那没有刷新标签。这就是为什么我要尝试另一种方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多