How to run off ASP.NET validation for aspx in VS?

三世轮回 提交于 2019-12-25 04:28:05

问题


I have an ascx control named PicView in a classic ASP.NET 4.0 project. The beginning of it looks like this:

<script runat="server">
Public ImageUrl As String

, meaning that we can use the control's ImageUrl property in aspx pages. To use the control, I add the following directive to an aspx page:

<%@ Register TagPrefix="x" TagName="PicView" src="~/ascx/PicView.ascx" %>

, and then later in the body of the aspx use the control like this:

<x:PicView ImageUrl="img/bla-bla.jpg" runat="server" />

However, Visual Studio's automatic built-in ASP.NET validation always reports the following problem:

Validation (ASP.Net): Attribute 'ImageUrl' is not a valid attribute of element 'PicView'.

I get tons of these warning messages for my real-world project. I have not managed to do anything that could help VS to understand that my code is correct, so I wonder is there a way to turn this validation off?

Note that I need to turn off only the ASP.NET validation, but not the specific HTML version validation for this project. Sure, if you can tell me how to make this validation working in my situation, it would be very nice.


回答1:


ImageUrl needs to be a property, rather than a field, for example:

Public Property ImageUrl As String
    Get
        Return _imageUrl
    End Get
    Set(value As String)
        _imageUrl = value
    End Set
End Property
Private _imageUrl As String

If you want to be able to modify the property in code, and have it persisted across a PostBack, you should use ViewState to store its value, e.g.:

Public Property ImageUrl As String
    Get
        Return ViewState("imageUrl")
    End Get
    Set(value As String)
        ViewState("imageUrl") = value
    End Set
End Property


来源:https://stackoverflow.com/questions/21753237/how-to-run-off-asp-net-validation-for-aspx-in-vs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!