How to Bind a databound TextBox with complex expression - asp.net

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>

Related

The server tag is not well formed when puting data in label

In my code I have a repeater and in it's datasorce i have a property FirstName. When I try to get the data from it I get this error:
The server tag is not well formed.
The problematic code is:
<asp:Label ID="lblOName" runat="server" Text="<%# Eval("FirstName") %>"></asp:Label>
What am I missing?
Never mind I fixed it. I just had to replace the quotes to:
<asp:Label ID="lblOName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
You should also use a literal control rather than a label in most cases.
A label should really only be used as a 'Label' for another control. So for example a label would be used next to a textbox input to inform people of what info to put in the textbox.
A literal should be used for all other eg non label text.

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.

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.

ASP.NET Setting a DetailsView BoundField value from javascript

I'd like to know if it is possible to set a field of a DetailsView BoundField (in edit mode) from javascript? I can't set an id for the boundfield, so obviously can't use document.getElementById() to get to it, but I was wondering if there's another way to do it?
You could try to create an EditItemTemplate, place a TextBox inside and give it an ID of your choice.
Like this:
<Fields>
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
If there is stuff to be bound on the TextBox's Text property you could use Bind() method.
Good Luck.

Resources