How can I access data in an ODS with an ASP Expression? - asp.net

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.

Related

Updating Database with value from datepicker control ASP.NET VB

Apologies in advance, for what may be a newbie question. (new to asp.net, coding in VB, zero knowledge of best controls). Please respect that I am not interested in AJAX controls, or using MVC and trying to minimise javascript. What I need to do is very simple in terms of technology.
I am developing a form that allows users to Edit database data. I have chosen to use FormView so that I can format it like a legacy vba app. I am able to set the data source to my database table and the values from the database successfully show up if I allow VWD to automatically format the control. I can edit everything in the database using this form as well.
However, the boss hates having to type dates.
I haven't been able to find a datepicker control with a datasource feature that works like the text boxes that are automatically built in formview (with a bind to a datasource feature). So, I am assuming none exist. I need some assurances that what I think I need to do is the right way.
Instead of using the FormView control's datasource via the front-end automagical stuff, I should instead
place one of these datapicker controls that simply combine calendar with a text box for all date fields in the formview (no disrespect to those who built them, just can't believe they are not more feature-rich, this seems so needed giving the number of datepickers available)
declare my data source in Page_Load and load all the controls if they have existing data utilising FindControl
use Data_Bound block to retrieve the selected values from each control utilising FindControl and build a dynamic SQL string for Update
declare and update my database using one of the code behind blocks, perhaps in the DataBound
Am I on the right track? I have no experience to be confident in my assumption.
and please, if there is an easier way, I'll take it, but I have to make it "pretty".
And suggestions for controls of any sort are welcomed.
---Further into my issue
Here is some code to prove I've actually tried to resolve this...
In my FormView code block I have:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="MASTERID_Action"
DataSourceID="srcAction">
<EditItemTemplate>
MASTERID_Action:
<asp:Label ID="MASTERID_ActionLabel1" runat="server"
Text='<%# Eval("MASTERID_Action") %>' />
<br />
Action_Description:
<asp:TextBox ID="Action_DescriptionTextBox" runat="server"
Text='<%# Bind("Action_Description") %>' />
<br />
Action_Target:
<asp:TextBox ID="Action_TargetTextBox" runat="server"
Text='<%# Bind("Action_Target") %>' />
<br />
Action_Captured:
<asp:TextBox ID="Action_CapturedTextBox" runat="server"
Text='<%# Bind("Action_Captured") %>' />
<br />
Action_Declined:
<asp:TextBox ID="Action_DeclinedTextBox" runat="server"
Text='<%# Bind("Action_Declined") %>' />
<br />
Action_AgreedDate:
<ewc:CalendarPopup ID="CalendarPopup1" runat="server" Culture="en-AU"
PostedDate="" SelectedDate='<%# Bind("Action_AgreedDate") %>'
SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
VisibleDate="01/14/2013 08:47:54" />
<br />
...
</EditItemTemplate>
My database holds this Action_AgreedDate as nullable.
When I view the ItemTemplate (in the browser) the date shows up as 0.000 (because its a text field and bound to Action_AgreedDate, no error occurs) and when I click Edit to go to the EditItemTemplate I get this error:
Conversion from type 'DBNull' to type 'Date' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.
Source Error:
Line 95:
Line 96: Action_AgreedDate:
Line 97: '
Line 99: SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
I can easily translate this into "the control found a null field and doesn't know what to do with it"; problem is, I don't know what to do. I have checked the properties of the field (the CalendarPOP field to see if there is a setting for handling nulls and nothing is obvious to me. I'm currently trying to find further documentation on the control online. (I've contacted eWorld and hope they will be able to respond.)
I should also add that if I request a record that already has an Action_AgreedDate I get no errors because there is a value present in the database for the control to display.
I think the best way for you to do this would be to use jQuery and jQuery UI, which has a date picker, which can be attached to a regular ASP.NET TextBox in your FormView. So if you had FormView code that looks something like this for example:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1">
<EditItemTemplate>
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:TextBox ID="date_modifiedTextBox" runat="server" Text='<%# Bind("date_modified") %>' />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:Label ID="date_modifiedLabel" runat="server" Text='<%# Bind("date_modified") %>' />
</ItemTemplate>
</asp:FormView>
You could run something like this using jQuery UI to get a datepicker on the date_modified text box:
$('[id$=date_modifiedTextBox]').datepicker()
The $('[id$=date_modifiedTextBox]') part is necessary to select the ASP.NET control using jQuery, if it were just a normal element you could just use $('.className') or $('#id') or something like that. Hope this helps.
Problem solved.
I ran across an article on 4GuysFromRolla.com about the RJS POP Calendar (http://preview.tinyurl.com/cerm9xv) and it ticked all the boxes. Successfully implemented it and, because the calendar is separate from the specified text box, it doesn't have to be bound ! It works very nicely. The 4GuysFromRolla save my bacon yet again...

The server tag is not well formed, ASP Repeater Datasource

I've been getting parser error with message The server tag is not well formed for the following line.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>">
<ItemTemplate>
<sc:FieldRenderer ID="FieldRenderer1" runat="server" FieldName="Tag name" Item="<%# Container.DataItem %>"/>
</ItemTemplate>
<SeparatorTemplate>
/
</SeparatorTemplate>
</asp:Repeater>
The syntax looks fine, but one thing I'm not sure about is whether you can use the ".Field["tags"] element in there.
I've tried looking it up, but couldn't find a similar problem. I'm hoping someone provide me with some insight to why the parser is complaining about this line.
Thanks
What comes into my mind right now is to use a single-quoted string instead:
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
You have double quotes within the attribute. This confuses the parser - it can't tell where the attribute ends.
Wrap the attribute in single quotes to fix it:
DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>'
try ' instead of " it might work
else try binding from code behind
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
Do you have a closing tag? i.e.
</asp:Repeater>
Otherwise you are missing the / at the end of your tag declaration.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>" />

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.

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