简体   繁体   中英

error in the gridview control

I have a Gridview and the columns defined like below.

When I run the program I get the error

Literal content is not allowed within a System.Web.UI.WebControls.DataControlFieldCollection

<Columns>
    <asp:CommandField ButtonType="Image"
            ControlStyle-Height="20"  
            ControlStyle-Width="30"
            SelectImageUrl="tar.png"
            SelectText="Select"
            ShowSelectButton="true"/>                       
        <asp:TemplateField HeaderText="Target Date">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" 
                    Text='<%# Bind("tar_date") %>'>
            </asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lbl1" runat="server" 
                    Text='<%# Bind("tar_date") %>'>
            </asp:Label>
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Right" />
    </asp:TemplateField>
</Columns>

Can anyone help me solving this?

Nothing seems to be wrong with your markup.

The only thing I would recommend is ending the Label control immediately and trying it again.

<asp:Label ID="lbl1" runat="server" Text='<%# Bind("tar_date") %>' />
// OR
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("tar_date") %>'></asp:Label>

In the past I have seen issues when Tab, or some unintentional characters come in between some of the templated controls. Check if you have any such characters by redoing every line from scratch.

This question is a little old, but for the others who encounter this problem: This problem can caused by not putting white space between properties. For example:

<asp:TextBox ID="TextBox1" runat="server"Text='<%# Bind("tar_date") %>'> </asp:TextBox> //wrong (no space before Text)

This was such a frustrating error to run into. I wasted about 4 hours on this and there are surprisingly few resources I could find on Google to help me troubleshoot it. I was updating a legacy application, so the intricacies of a GridView were a bit hazy since I haven't created one from scratch in a while.

At the end of it, the fix was a result of a suggestion by Raja to rewrite the control. Visual Studio wasn't highlighting a very important issue and the ambiguous nature of the error message had me looking at the wrong grid columns. Despite the error pointing to an issue with the TemplateField , the issue for me was actually in a BoundField .

During a conversion from Telerik RadGrid to GridView, the BoundField control had an orphaned <ItemStyle> tag nested inside of it, but the BoundField control doesn't allow this.

Visually, you wouldn't know or even suspect this, unless you have recent familiarity with GridView. You couldn't run into it by debugging. Visual Studio and the compiler were not reporting this either. So troubleshooting it was a beast.

The thing that worked was rewriting the grid, line-by-line. Thanks, Raja!

The autocomplete feature in Visual Studio wouldn't let me close the BoundField control to add any other tag/control of any kind. This is when I finally realized where the issue was.

I hope this helps another unlucky Googler. :)

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