A call to Bind was not well formatted - c#

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") %>'

Related

ASP.net Adding new Button to GridView give an error in jquery.dataTables.min.js

I currently have a GridView and I'm trying to add a button. I'm able to add it but when I click on it I get this weird error on this line.
if(c.mData===a){var d=u(b,"sort")||u(b,"order"),e=u(b,"filter")||u(b,"search");
Error:
Unhandled exception at line 91, column 426 in
http://localhost:23549/Scripts/jquery.dataTables.min.js
0x800a138f - Microsoft JScript runtime error: 'mData' is null or not
an object
I've done a bit of research and I can't find this issue anywhere else. Other users are getting the same error but none of them use ASP. I tried changing some properties on the button. I also tried adding it has a ButtonField instead but I still get the same error.
<div class="datatable">
<asp:GridView runat="server" ID="jobGridView" AutoGenerateColumns="False" BorderWidth="0px" GridLines="None" CssClass="table table-hover table-condensed font13" OnRowDataBound="jobGridView_RowDataBound" DataKeyNames="site_id,id" OnRowCommand="OnOff">
<Columns>
<asp:BoundField DataField="site_id" meta:resourcekey="siteField" />
<asp:BoundField DataField="id" meta:resourcekey="jobNameField" />
<asp:BoundField DataField="job_start_date" meta:resourcekey="jobStartField" DataFormatString="{0:yyyy-MM-dd HH:mm:ss}" />
<asp:BoundField DataField="job_end_date" meta:resourcekey="jobEndField" DataFormatString="{0:yyyy-MM-dd HH:mm:ss}" />
<asp:BoundField DataField="job_duration" meta:resourcekey="jobDurationField" DataFormatString="{0:HH:mm:ss.fff}" />
<asp:BoundField DataField="job_last_run_elapsed_time" meta:resourcekey="jobElapsedTimeField" />
<asp:BoundField DataField="return_code" meta:resourcekey="returnCodeField" />
<asp:BoundField DataField="return_description" meta:resourcekey="returnDescriptionField" />
<asp:BoundField DataField="status_code" meta:resourcekey="statusField" />
<asp:BoundField DataField="job_last_failure" meta:resourcekey="jobLastFailureField" DataFormatString="{0:yyyy-MM-dd HH:mm:ss}" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelect" runat="server" Text="View" OnClientClick='<%# GetOpenTabJavascript(DataBinder.Eval(Container.DataItem, "id"), DataBinder.Eval(Container.DataItem, "site_id"))%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="OnOff" Text="Turn On/Off" ShowHeader="True" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
protected void OnOff(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "OnOff")
{
//This part gets hit when debugging, error is after
}
}
If I remove the button everything works fine. (The LinkButton work)
I tried changing the type of event to RowEditing and RowUpdating. Same error.
Where should I start debugging to find out where the error comes from?
Feel free to ask any questions.
The error is being pretty straightforward in just letting you know jquery.dataTables is looking for an object named mData which is currently null and/or false.
Try giving your <div class="datatable"> an ID to match what dataTables is looking for.
That is, <div class="datatable" id="mData"> I'm not 100% sure that will do it, but your solution will be definitely in the neighborhood.
I've used dataTables a lot and tended to run into lots of annoying errors like this that just took debugging and banging my head on the desk some before I figured it out.
Seems like otherwise (can't really tell without debugging myself) your code would be fine--you just need to explicitly tell dataTables what mData is. If it's not on the <div>, maybe the button or GridView: honestly, too many variables for me to know for sure (unless you try some of these and have feedback! :))

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.

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

Visibility of an ASP.Net button which depends if a file exists in database

I have a gridview which has a few rows (each with a unique rowId), and each line has a FileUpload control, now everything works okay with FileUpload.
(my uploaded file database image can be seen below)
I have the download button, which also works okay, however I want to make this button invisible if no file exists for the corresponding row.
Nothing proper comes to my mind.
My button and FileUpload control:
<asp:TemplateField HeaderText="BatchList">
<EditItemTemplate>
<asp:ImageButton ID="ibt_Download" runat="server" src="Images/Download.png" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>' ></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UploadBatchList">
<HeaderTemplate>
<asp:Label ID="lbl_Header" ToolTip="Upload Batch List" runat="server" Text="UBL"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu_UploadBatchList" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click" />
</EditItemTemplate>
</asp:TemplateField>
This is how it looks on my gridView
When gridview is first created the green dots must not be visible if a file has been uploaded before.
My file database:
You can check some property of the data item (DocName in your case) if it contains a value (it might not work when copy-pasted, I'm a little bit improvising):
<asp:Button ID="btn_Upload" runat="server"
Text="Upload"
Visible='<% DataBinder.Eval(Container.DataItem, "DocName") == null %>
OnClick="btn_Upload_Click" />
Or you can create a function that will evaluate the visibility. See Mastering ASP.NET DataBinding for more.

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.

Categories