简体   繁体   中英

Unbound DetailsView Not Displaying in ASP.NET/VB.NET

In this project that I'm creating, I'm creating a mock-up of a page that uses a DetailView (something I saw in a blog) that seems to fit my needs. However, I don't want the data to be bound to anything.

So, I added the control and then I started adding the appropriate mock-up columns to it and tried to render the control on the test page. Where the HTML that ASP.NET should generate is nothing.

Here's my code (there's a 'real' DetailsView control, then a button and then a blank testing Details view control):

<asp:DetailsView ID="DetailsView1" runat="server" 
    Height="184px" Width="271px" EmptyDataText="no data" 
    style="margin-right: 1px">
    <EmptyDataTemplate>
        There is no data.
    </EmptyDataTemplate>
    <FooterTemplate>
        Passenger Details
    </FooterTemplate>
    <Fields>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PassengerName") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PassengerName") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ErrorMessage="this field must be filled in " ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
                <br />
                <br />
                <br />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("PassengerName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ErrorMessage="this field must be filled in " ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Phone">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                    ControlToValidate="TextBox3" ErrorMessage="this field must be filled in "></asp:RequiredFieldValidator>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Frequent Flyer Number">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                    ControlToValidate="TextBox4" ErrorMessage="this field must be filled in "></asp:RequiredFieldValidator>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Hello, World." />
    </Fields>
    <HeaderTemplate>
        Passenger Details
    </HeaderTemplate>
</asp:DetailsView>
<asp:Button ID="btnPay" runat="server" Text="Payment page" />
<br />
<asp:DetailsView ID="DetailsView2" runat="server" 
    Height="184px" Width="271px" EmptyDataText="no data" 
    style="margin-right: 1px">
    <EmptyDataTemplate>
        There is no data.
    </EmptyDataTemplate>
    <FooterTemplate>
        Passenger Details
    </FooterTemplate>
    <HeaderTemplate>
        Passenger Details
    </HeaderTemplate>
</asp:DetailsView>

Here is what was generated:

<div>



</div>

<div>



</div>

    <input type="submit" name="btnPay" value="Payment page" id="btnPay" />

    <br />

<div>



</div>

    <br />

Anybody have any ideas? In this environment we're using Visual Studio 2008, as well.

Your goal appears to be to trick an asp:DetailsView into displaying itself and its fields, even though you have no intention of binding it to data. This can be done!

DataTable = New DataTable
DataTable.Rows.Add()
Me.dtlMyDetailView.DataSource = DataTable
Me.dtlMyDetailView.DataBind()

Moreover, ButtonFields will generate the ItemCommand event if CommandName is set. You can use IsPostBack or other logic to ensure that you don't DataBind when you don't want to and lose your viewstate. You'll need your viewstate if you intend to respond to ItemCommand events and use the value of any control in the details view. DataBind blows that away.

The "DataBind" method of the control has to be called in order to show anything. The following aspx shows nothing alone

<asp:DetailsView ID="dtsView" runat="server">
            <EmptyDataTemplate>
                THIS IS EMPTY
            </EmptyDataTemplate>
        </asp:DetailsView>

but if he add the following code through the c# end

 protected void Page_Load(object sender, EventArgs e)
    {
        dtsView.DataSource = new DataTable();
        dtsView.DataBind();
    }

the following gets generated

<table cellspacing="0" rules="all" border="1" id="dtsView" style="border-collapse:collapse;">
    <tbody><tr>
        <td>
                THIS IS EMPTY
            </td>
    </tr>
</tbody></table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM