ASP GridView DataBind with Entity Navigated property - asp.net

I have a GridView DataBind with entity ClassA's properties that is working fine.
I am able to directly bind below properties in ASPX file.
ClassA.Id
ClassA.Name
etc.
But ClassA also have a navigation property to related ClassB. I would like in a the same GridView to display related classB's properties.
I try to bind the following in the GridView but it does not work even if I am able to properly evalute the below value in debug mode (entity performs lazy loading when required).
ClassA.classB.Name
How should I proceed ?

You can achive your goal by a template column with an eval function as below;
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Eval("ClassA.ClassB.Name") %>'></asp:TextBox>
</EditItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("ClassA.ClassB.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The downside of this approach is disabling the two-way databinding feature by using the late-bound eval method.

Related

How to Bind a databound TextBox with complex expression

I have following textbox within a Gridview:
<asp:TextBox ID="due" runat="server" TextMode="Date" Text='<%# Bind((Convert.ToDateTime(Eval("due")).ToString("yyyy-MM-dd"))) %>'></asp:TextBox>
This does not work. I get a Parser-error: A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.
When I use that expression without "Bind()" the textbox shows datepicker and stored date as desired, but when updating is clicked, no date is updated (because it is only ...Eval...).
What is the correct Syntax for Binding that two-way?
Give this a shot:
<asp:TextBox ID="due" runat="server" TextMode="Date" Text='<%# Bind("due", "{0:yyyy-MM-dd}") %>'></asp:TextBox>

Add TextBox to DataGrid

I have a web page built in .NET where I am a very simple DataGrid with several fields. I would like to have one of those field have its data placed in a TextBox so the user can edit the default description. Is there an easy way to do this by default so the user doesn't have to click an edit button for the row?
Yes, disable AutoGenerateColumns and define your own fields:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Data") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Updating this data to the database might be more difficult though.

ASP.NET: How to re-flow GridView cells over multiple lines

I have an application which contains a table with two columns: messageid and messageDesc . Inside the messageDesc column there is a message description. When I try to bind the GridView to the messageDesc column, it renders the entire column on a single line, requiring the user to scroll horizontally to view the complete text. I want to prevent this by having the description flow over multiple lines. How do I do this?
I have tried using the ItemStyle attribute but it's not working. Please suggest a solution. In addition to this, I can't change the database message content.
Thanks in advance.
Warp label insdie div element and than try out this will surely work for you
<asp:TemplateField>
<ItemTemplate>
<div style="width:100px;">
<asp:Label ID="Label2" runat="server" Text="<%# Eval("description") %>"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
or
<ItemStyle Wrap="true" Width="100px" />
You can display data in textbox using template field instead of bound field. See below, textbox code. Your TemplateField would look like this:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" Text='<%# Eval("MessageDesc") %>'
TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
The ReadOnly setting keeps users from editing the data while having it still enabled for them. In Edit mode, of course you would set ReadOnly to false.

How can I access data in an ODS with an ASP Expression?

If I have the following (generic) ASP code:
<asp:ObjectDataSource runat="server" ID="myODS">...</asp:ObjectDataSource>
<asp:GridView runat="server" ID="myGV" DataSourceID="myODS">
<Columns>
<asp:TemplateField>
<ContentTemplate>
<asp:Label runat="server" ID="myLabel" Text='<%# [What goes here?] %>' />
</ContentTemplate>
</asp:TemplateField>
<asp:BoundField ... />
<%-- Other columns... --%>
</Columns>
</asp:GridView>
What can I call within an ASP expression, inside my Template Field, that will allow me to have access to my ODS results? Basically, inside the expression, I want to do something like row("ID") to get a specific value out of a column from my ODS.
There are a number of different syntaxes you can use to specify a particular property. The simplest is to use the Eval method:
<asp:Label runat="server" ID="myLabel" Text='<%# Eval("ID") %>' />
This is a short-hand syntax for the more verbose syntax using DataBinder.Eval static method, so you can use this more verbose syntax if you want:
<asp:Label runat="server" ID="myLabel" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' />
See here for the MSDN documentation on DataBinder.Eval
Edit:
One thing I forgot to mention what that ASP.Net 2.0 and higher support the "Bind" method:
<asp:Label runat="server" ID="myLabel" Text='<%# Bind("ID") %>' />
which nominally supports 2-way data binding. Thus if you use the Bind method, you don't have to manually screen-scrape your value from your inserted or deleted row, and the ASP.Net infrastructure will handle generating the new or modified object. However, in practice, there are a lot of caveats for using this. Your domain object must have a default constructor, must only be composed of simply properties, and I think there are a few more. In practice, this is so restrictive that I don't think it is a very useful thing to use. But I just wanted to point it out.

custom *arbitrary* TemplateField definitions

I'm building a GridView on the fly, and I'd like to pre-define the TemplateFields to be included ondemand. So, what I'd like to do is have a declarative file that defines how the different templates look for a specific column. Like:
<asp:TemplateField>
<HeaderTemplate>
this is a text column
</HeaderTemplate>
<ItemTemplate>
data goes here
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text="databindhere" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
this is a bool column
</HeaderTemplate>
<ItemTemplate>
if(true) "yes" else "no"
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox Checked="databindme" />
</EditItemTemplate>
</asp:TemplateField>
So, if my query had a text and two bool fields, I could push the appropriate TemplateFields in the the Columns property as needed. (I hope I'm making sense here)
So, how would I go about creating declarative files for the above definitions? And then, how would I reference those definitions programmatically?
Ok, what would work best is to subclass System.Web.UI.WebControls.TemplateField, but when I do that, I can't seem to use the object with the <%# Register %> directive. If I could, I'd create a handful of UserControls with the new derivative, and then LoadControl() and Add() them to the Columns of my grid as needed.
Any ideas?

Resources