开发者

ASP.NET Intellisense doesn't work in attributes

开发者 https://www.devze.com 2023-01-01 03:50 出处:网络
It seems that Intellisense 开发者_Go百科just doesn\'t work within attributes in an ASP.NET page.I really like strong typing, because I like Intellisense, and so I generally make sure to bind to a stro

It seems that Intellisense 开发者_Go百科just doesn't work within attributes in an ASP.NET page. I really like strong typing, because I like Intellisense, and so I generally make sure to bind to a strongly typed object in ASP.NET:

<Repeater ID="rep" runat="server">
  <ItemTemplate>
    <div id="mydiv" class="<%# TypedObject.Class  %>" runat="server">
      <%# TypedObject.Name %>
    </div>
  </ItemTemplate>
</Repeater>

Intellisense just works within the body of the div, but no matter what I do it will not work to set that class attribute. This is very annoying, since attributes are pretty fundamental in HTML, and many of the built in controls use them heavily.

I can't find anything about this, but I can't believe this isn't a pretty fundamental need. Is there any way to get this to work?


It is really bad that Visual Studio 2008 / 2010 does not handle this. I sincerely hope it will come in the next release. However there are extensions out there that do help here.

Just tried Resharper for this particular reason and found it to be of great help. Both syntax checking and IntelliSense in my attribute strings now :-). I guess there are other Visual Studio extensions that do this aswell, havent tried just yet. (Resharper costs some, but they do have alternative free licenses for academic and open source dev.)


I have same problem very often, even while working in asp.net mvc 2. Usually I just type code outside class (where intellisense works) and then just move that piece of text into attribute. :S


To ensure proper Intellisense and strong typing, put your databinding code in the code file instead of the ASPX page. You will find your apps much easier to support should/when your typed object changes. The only "yellow" you should see in your ASPX page is at the top.

Your repeater should look like this...

<asp:Repeater ID="rep" runat="server">
    <ItemTemplate>
        <div id="mydiv" runat="server" />
    </ItemTemplate>
</asp:Repeater>

Your code file should look like this...

Option Explicit On
Option Infer On
Option Strict On

'Replace this class with your custom typed object'
Public Class TypedObject
    Public [Class] As String
    Public [Name] As String

    Sub New(ByVal NewClass As String, ByVal NewName As String)
        Me.Class = NewClass
        Me.Name = NewName
    End Sub    
End Class


Partial Class _Default
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Creating sample data and binding it to the repeater'
        Dim aData() As TypedObject = {New TypedObject("Class1", "Name1"), New TypedObject("Class2", "Name2")}

        rep.DataSource = aData
        rep.DataBind()
    End Sub

    Private Sub rep_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep.ItemDataBound
        'Do not process headers/footers/separators'
        Select Case e.Item.ItemType
            Case WebControls.ListItemType.Item, WebControls.ListItemType.AlternatingItem
            Case Else
                Exit Sub
        End Select

        'Aquire our datasource for this row'
        Dim dr = DirectCast(e.Item.DataItem, TypedObject)

        'Aquire the control to bind (Do this for each control you are binding)'
        Dim mydiv = DirectCast(e.Item.FindControl("mydiv"), HtmlGenericControl)

        'bind the control'
        mydiv.InnerHtml = dr.Name
        mydiv.Attributes("class") = dr.Class

    End Sub
End Class

You should also consider using an <asp:Label> instead of a <div>. Then you can use the .CssClass property instead of .Attributes("class").


I'm not sure this really counts as an answer, but: I've switched all new development to Razor, which fixes this problem and is much better in many other ways as well. In particular, the compile time checking in general is much better, making it easier to catch problems in the designer instead of at run time.

Again, not really an answer for WebForms development, but at least there is another view engine available now.

0

精彩评论

暂无评论...
验证码 换一张
取 消