DataBinder.Eval error in asp.net - asp.net

I have created a class doing some jobs like GridView inherit from System.Web.UI.WebControls.WebControl.
public class IHGridView : System.Web.UI.WebControls.WebControl
{
// inside here, actually return Repeater class.
protected override void OnInit(EventArgs e)
{
_repeater.ItemTemplate = new IHGridItemTemplate(ListItemType.Item, this.Columns);
this.Controls.Add(_repeater);
}
}
I also created ItemTemplate for my repeater in IHGridView.
public class IHGridItemTemplate : ITemplate
{
}
IHGridView class returns Repeater and some html codes, but in convenience to deveop I have created some stuff.
public class Columns : StateManagedCollection
{
}
public class IHBoundFieldBase : IStateManager
{
}
public class IHLabelField : IHBoundFieldBase
{
}
Now in my aspx, I can use this like below:
<cc1:IHGridView ID="IHGridView1" runat="server" EditMode="View">
<Columns>
<cc1:IHLabelField ID="IHLabelField7" DataField="PERSON_NAME" HeaderText="PersonName" />
</Columns>
</cc1:IHGridView>
Now I come up with a problem.
I cannot use DataBinder.Eval in aspx.
<cc1:IHLabelField ID="IHLabelField7" HeaderText="PersonName" Text='<%# DataBinder.Eval(Container.DataItem, "PERSON_NAME") %>' />
This gives me an error.
The error message is below: CS1061: There is no definition of 'DataItem' in 'System.Web.UI.Control'. There is no extendable method 'DataItem' in 'System.Web.UI.Control''s first argument. Please check if there is using rubric or assembly reference. This was written in Korean, but I translated into English.
Could anyone give me a clue to solve this problem?

In templated controls, the template is instantiated in the container. For data-binding to work in the templated fields, its recommended that container should implement IDataItemContainer interface - the interface implementation should be supplying the data-item.
AFAIK, to support data binding expressions, ASP.NET parser injects handler for DataBinding event for the control (whose properties uses these expressions) and then in the handler, it generates code that looks for data-item in the container.
So in your example, if you wish to use data-binding expression in the IHLabelField.Text property then the control's naming container should either implement IDataItemContainer or should have DataItem property. So in this case, you will probably need DataItem on IHGridView control - and it wouldn't work the way you want.

here is an example we used. i hope it helps
<asp:HyperLink ID="phoneManagementHyperLink" runat="server" Text='<%# (Container.DataItem as WcfUser).firstName + " " + (Container.DataItem as WcfUser).lastName%>'

Related

How do I declaratively bind 'SelectedValue' to datasource field?

I want to drive a RadioButtonLists SelectedValue property from a property in my datasource, but I'm not managing to make it work.
I have a <asp:RadioButtonList on my page bound to an <asp:ObjectDataSource. This datasource in turn provides the following model class:
public class CollectionWithDefault : Collection<string>
{
CollectionWithDefault(IEnumerable<string> items, string defaultItem)
{
foreach (var item in items)
Add(item);
DefaultItem = defaultItem;
}
public string DefaultItem { get; }
}
Notice that this class is a standard collection of strings that also exposes which one of them is the default option.
Consider that I have the following implementation for a value provider. This is a simple in-memory implementation, but keep in mind that this could be coming from a database or any other source:
public static class ItemProvider
{
public static CollectionWithDefault GetAvailableItems()
{
var items = new [] { "option1", "option2", "option3" };
return new CollectionWithDefault(items, items[1]);
}
}
I tried the following:
<asp:ObjectDataSource runat="server"
ID="ItemSource"
TypeName="MyNamespace.ItemProvider"
SelectMethod="GetAvailableItems" />
<asp:RadioButtonList runat="server"
DataSourceID="ItemSource"
SelectedValue='<%# Eval("DefaultItem") #>' />
I'm getting the following exception in the Eval call:
System.InvalidOperationException: 'Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.'
How can I ensure that the correct radio is preselected based on the field coming from my datasource?
Changing the collection model itself to make it work is acceptable, but I can't set the SelectedValue from codebehind. I wanted to rely on the datasource control to do the heavy lifting.
I managed to make this work seamlessly without requiring manual assignments in codebehind by extending the original RadioButtonList control and modifying the core databinding method to honor ListItem objects.
It goes something like this:
public class MyRadioButtonList : RadioButtonList
{
protected override void PerformDataBinding(IEnumerable dataSource)
{
if (dataSource is IEnumerable<ListItem> listItems)
{
...
foreach (var listItem in listItems)
Items.Add(listItem);
...
}
else
{
base.PerformDataBinding(dataSource);
}
}
}
With this in place, it was just a matter of converting my source model into a IEnumerable<ListItem> on the presentation layer (easy to accomplish with an adapter/proxy implementation) and then feed these ListItems to the control.
Once I got this in place, I could see my selected items reflected correctly in the UI based on the datasource field. Considering how trivial the extension is, I feel it was quite worth it :)
The same inheritance approach can probably be used for similar controls like CheckBoxList, which suffers from the very same limitation.
For the more adventurous folks, one could also make this work by introducing extra DataSelectedField and DataEnabledField properties in the control and using Eval on top of them as part of the original databinding algorithm (which already does this with DataTextField and DataValueField). I felt this would be a little bit more involved for my use case and decided to go with a simpler override, but it is definitely a valid approach that could even live along my proposed solution for an even more robust RadioButtonList.

