DynamicField within GridView truncating datahttp://forums.asp.net/t/1573073.aspx/1?DynamicField+within+GridView+truncating+dataFri, 19 Nov 2010 00:03:14 -050015730733947288http://forums.asp.net/p/1573073/3947288.aspx/1?DynamicField+within+GridView+truncating+dataDynamicField within GridView truncating data <p><font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"></p> <p>When I use a DynamicField within a<font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas">GridView the data displayed in the column is truncated. How can I control this behaviour?</p> </font></font></font></font></font></font> <p></p> 2010-06-28T05:06:14-04:003947538http://forums.asp.net/p/1573073/3947538.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>hi,</p> <p>&nbsp;&nbsp; Please check datasource function you are binding the gridview, whether it is returning full data or it is getting truncated in&nbsp;</p> <p>the function or query itself.</p> 2010-06-28T07:43:25-04:003947542http://forums.asp.net/p/1573073/3947542.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>There can be multiple reasons for that to be...&nbsp; Is the max length property set for the text field you are using in the gridview ???<br> </p> 2010-06-28T07:45:35-04:003947604http://forums.asp.net/p/1573073/3947604.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>The data isn't truncated in the source. If I&nbsp;use a BoundField instead of a DynamicField the data isn't truncated.</p> <p>Cheers<font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"><font color="#800000" size="2" face="Consolas"></p> <p>&nbsp;</p> </font></font></font> 2010-06-28T08:18:09-04:003947611http://forums.asp.net/p/1573073/3947611.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>The DynamicField control doesn't have a max length property.&nbsp;</p> 2010-06-28T08:20:32-04:003947718http://forums.asp.net/p/1573073/3947718.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Hi Simon, if the field is a long test field you may need to add a UIHint to the override the field template it is using.</p> <p>I would put a break point in each of the possible field templates and then debug to see which is being used, or use F12 in IE8 to get the Dev tools and look at the column to see how it is being rendered, as this bould be a css issue (i.e. overflow set to hidden)</p> 2010-06-28T09:18:21-04:003947908http://forums.asp.net/p/1573073/3947908.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p style="margin:0in; font-family:Verdana; color:black; font-size:9pt">Thanks for pointing me in the right direction Steve. The issue was simply due to the&nbsp;MAX_DISPLAYLENGTH_IN_LIST constant in the default Text field template.&nbsp;Exactly where it should be <img title="Embarassed" border="0" alt="Embarassed" src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-embarassed.gif"></p> <p style="margin:0in">&nbsp;</p> <p style="margin:0in; font-family:Verdana; color:black; font-size:9pt">Now all I need is an attribute to replace this constant. Don't suppose you've got one of those up your sleeve <img title="Laughing" border="0" alt="Laughing" src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-laughing.gif"></p> <p style="margin:0in; font-family:Calibri; font-size:11pt">&nbsp;</p> <p style="margin:0in; font-family:Calibri; font-size:11pt">Thanks, Simon</p> 2010-06-28T11:14:43-04:003949807http://forums.asp.net/p/1573073/3949807.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Hi Simon, yes I do [:)]</p> <pre class="prettyprint">[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class FieldOptionsAttribute : Attribute { /// &lt;summary&gt; /// Gets or sets the rows. /// &lt;/summary&gt; /// &lt;value&gt;The rows.&lt;/value&gt; public int Rows { get; set; } /// &lt;summary&gt; /// Gets or sets the columns. /// &lt;/summary&gt; /// &lt;value&gt;The columns.&lt;/value&gt; public int Columns { get; set; } }</pre> <P>And then change your Test.aspx page to this.</P><pre class="prettyprint">public partial class TextField : System.Web.DynamicData.FieldTemplateUserControl { private const int MAX_DISPLAYLENGTH_IN_LIST = 25; public override string FieldValueString { get { string value = base.FieldValueString; if (ContainerType == ContainerType.List) { var fieldOptions = MetadataAttributes.OfType&lt;FieldOptionsAttribute&gt;().FirstOrDefault(); int maxColumns; if (fieldOptions != null &amp;&amp; fieldOptions.Columns &gt; 0) maxColumns = fieldOptions.Columns; else maxColumns = MAX_DISPLAYLENGTH_IN_LIST; if (value != null &amp;&amp; value.Length &gt; maxColumns) value = value.Substring(0, maxColumns - 3) + "..."; } return value; } } public override Control DataControl { get { return Literal1; } } }</pre> <p>Shouls work, I also use the&nbsp;Row for the MultilineText_Edit field to alter&nbsp;how many rows it has.<br> &nbsp;</p> 2010-06-29T10:37:01-04:003951049http://forums.asp.net/p/1573073/3951049.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Thanks Steve. I jumped the gun and built one myself yesterday - my first metadata attribute. Pretty similar to yours except you also have the Row parameter which should be useful. I'll include mine here because I also implemented it in the ForeignKey field template, which might be of interest to someone.</p> <pre class="prettyprint">[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class MaxDisplayLengthInListAttribute : Attribute { public Int16 Length { get; private set; } public MaxDisplayLengthInListAttribute(Int16 length) { Length = length; } }</pre> <P><BR>Text.ascx</P><pre class="prettyprint">public override string FieldValueString { get { string value = base.FieldValueString; if (ContainerType == ContainerType.List) { Int16 MaxLength = 25; var metadata = MetadataAttributes.OfType&lt;MaxDisplayLengthInListAttribute&gt;().FirstOrDefault(); if (metadata != null) { MaxLength = metadata.Length; } if (value != null &amp;&amp; value.Length &gt; MaxLength) { value = value.Substring(0, MaxLength - 3) + "..."; } } return value; } }</pre> <P>ForeignKey.ascx</P><pre class="prettyprint">protected string GetDisplayString() { object value = FieldValue; if (value == null) { return FormatFieldValue(ForeignKeyColumn.GetForeignKeyString(Row)); } else { string stringValue = FormatFieldValue(ForeignKeyColumn.ParentTable.GetDisplayString(value)); if (ContainerType == ContainerType.List) { Int16 MaxLength = 20; var metadata = MetadataAttributes.OfType&lt;MaxDisplayLengthInListAttribute&gt;().FirstOrDefault(); if (metadata != null) { MaxLength = metadata.Length; } if (stringValue.Length &gt; MaxLength) { stringValue = stringValue.Substring(0, MaxLength - 3) + "..."; } } return stringValue; } }</pre> <p><br> <br> <br> &nbsp;</p> <p><br> &nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2010-06-30T01:07:19-04:003951599http://forums.asp.net/p/1573073/3951599.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Excelant job Simon, I'll certanly look at you sample [:)]</p> 2010-06-30T09:05:12-04:004163602http://forums.asp.net/p/1573073/4163602.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>I commented out the code to&nbsp;truncate the FieldValueString, but the rendered text doesn't wrap since the td tag has&nbsp;style=&quot;white-space: nowrap;&quot;&nbsp;</p> <p>There is no code to set the wrap = false anywhere.</p> <p>Why is this happening?</p> 2010-11-10T23:48:33-05:004163613http://forums.asp.net/p/1573073/4163613.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>i found a workaround by&nbsp;adding the following css code to List.aspx, but i rather get rid of the extra style code from the td tag.</p> <p>td</p> <p>{</p> <p>&nbsp; white-space:normal;</p> <p>}</p> 2010-11-10T23:58:52-05:004163691http://forums.asp.net/p/1573073/4163691.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Hi Yuipcheng, it would appear that DD is adding this and I can't find a way around it yet [:(]</p> 2010-11-11T01:16:11-05:004167960http://forums.asp.net/p/1573073/4167960.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Hi Yuipcheng, David Fowler gave me the solution, which is to create a class based on the DefaultAutoFieldGenerator and override the CreateField method:</p> <pre class="prettyprint">/// &lt;summary&gt; /// Add the option to the Default Auto Field Generator /// to not add the (Style=&quot;white-space: nowrap;&quot;) to each field. /// &lt;/summary&gt; public class AdvancedAutoFieldGenerator : DefaultAutoFieldGenerator { private Boolean _noWrap; /// &lt;summary&gt; /// Initializes a new instance of the /// &lt;see cref=&quot;AdvancedAutoFieldGenerator&quot;/&gt; class. /// &lt;/summary&gt; /// &lt;param name=&quot;table&quot;&gt;The table.&lt;/param&gt; /// &lt;param name=&quot;noWrap&quot;&gt;if set to &lt;c&gt;true&lt;/c&gt; /// the (Style=&quot;white-space: nowrap;&quot;) is added /// to each field.&lt;/param&gt; public AdvancedAutoFieldGenerator(MetaTable table, Boolean noWrap) : base(table) { _noWrap = noWrap; } protected override DynamicField CreateField( MetaColumn column, ContainerType containerType, DataBoundControlMode mode) { DynamicField field = base.CreateField(column, containerType, mode); field.ItemStyle.Wrap = !_noWrap; return field; } }</pre> <P>In the CreateFiedl method we call the base and then set the field.ItemStyle.Wrap value to turn off the nowrap feature.</P> <P>I decided that I wanted to be able to turn this off or leave it on so I added a parameter to the contstuctor for this.</P> <P>We use this on the List page in the Page_Load event (if we do it6 any earlier then the DefaultAutoFieldGenerator will still be applied instead)&nbsp;like this:</P><pre class="prettyprint">// apply custom auto field generator GridView1.ColumnsGenerator = new AdvancedAutoFieldGenerator(table, false);</pre> <p>setting the noWrap parameter to false will turn off the feature wich adds the style=&quot;white-space:nowrap;&quot;&nbsp;to each table cell and row.</p> <p>Hope this helps [:)]</p> 2010-11-14T10:27:25-05:004175188http://forums.asp.net/p/1573073/4175188.aspx/1?Re+DynamicField+within+GridView+truncating+dataRe: DynamicField within GridView truncating data <p>Steve,</p> <p>that works! </p> <p>Thank you!&nbsp;</p> <p>yuipcheng</p> 2010-11-19T00:03:14-05:00