DevExpress control with different field name and JavaScript control name - devexpress

When you render a DevExpress MVC control, you define the Name field, which is used both for the model binding and the name of the JavaScript object that is created to access the control.
Is there a way to define a different name for the JavaScript object?
Here is the code I'm using:
var textBoxSettings = new TextBoxSettings
{
Name = "ModelName.FieldName",
};
writer.Write(devExpress.TextBox(textBoxSettings).GetHtml());

use the settings.Properties.ClientInstanceName

Related

Binding to self in code

I'm defining a DataTemplate in code and I want to set the binding to itself.
DataTemplate = new DataTemplate(typeof(MyCustomViewCell));
DataTemplate.SetBinding(MyCustomViewCell.MyCustomProperty, "");
The code above doesn't work. You can't use an empty path in that method. The XAML equivalent I usually use is MyCustomProperty="{Binding}". This sets the property to the current BindingContext / DataContext.
What is the syntax to achieve this in C#?
Use a "." as the binding path to refer to the BindingContext itself.
DataTemplate.SetBinding<YourType>(MyCustomViewCell.MyCustomProperty, x => x.PropertyOfYourType);

How are DataTextField and DataValueFields evaluated

For a data bound control it is common scenario where we provide data text field and data value field ( in simple controls like Dropdownlist) but more fields in controls like Gridview. Generally the datasource is of type IEnumerable.
How does the control internally process these values or rather how do they get the value from the data source without knowing what kind of datasource they are dealing with.
Can someone explain with code how the controls evaluate these fields from the data source.
Typically, the data-bound control (or concerned components such as
DataControlField in GridView) will handle DataBinding event.
Within event handler, the data item that is being currently bound (e.g. DataRowView or entity instance) is retrieved. This is done via DataBinder.GetDataItem passing the actual control or control's NamingContainer. For example, if you are implementing a lower level control such as DataControlField for higher level data-bound control such as GridView then it would handle data-binding of a cell control and hence it will use cell's naming container to pass to DataBinder.GetDataItem method which uses current data binding context to get the same.
Once the data item object is retrieved, one need to evaluate the given data-binding expression against it to get the actual value and apply any formatting as per different properties set to the control/component. The most simple way is to use DataBinder.Eval overload. However, one may use the more efficient ways - for example, say DataField string is going to be only property name then you may look and cache the property descriptor and then use the same against different data items.
I will suggest you to use tool such as Reflector to inspect relevant control's code to get the better idea.
I never knew i could find this information so easily and LLyod was in fact wrong on using reflection to find data from a datasource. None of the data controls use it when i inspected through Reflector ;(
link that solved the problem
http://msdn.microsoft.com/en-us/library/ms366540.aspx
how you do it is below
protected override void PerformDataBinding(IEnumerable retrievedData)
{
base.PerformDataBinding(retrievedData);
// Verify data exists.
if (retrievedData != null)
{
string dataStr = String.Empty;
foreach (object dataItem in retrievedData)
{
if (DataTextField.Length > 0)
{
dataStr = DataBinder.GetPropertyValue(dataItem,
DataTextField, null);
}
else
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(dataItem);
if (props.Count >= 1)
{
if (null != props[0].GetValue(dataItem))
{
dataStr = props[0].GetValue(dataItem).ToString();
}
}
}
}
}
}
If the above code seem Greek and Latin , you will have to have a course on asp.net controls development to understand what is being done.

Does Grails have a built-in way to bind a string to a date for a single bean property?

