I think i should able to do "Enable/Disable" using same code; but i don't get where should I have to implement "Enable/Disable". Even i couldn't see in the code visible=true and visible=false in existing code [which is already works fine] therefore i don't
know where shall i have to implement.
Hi there, in the article you use the attribute like in this excerpt:
[HideColumnIn(PageTemplate.List)] public Object RequiredDate { get; set; }
The attribute tells DD when to hide the field in the above case it will only hide the field in the List page.
Sorry did not fully read your example there is an example on my blog somwhere but you will need to use a custom Field Template factory to set the field template to read only.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Do you mean to create custom field template with enabled = false ?
In my case i wanted to do this on ForeignKey Column. So i created exectly same Field Template control with name "DisabledForeignKey.ascx" and "DisabledForeignKey_Edit.ascx" (Edit template created by default by DD)
I set UIHints("DisabledForeignKey") on require coloumn on MetaDataClass.
So on "List.aspx" page shows with "Hyperlink" and "Edit.aspx" page dropdownlist box comes with disabled.
This is exectly i wanted. But now when i go to "Insert.aspx" page to insert new entry this Dropdownlist box is disabled, while here i want to use this DDL.
Well i sorted out this issue by extending logic on Foreignkey_edit.ascx (default field template provided by DD); please suggest if i am wrong or any other better way.
By doing below I can able to control apearence of Field Template on basis of Entity (Table) and Views (Edit, List, Insert, Details);
//to get current entity MetaTable
table; table = DynamicDataRouteHandler.GetRequestMetaTable(Context); // checks the mode for which apply apearence of field template if
(Mode == DataBoundControlMode.Edit) { switch
(table.DisplayName) { case"Book":
//Entity table for which i want to disable DropDownList1.Enabled =false;
break; case"Category":
//Entity table for which i want to disable DropDownList1.Enabled =false; break; default: break; }
}
}
I assume that by doing above process can able to change apearence (Enable/Disable or Hide/Show) of any of the Field template by writing above process in respected field template for specific entity and view.
Please advice if this is not right way to do or suggest if there are other way to do this.
Sorry no what I meant was to create a custom FieldTemplateFactory and then when the field is generated you can use the same method to detect is the field should be set to read only here is a sample:
using System;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
namespace NotAClue.Web.DynamicData
{
public class AdvancedFieldTemplateFactory : FieldTemplateFactory
{
public override IFieldTemplate CreateFieldTemplate(MetaColumn column,
DataBoundControlMode mode,
String uiHint)
{
// code to fix caching issue
var pageTemplate = FieldTemplateExtensionMethods.GetPageTemplate();
var readOnlyIn = column.GetAttribute<ColumnReadOnlyInAttribute>();
if (column.IsReadOnly || (readOnlyIn != null && (readOnlyIn.PageTemplate & pageTemplate) == pageTemplate))
mode = DataBoundControlMode.ReadOnly;
return base.CreateFieldTemplate(column, mode, uiHint);
}
}
}
and here's the attribute
using System;
namespace NotAClue.ComponentModel.DataAnnotations
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ColumnReadOnlyInAttribute : Attribute
{
/// <summary>
/// Gets or sets the page template.
/// </summary>
/// <value>The page template.</value>
public PageTemplate PageTemplate { get; private set; }
/// <summary>
/// Gets or sets the disabled CSS class.
/// </summary>
/// <value>The disabled CSS class.</value>
public String DisabledCssClass { get; set; }
public ColumnReadOnlyInAttribute()
{
}
public ColumnReadOnlyInAttribute(PageTemplate pageTemplate)
{
PageTemplate = pageTemplate;
DisabledCssClass = DisabledCssClass ?? "disabled";
}
}
}
Hope that sorts it out for you
, I knew I had a sample somewhere.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
When you have time pls find the link of your sample and send it to me.
Just want to share my general thought;
when i started with DD i found extreamly easy (and it is to generate Admin UI) , Microsoft reduce lot work on one hand but also increase work on other hand as it requires to write extension code for basic requirements; so at end of the day it require same work
on normal web and DD development. Since microsoft already added some attributes for columns; i hope they will add such basic functionalities in comming version and stop us to keep writing code for basic task; i don't mind to write code for the component which
is not provided by microsoft but atleast not for such basic functionality.
Hi all my published samples are on my blog, I am working on getting all the curerent and uptodate stuff on NuGet and codeplex but work load are delaying me :( we all have to earn a crust.
I plan to have Project templates also adding all the major functionality of my extenhsions built in.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
pratiksolank...
Member
271 Points
76 Posts
Switch enable and disable (Field Templates) on page template basis
Sep 19, 2011 12:45 AM|LINK
Hi,
I want to enable and disable field template on basis of page template. I able to show or hide using code written by "NotAClue" (sjnaughton)
http://csharpbits.notaclue.net/2010/02/new-way-to-do-column-generation-in.html
I think i should able to do "Enable/Disable" using same code; but i don't get where should I have to implement "Enable/Disable". Even i couldn't see in the code visible=true and visible=false in existing code [which is already works fine] therefore i don't know where shall i have to implement.
Thanks
Pratik Solanki
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Switch enable and disable (Field Templates) on page template basis
Sep 19, 2011 12:19 PM|LINK
Hi there, in the article you use the attribute like in this excerpt:
[HideColumnIn(PageTemplate.List)] public Object RequiredDate { get; set; }The attribute tells DD when to hide the field in the above case it will only hide the field in the List page.
Sorry did not fully read your example there is an example on my blog somwhere but you will need to use a custom Field Template factory to set the field template to read only.
Always seeking an elegant solution.
pratiksolank...
Member
271 Points
76 Posts
Re: Switch enable and disable (Field Templates) on page template basis
Sep 19, 2011 09:49 PM|LINK
Hi sjnaughton,
Thanks for your reply
Do you mean to create custom field template with enabled = false ?
In my case i wanted to do this on ForeignKey Column. So i created exectly same Field Template control with name "DisabledForeignKey.ascx" and "DisabledForeignKey_Edit.ascx" (Edit template created by default by DD)
I set UIHints("DisabledForeignKey") on require coloumn on MetaDataClass.
So on "List.aspx" page shows with "Hyperlink" and "Edit.aspx" page dropdownlist box comes with disabled.
This is exectly i wanted. But now when i go to "Insert.aspx" page to insert new entry this Dropdownlist box is disabled, while here i want to use this DDL.
Well i sorted out this issue by extending logic on Foreignkey_edit.ascx (default field template provided by DD); please suggest if i am wrong or any other better way.
By doing below I can able to control apearence of Field Template on basis of Entity (Table) and Views (Edit, List, Insert, Details);
I added logic to OnDataBinding event
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
string selectedValueString = GetSelectedValueString();
ListItem item = DropDownList1.Items.FindByValue(selectedValueString);
if (item !=null)
{
DropDownList1.SelectedValue = selectedValueString;
}
// Above code from DD.
//to get current entity
MetaTable table;
table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
// checks the mode for which apply apearence of field template
if (Mode == DataBoundControlMode.Edit)
{
switch (table.DisplayName)
{
case "Book": //Entity table for which i want to disable
DropDownList1.Enabled =false;
break;
case "Category": //Entity table for which i want to disable
DropDownList1.Enabled =false;
break;
default:
break;
}
}
}
I assume that by doing above process can able to change apearence (Enable/Disable or Hide/Show) of any of the Field template by writing above process in respected field template for specific entity and view.
Please advice if this is not right way to do or suggest if there are other way to do this.
Appreciate your help
Praitk
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Switch enable and disable (Field Templates) on page template basis
Sep 19, 2011 11:02 PM|LINK
Sorry no what I meant was to create a custom FieldTemplateFactory and then when the field is generated you can use the same method to detect is the field should be set to read only here is a sample:
using System; using System.Web.DynamicData; using System.Web.UI.WebControls; using NotAClue.ComponentModel.DataAnnotations; namespace NotAClue.Web.DynamicData { public class AdvancedFieldTemplateFactory : FieldTemplateFactory { public override IFieldTemplate CreateFieldTemplate(MetaColumn column, DataBoundControlMode mode, String uiHint) { // code to fix caching issue var pageTemplate = FieldTemplateExtensionMethods.GetPageTemplate(); var readOnlyIn = column.GetAttribute<ColumnReadOnlyInAttribute>(); if (column.IsReadOnly || (readOnlyIn != null && (readOnlyIn.PageTemplate & pageTemplate) == pageTemplate)) mode = DataBoundControlMode.ReadOnly; return base.CreateFieldTemplate(column, mode, uiHint); } } }and here's the attribute
using System; namespace NotAClue.ComponentModel.DataAnnotations { [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class ColumnReadOnlyInAttribute : Attribute { /// <summary> /// Gets or sets the page template. /// </summary> /// <value>The page template.</value> public PageTemplate PageTemplate { get; private set; } /// <summary> /// Gets or sets the disabled CSS class. /// </summary> /// <value>The disabled CSS class.</value> public String DisabledCssClass { get; set; } public ColumnReadOnlyInAttribute() { } public ColumnReadOnlyInAttribute(PageTemplate pageTemplate) { PageTemplate = pageTemplate; DisabledCssClass = DisabledCssClass ?? "disabled"; } } }Hope that sorts it out for you
, I knew I had a sample somewhere.
Always seeking an elegant solution.
pratiksolank...
Member
271 Points
76 Posts
Re: Switch enable and disable (Field Templates) on page template basis
Sep 20, 2011 08:40 PM|LINK
Hi sjnaughton,
When you have time pls find the link of your sample and send it to me.
Just want to share my general thought;
when i started with DD i found extreamly easy (and it is to generate Admin UI) , Microsoft reduce lot work on one hand but also increase work on other hand as it requires to write extension code for basic requirements; so at end of the day it require same work on normal web and DD development. Since microsoft already added some attributes for columns; i hope they will add such basic functionalities in comming version and stop us to keep writing code for basic task; i don't mind to write code for the component which is not provided by microsoft but atleast not for such basic functionality.
Thanks
Pratik
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Switch enable and disable (Field Templates) on page template basis
Sep 20, 2011 09:28 PM|LINK
Hi all my published samples are on my blog, I am working on getting all the curerent and uptodate stuff on NuGet and codeplex but work load are delaying me :( we all have to earn a crust.
I plan to have Project templates also adding all the major functionality of my extenhsions built in.
Always seeking an elegant solution.
pratiksolank...
Member
271 Points
76 Posts
Re: Switch enable and disable (Field Templates) on page template basis
Sep 21, 2011 01:44 AM|LINK
Hi,
Thanks,
You are a champion
Regards
Pratik