Passing a web control reference to a User Control base class

I created several user controls - most containing a single web control (text box, drop down, radio button etc) - along with one or more validation controls. The point being to combine control and validation in a single user control.
I created a base class for these user control with some common functionality - setters for several properties of a single web control, specifically CssClass and Style to be set in the control in the ascx.
Eg a single text box with a single required field validator.
Sample code for the base class:
public WebControl ctrl {get; set;} //allow derived class access to this
public string CssClass
{
set { ctrl.CssClass = value; } //allow CssClass to be set in the aspx page
}
Sample code for derived class:
(in constructor or control OnInit Event - or ?)
base.ctrl = txt; //tell the base class which web control to apply common properties to.
public string ErrorMessage
{
set { val.ErrorMessage = value;} //this works !
}
Sample code for ascx:
<asp:TextBox ID="txt" Cssclass="input-text-m" maxlength="50" runat="server" />
<asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txt"
ErrorMessage="">*</asp:RequiredFieldValidator>
Sample code for aspx:
<uc:TextBox ID="ForeName" Cssclass="input-text-m" maxlength="50"
ErrorMessage="Forename" runat="server"/>
The problem I found was that I couldn't find a way for the derived class to set the base class web control reference before the base classes property setters are called.
If I set base.ctrl in the derived class constructor - then the derived class control reference (txt) is still null at this point.
If I set base.ctrl in any of the control events - eg OnInit - then this is too late.
So far I have got around the problem by simply not using a base class, and writing the property setter code in the user control class instead, however this means duplication of code, which I was trying to avoid.
Is there a way to inform the base class of the control I want it to set the properties for in advance of them being set - or am I going about things the wrong way...
What about calling EnsureChildControls before any get/set operations and including the set operation for ctrl = txt in EnsureChildControls? This is pretty standard practice for a normal servercontrol, I would think it would work for UserControls too.
public string CssClass { set { EnsureChildControls(); ctrl.CssClass = value; } }
Override EnsureChildControls, leaving in the call to base, and set ctrl = txt; here after the call to base.
More information: http://msdn.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx

How do I get many property values from View to Presenter in WebFormsMvp?

What is the best way to get a number of property values of a business object from the View to the Presenter in a WebFormsMvp page?
Bearing in mind this issue with DataSources.
Here is what i propose:
The scenario is, I have a business object called Quote which i would like to load form the database, edit and then save. The Quote class has heaps of properties on it. The form is concerned with about 20 of these properties. I have existing methods to load/save a Quote object to/from the database. I now need to wire this all together.
So, in the View_Load handler on my presenter i intend to do something like this:
public void View_Load(object sender, EventArgs e)
{
View.Model.Quote = quoteService.Read(quoteId);
}
And then bind all my controls as follows:
<asp:TextBox ID="TotalPriceTextBox" runat="server"
Text="<%# Model.Quote.TotalPrice %>" />
All good, the data is on the screen.
The user then makes a bunch of changes and hits a "Submit" button. Here is where I'm unsure.
I create a class called QuoteEventArgs exposing the 20 properties the form is able to edit. When the View raises the Submit button's event, I set these properties to the values of the controls in the code behind. Then raise the event for the presenter to respond to. The presenter re-loads the Quote object from the database, sets all the properties and saves it to the database.
Is this the right way to do this? If not, what is?
"A nicer way" (/alternative) is to make use of the 2-way binding, therefore what will be passed back to the Presenter for processing will be your Quote object.
This can be achieved through the use of an asp:FormView in conjunction with the mvp:PageDataSource that specifies an UpdateMethod and the Bind() method.
The WebFormsMVP sample project demonstrates this via the 'EditWidgetControl', including the methods required on the View code-behind file.
As an option your view can simply implement only the EditItemTemplate for asp:FormView making use of DefaultMode="Edit" on the FormView.
Sample Structure:
<asp:FormView DataSourceID="theSource" DefaultMode="Edit">
<EditItemTemplate>
<fieldset>
<asp:TextBox id="totp" value='<%# Bind("TotalPrice") %>' runat="server" />
</fieldset>
</EditItemTemplate>
</asp:FormView>
<mvp:PageDataSource ID="theSource" runat="server"
DataObjectTypeName="Your.NameSpace.Quote"
UpdateMethod="UpdateQuote">
</mvp:PageDataSource>
Code-behind:
public void UpdateQuote(Quote q, Quote ori)
{
OnUpdatingQuote(q, ori);
}
public event EventHandler<UpdateQuoteEventArgs> UpdatingQuote;
private void OnUpdatingQuote(Quote q, Quote ori)
{
if (UpdatingUserGroup != null)
{
UpdatingUserGroup(this, new UpdateQuoteEventArgs(q, ori));
}
}
How to use the GridView inside a FormView.
Because I have list to populate the grid in a entity.