I know that it's possible to register a custom property editor, as demonstrated here. It is my understanding that this will cause all properties for the registered type to be bound using that custom editor.
(Perhaps that's a misunderstanding on my part? If so, enlighten me!)
However, what if I only want to register a custom editor for a single property on a given domain?
Example:
class MyCommand {
Date foo // bind this using a custom format, e.g. 'yyyy-MM-dd'
Date bar // bind this using the normal 'struct' date picker fields
}
class MyController {
def myAction = { MyCommand command ->
// params has [foo: '2011-01-01', bar: 'struct', ...]
// which get bound to command as above
}
}
Does Grails have a built-in way to do this?
I know that it's possible to register a custom property editor, as demonstrated here. It is my understanding that this will cause all properties for the registered type to be bound using that custom editor.
AFAIK, this is correct
However, what if I only want to register a custom editor for a single property on a given domain?
If you're using Grails 2.3.0 or later, you can do this like so:
class MyCommand {
#BindingFormat('yyyy-MM-dd')
Date foo // bind this using a custom format, e.g. 'yyyy-MM-dd'
Date bar // bind this using the normal 'struct' date picker fields
}
You could write getter and setters that take the format you want and return the format you want. The DateFormatter or SimpleDateFormatter classes would be useful in your get/set methods.
You can still use the neat trick for dates in grails:
<g:form>
<g:hiddenField name="myDomainInstance.bar_year" value="2011"/>
<g:hiddenField name="myDomainInstance.bar_month" value="07"/>
<g:hiddenField name="myDomainInstance.bar_day" value="01"/>
<g:textField name="myDomainInstance.bar_hour" value=""/>
<g:textField name="myDomainInstance.bar_minute" value=""/>
</g:form>
In controller:
myDomainInstance.properties = params.myDomainInstance
Will result in the desired date for bar.

access a public function in .ascx

how can i access a public function in an .ascx file using C#?
Thanks
If the function is not static you will need to first obtain an instance of the class containing the function and then invoke the function on this instance. For example:
<%
// obtain an instance of the type containing the function
Foo instance = new Foo();
// invoke the function on this instance
string result = instance.Bar();
%>
Obviously it would be better to do this in the code behind file instead of polluting your markup.
Like other public functions in .NET Framework - via object reference. But sometimes Visual Studio doesn't automatically see your User Control's public members. Try to rebuild your user control and the site if IntelliSense window doesn't show it to you.
Where are you calling the function from? The containing page? Masterpage? Parent control? The control itself? Regardless, you'll need to somehow obtain a reference to the control instance (unless the method is static) in order to invoke this method. And the type of your reference must match that of the class that defines the method.
Edit:
MyControl myControl = (MyControl)Page.FindControl("Id_Of_The_Control");
if (myControl != null)
{
myControl.TheMethod();
}
if you don't want to add your ascx control into a placeholder programmatically, just implement IAttributeAccessor and IUserControlDesignerAccessor interfaces to your user control class like;
public partial class yourascxclassname: System.Web.UI.UserControl, IAttributeAccessor, IUserControlDesignerAccessor
you can access only public members of your ascx control.

Best way of implementing DropDownList in ASP.NET MVC 2?

I am trying to understand the best way of implementing a DropDownList in ASP.NET MVC 2 using the DropDownListFor helper. This is a multi-part question.
First, what is the best way to pass the list data to the view?
Pass the list in your model with a SelectList property that contains the data
Pass the list in via ViewData
How do I get a blank value in the DropDownList? Should I build it into the SelectList when I am creating it or is there some other means to tell the helper to auto create an empty value?
Lastly, if for some reason there is a server side error and I need to redisplay the screen with the DropDownList, do I need to fetch the list values again to pass into the view model? This data is not maintained between posts (at least not when I pass it via my view model) so I was going to just fetch it again (it's cached). Am I going about this correctly?
Your best bet is to create a SelectList in your Controller - use my extension method here:
http://blog.wekeroad.com/2010/01/20/my-favorite-helpers-for-aspnet-mvc
Pop that into ViewData using the same key as your property name:
ViewData["statusid"]=MySelectList
Then just use Html.DropDownFor(x=>x.StatusID) and you're all set.
Answering in parts:
The best way IMHO is to pass the list in the ViewModel like this:
public SelectList Colors
{
get
{
// Getting a list of Colors from the database for example...
List<Color> colors = GetColors().ToList();
// Returning a SelectList to be used on the View side
return new SelectList(colors, "Value", "Name");
}
}
To get a blank or default option like ( -- Pick a color -- ), you can do this on the view side:
#Html.DropDownListFor(m => m.Color, Model.Colors, "-- Pick a color --")
You'll have to fetch/populate the list again if it's part of the ViewModel.
Take a look at the following blog post. It can give you some tips:
Drop-down Lists and ASP.NET MVC
You could do something like:
<%= Html.DropDownListFor((x => x.ListItems), Model.ListItems, "")%>
or
<%= Html.DropDownList("ListItems", Model.ListItems, "")%>
The last param 'optionLabel' makes a blank list item
In this case, you can see ListItems is a property of the model.
I have made the view strongly typed to the model also.
(You know this already!)
Pass the list in your model with a SelectList property that contains the data
Yes, add it when you build the SelectList. (If you build the list using LINQ, Union might come in handy.)
Yes do do, and yes you are.
I find it more intuitive to work with a sequence of SelectListItems (rather than a SelectList).
For example, this would create an IEnumerable<SelectListItem> from a sequence of customer objects that you can pass to the Html.DropDownListFor(...) helper. The 'Selected' property will optionally set the default item in the dropdown list.
var customers = ... // Get Customers
var items = customers.Select(c => new SelectListItem
{
Selected = (c.Id == selectedCustomerId),
Text = c.Email,
Value = c.Id.ToString()
});

Resources