Changing ASP.NET compilation behavior based on page content?

I was struggling with some issues related to referencing child controls in a FormView. Another developer wrote an ASPX page that compiles and runs, and in this code, he refers to the child controls in the FormView directly as properties of the page object. The page is part of an ASP.NET Web SITE project (as opposed to a web application project). We decided to convert the project to a web application model and noticed that these property references no longer compile. The code behind the file does not create controls on the form view.

While researching this issue (I had a separate article here dealing with these issues), I came across something incomprehensible. From all the posts you read, you should always refer to the child controls in the FormView template using FindControl - that is, it might not be possible to do this with a simple generated property, no matter re in the website project model or the web application project model ...

I was puzzled about how my colleague's code was put together and run. As I pointed out, it refers to the controls contained in the FormView using simple properties on the page and does not have to resort to calls to FindControl. To understand this mystery, I have prepared the shortest example demonstrating this phenomenon.

What I found was very strange. The code I have has an ASP: FormView with multiple label controls in it ItemTemplate. One of these labels has the identifier MyComment . When I bind FormView data (to Northwind product table) I just set some text.

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.FormView1.ChangeMode(FormViewMode.ReadOnly);
    }
    protected void FormView1_DataBound(object sender, EventArgs e) {
        MyComment.Text = "Data bound at " + DateTime.Now.ToString();
    }
}

      

This code will not compile because MyComment is not a valid property. Here's the weird part. If I insert a TabContainer from the Ajax Control Toolkit into the FormView ItemTemplate, the above code compiles and runs correctly .

So why is my colleague's code compiling due to the built-in TabContainer in the FormView? Why this should change the behavior of the compiler and the mechanisms by which you can access the FormView controls is a mystery to me. By the way, even though it compiles correctly and works correctly, Intellisense does not see these properties and ReSharper reports them as compilation errors (red in the dashboard).

Here's the markup for the page. Can anyone shed some light on this behavior? BTW, I'm not complaining about ASP.NET creating these properties in this case. (Unfortunately this happy but strange behavior seems to only apply if the project is a website project, and as a web application project, property accessors do not work in the FormView even with the built-in TabControl).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:FormView ID="FormView1" runat="server" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"
            OnDataBound="FormView1_DataBound">
            <ItemTemplate>
                <ajaxToolkit:TabContainer runat="server" ID="TabsItem">
                    <ajaxToolkit:TabPanel runat="Server" ID="PanelBasicsItem" HeaderText="Basics">
                        <ContentTemplate>
                            ProductID:
                            <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
                            <br />
                            ProductName:
                            <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' />
                            <br />
                            My Comment:
                            <asp:Label ID="MyComment" runat="server"></asp:Label>
                            <br />
                        </ContentTemplate>
                    </ajaxToolkit:TabPanel>
                </ajaxToolkit:TabContainer>
            </ItemTemplate>
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName] FROM [Alphabetical list of products]">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

      

0


source to share


1 answer


When did you convert the website to web application , you have verified that the partial classes were generated correctly for your aspx files (for example, there should be a file called "Default.aspx.cs.designer" in your web application in the Default.aspx and Default.aspx.cs files).

In the website they are generated on the fly by the server as your site is run and compiled and generally do not exist in your project, but on the internet applications , they are created and managed by Visual Studio - if they do not exist, then the code will potentially fail to compile because the objects do not have been installed - what is the actual compiler error you are seeing?



By adding a new control to the page after converting the project to a web application, you are forcing VS to create the incomplete class that was missing.

0


source







All Articles