How to set the RowStyle of a GridView row depending on a property of the Object that the row is being bound to

I'm currently using a GridView and I want to set the CssClass for the Row depending on a property of the object that the row is being bound to.
I tried the following but it does not work (see comments):
<asp:GridView id="searchResultsGrid" runat="server" AllowPaging="true" PageSize="20" AutoGenerateColumns="false">
<!-- The following line doesn't work because apparently "Code blocks
aren't allowed in this context: -->
<RowStyle CssClass="<%#IIF(DataBinder.Eval(Container.DataItem,"NeedsAttention","red","") %>
<Columns>
<!--............-->
</Columns>
</asp:GridView>
Now I could simply handle the GridView's RowDataBound event and change the css class of the row there...but I'm trying to keep a clear separation between the UI and the page/business logic layers.
I have no idea how to accomplish this and I'm looking forward to hearing any suggestions.
Thanks,
-Frinny
You cannot do this in declarative markup.
Nearly all of GridView's declarative properties (including GridView.RowStyle) are grid-level settings rather than row-level. Apart from TemplateFields , they are not bound data containers, so they don't have access to the data in their rows.
If you want to keep this logic in the .aspx template, your only real option is to use template fields and manipulate their contents:
<asp:TemplateField>
<ItemTemplate>
<span class="<%# ((string)Eval("property3")) == "NeedsAttention" ? "red" : string.Empty %>">
<%# Eval("property1") %>
</span>
</ItemTemplate>
</asp:TemplateField>
Depending on what you want to do, this may be awkward - you don't have access to the containing <td> (or <tr> for that matter) and you'll have to repeat the formatting for each cell.
The GridView class goes to a lot of lengths to hide the details of HTML and styling from you. After all you could create a GridView control adapter that wouldn't even render as HTML tables. (Unlikely though that may be.)
So even though you're trying to avoid it, you're probably best off dealing with this in a OnRowDataBound handler - or use a Repeater (if that's appropriate).
I know it has been almost a year, but if anyone else is trying this, try to subclass the GridView.
public class GridViewCSSRowBindable : GridView
{
public string DataFieldRowCSSClass { get; set; }
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
base.OnRowDataBound(e);
if (!string.IsNullOrEmpty(DataFieldRowCSSClass))
{
//This will throw an exception if the property does not exist on the data item:
string cssClassString = DataBinder.Eval(e.Row.DataItem, DataFieldRowCSSClass) as string;
if (!string.IsNullOrEmpty(cssClassString))
{
string sep = string.IsNullOrEmpty(e.Row.CssClass) ? string.Empty : " ";
e.Row.CssClass += sep + cssClassString;
}
}
}
}
And then in your Page:
<custom:GridViewCSSRowBindable ID="gvExample" runat="server" DataFieldRowCSSClass="RowCSS">
</custom:GridViewCSSRowBindable>
The objects being bound to this example GridView should have a public string RowCSS property.
If you haven't used inherited controls before, you might have to look up how to set that up in your project.
foreach (TableCell gvc in gvRowPhistry.Cells)
{
gvc.ForeColor = System.Drawing.Color.Blue;
}

How to use higher level datasource in asp.net custom usercontrol?

I'm setting up a web application with multiple forms. Each form is defined within an asp:FormView with the DataSource set to an ObjectDataSource. Each form contains it's own set of fields and always contains one or more blocks of fields, which are the same for multiple forms.
Because this blocks are the same, I decided to define them in a custom usercontrol. The questions that came up with this:
How can I use the same datasource
for the input fields in the
usercontrol as in the 'higher'
asp:FormView?
Is it possible to use DataBinding.Bind() for the input fields in the usercontrol, with this same datasource?
Thanks in advance for replies.
After a long search, I found a similar problem on this website: http://weblogs.asp.net/anasghanem/archive/2009/03/31/sharing-formview-edit-and-insert-templates-and-avoid-duplicate-markup.aspx.
In short the solution hierarchy:
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" DataSourceID="ObjectDataSource1">
<uc1:TestControl ID="TestControl1" runat="server" PhoneNumber='<%# Bind("PhoneNumber") %>' />
And the codebehind for the usercontrol:
[Bindable(true)]
public partial class TestControl : System.Web.UI.UserControl
{
[Bindable(true), DefaultValue("")]
public string PhoneNumber
{
get
{
return this.PhoneTextBox.Text;
}
set
{
this.PhoneTextBox.Text = value;
}
}
}

Resources