error in the gridview control - c#

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. :)

Related

A call to Bind was not well formatted

I've recently upgraded to VS2013. For some reason I'm getting the above error when trying to bind data in a detailsview. This is my code:
<asp:TemplateField HeaderText="Resource Link">
<EditItemTemplate>
<asp:TextBox ID="RESOURCE_LINK" runat="server" Text='<%#Bind("Resource Link") %>'
TextMode="MultiLine" class="myTextEditor" cols="50" name="tinymce" Height='300px'
Width='600px'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle VerticalAlign="Top" />
<ItemStyle VerticalAlign="Top" />
</asp:TemplateField>
(Also tried "[Resource Link]")
The same code works fine on past asp applications I've published. What I'm asking:
Why does this error appear on vs2013 and not vs2010?
How can I change that code to allow the bind?
I've read several articles including Eilon Liptons blog post on Databinding but I don't see a solution.
Any help appreciated.
May be you should use Eval instead of Bind
Text='<%# Eval("Resource Link") %>'

nvarchar field has wrong encoding when loaded from SQL server

I have table with column MasName of type nvarchar(100). I am using SQL Server 2012 and Entity Framework 6 to work with database. I have DetailsView where I always bind one relevant record:
<asp:DetailsView ID="masDetailView" runat="server" AutoGenerateRows="False"
SelectMethod="GetMas" UpdateMethod="UpdateMas" ItemType="NSMAS.DAL.Mas"
AutoGenerateEditButton="True">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="nameLbl" Text='<%#: Item.MasName %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="nameEdit" runat="server" Text='<%#: Item.MasName %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%--other columns--%>
Text binded to Label nameLbl show correctly: Hříběcí hory, but when I click auto generated edit button and text is binded to TextBox nameEdit (for editing entry), text is: Hříběcí hory.
Where could be problem? Can I fix this for all records or do I have to set encoding to every TextBox (that I don't know how since I don't bind text in codebehind)
EDIT:
So I have rewrited Text='<%#: Item.MasName %>' to Text='<%# Item.MasName %>' and now it works. I was told that it's better to use : after <%# when I access data like this, but I can't find any article about what is this exactly doing.

Passing in extra parameters in a URL

I have the below column on my table what gets binded on pageload, the parameters in there work fine but i need to add an additional parameter which is the fullname which is the next column along but im having trouble figuring our the syntax,
here is my ASP
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:HyperLink ID="hyperLeadID" runat="server" NavigateUrl='<%#Eval("ID","/documents/Q-Sheet.aspx?LeadID={0}&isHappyCallReferral=yes&isHappyName={1}") %>'
Text='<%#Eval("ID")%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Referral Name">
<ItemTemplate>
<asp:Label ID="lblRefName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As you can see at the end of ID column i have added isHappyName={1} which i assumed it would select the next column along as it starts at 0 but it keeps throwing an error which is
"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."
Can someone help me to pass the usersname through the URL
Thanks
Rewrite the hyperlink like this:
NavigateUrl='<%# string.Format("/documents/Q-Sheet.aspx?LeadID={0}&isHappyCallReferral=yes&isHappyName={1}", Eval("ID"), Eval("HappyName"))%>'
The problem is with a call to String.Format (presumably <asp:HyperLink> is doing this internally). It's only being provided with a single argument, whereas {1} refers to a second, non-provided, argument.
Try using <a href="#"> instead without using asp:HyperLink (and ASP.NET's built-in databinding) and setting the URI manually - I'm not a fan of System.Web.UI.WebControls at all for this reason (I strongly suggest you look into ASP.NET MVC when you get the chance).

GridView-generated code adds "width='100%'" to the table it creates

My page looks correct in Firefox and IE8. But in IE7, a nested gridview spills into the adjacent cell, much like the issue here.
Looking at it in the developer tools, there is an inline-style associated with the table that ASP.NET generated, and it has a width attribute of 100%. If I remove this, the nested table pops back where it belongs.
Problem is, nowhere is an inline-style set. In fact, if I try to set width='250px', it gets overridden with width='100%'. If I try to remove the width attribute in the code-behind, attrGridView.Attributes["Width"] is null, and calling .Remove() does nothing. But every asp.net-generated gridview-table has an inline style with width='100%' set on it (it's only causing me issues in one place).
Setting table-layout='fixed', as suggested in the article I linked to, did not help.
How do I get ASP.NET to stop setting this property?
Some code:
<asp:TemplateField HeaderText="Attributes" SortExpression="Attributes">
<HeaderStyle CssClass="GridHeaderCell" />
<ItemStyle CssClass="GridTableCell AttrGridCellPadding" />
<ItemTemplate>
<asp:GridView id="attributesGridView" runat="server"
AutoGenerateColumns="false" ShowHeader="false" GridLines="None"
AlternatingRowStyle-BackColor="White" CssClass="StupidGridView" >
<EmptyDataTemplate>
<p class="italic">There are no attributes for this request.</p>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<ItemStyle CssClass="AttrTableCell" />
<ItemTemplate>
<asp:Label id="attributeName" runat="server"
Text='<%# Eval("Name") + ": "+ Eval("Value") %>'
CssClass="AttrGridCell"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
.StupidGridView {
width: 250px;
}
Themes are being applied and overriding control-level settings. Check theme settings on page, web.config, or anywhere else a theme may be set.
Unfortunately, the ASP.NET html rendering is really bad.
Microsoft know that and provided Control adapters in 2006 which allow you to modify control rendering.
Instead of searching on how to override what ASP.NET render, I would advise to use CSSFriendly which provides control adapters for most ASP.NET bad-rendered controls.
If you don't need "pure-css rendering" and CSS afraid you, you can check how they do to create your own adapter.
ScottGu post on this subject from google.

How do i put 2 DataSet Table Results in one GridView Column?

I have two results from a data set. I want to add both results in one gridview column. I do not want to merge in the code behind. Is there another way to do that?
<asp:TemplateField HeaderText="Entered Date" SortExpression="Date">
<ItemTemplate>
<div style="text-align: center;">
<asp:Label ID="Label3" runat="server" Text='<%# formatDisplayDate(Eval("Date").ToString()) %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
Now i would return "04/04/2009". I would like to return "04/04/2009-PASSED"
Have you tried:
formatDisplayDate(Eval("Date").ToString()) & "-" & Eval("OtherField").ToString()
If that doesn't work you should be able to set up a TemplateField for the combined rows like this:
<asp:TemplateField HeaderText="CombinedFieldName" SortExpression="CombinedFieldName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# DataBinder.Eval(Container,"DataItem.FirstField") %>' >
</asp:Label>
-
<asp:Label ID="Label2" runat="server"
Text='<%# DataBinder.Eval(Container,"DataItem.SecondField") %>' >
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
That said - it is usually much more efficient to do the field concatenation in the database operation if possible. I haven't tested, but I would assume that concatenating in the SELECT would be fastest, followed by concatenating in the data set in the codebehind, and combining them on the ASP.Net page would be slowest.
Another option would be to create a calculated column in the DataTable, using the DataColumn.Expression property
My first question is why don't you want to merge in the code behind? The more content you bind to a control the more data that gets put into that controls view state.
#Gary.Ray has a really good solution, but something you might find better is to use linq to build a single data object from your existing data sets that way you only include what you need and the data is a lot easier to isolate, think single responsibility pattern here, if you can move the code that deals with getting your data to somewhere that only deals with getting data your application will be MUCH easy to maintain in the future.

Categories