【问题标题】:Display inline cause error in code behind在后面的代码中显示内联原因错误
【发布时间】:2016-04-26 03:55:38
【问题描述】:

这是我的以下代码:

<tr id="trPrice" style = "display:none;">
     <td colspan=2></td>
     <td><b>Price:</b></td>
     <td><asp:textbox id="txtPrice" runat="server" style = "border-style:none"></asp:textbox></td> 
</tr>

在我后面的代码中,我想在特定的 page_load 处将 trPrice 的样式设置为“display:inline” ->

Protected WithEvents txtPrice As System.Web.UI.WebControls.TextBox

' In Page_Load function:
If type = 3 Or type = 4 Then
        trPrice.Attributes.Add("style", "display:inline")
End If

但是出现这个错误:

对象引用未设置为对象的实例。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

【问题讨论】:

  • 您需要runat="server" 才能将此tr 用于代码隐藏
  • 这是一个拼写错误,请尝试 txtPrice.Attributes :-)
  • @Andrei 谢谢!多么愚蠢的错误:/

标签: css asp.net .net vb.net


【解决方案1】:

您需要将其设为runat="server",然后Visual Studio 会将其添加到designer.vb 文件中:

Protected WithEvents trPrice As Global.System.Web.UI.HtmlControls.HtmlTableRow

它将在Page_Load中初始化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2022-01-04
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多