custom *arbitrary* TemplateField definitions - asp.net

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?

Related

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.

asp.net data controls

I have a table with the following structure,
I need to restructure the table to show the CustomSpaceName in the following order,
Space3 Personal Case Quick case
Space1 Space2
For each entry I will create a link button and pass the CustomSpaceId in query string.
So which is the ASP.NET Data Control matches best with my requirement. I think using the loop and generate table structure is a BAD idea.
No Need of doing that with a old method when ASP.net gives you GridView and other Data Bounding controls
Basically gridview will do the same operation that you told in a efficient way.
You can use the in-built methods.
Grid View
Repeater
ListView
DataList
Here I will recommend DataList.
Use RepeatColumns="4" property.
<asp:DataList ID="DataList1" RepeatColumns="4" runat="server">
<HeaderTemplate>
<asp:Label runat="server" ID="lbl1" Text='Header'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lbl1" Text='<% Eval("CustomSpaceName ") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>

ASP GridView DataBind with Entity Navigated property

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.

gridview databinder.eval question

We've got a number of gridviews that we're populating with data from an ADABAS datasource via some middleware that, unfortunately, gives us back lots of arrays of stringBuilder that we turn into a datatable, set as the datasource for a gridview, then bind.
It's convoluted, but I'm in a situation where I need to be able to update the gridview (say, drop a row), without going back to the mainframe, deleting the row, and regenerating the datatable, as I would if we had an Oracle datasource.
So, I'm trying to hack something together where on serverside I extract the data from the gridview, build a datatable, delete the given row, then use that datatable as the datasource for the gridview, and rebind.
I can do it on a case by case basis, so long as I know the layout of the gridview. But we're going to be doing this over and over on this current project, I'm trying to figure out a way to abstract it so that I can use it for any gridview I throw at it.
The problem I'm getting is that most of the cols in the gridview are templatefields with asp:labels, where the data is put in with the databinder.eval thing:
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
So, when building my 'fake' datasource, I need to be able to extract from somewhere in the gridview those column names.
Is that information available in the gridview?
Yikes. You are going to need to store the fake datasource column names within the GridView along with the column values. One suggestion would be to add a HiddenField along with each fake datasource field value. In the example below, display the code (value) in the Label and the column (name) in which code maps in the HiddenField:
<asp:TemplateField HeaderText="Code"
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label>
<asp:HiddenField ID="hfCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "codefield")%>'></asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
It's ugly, but the column names will always be available to you -- no matter what they might be. Best of luck.

Resources