GridView, ODS, Entity and DataObjectFieldAttribute

A

Andrew Robinson

Does the DataObjectFieldAttribute have any affect on the GridView by way of
an ODS?

In partuculare, I was hoping that the max length of a text box within a
gridview would be set to the length property of the attribute but it seems
to have no affect. Any info would be helpful. Thanks,



public class CustomerEntity {

[DataObjectField(false, false, false, 25)]
public String FirstName {
get { return firstName; }
set { firstName = value; }
}
 
W

Walter Wang [MSFT]

Hi Andrew,

It seems the DataObjectFieldAttribute currently is only used by designers
such as ObjectDataSourceDesigner. It's not used at run-time by GridView to
configure the default edit template's TextBox's MaxLength.

I'll see if there's a better workaround other than creating a TemplateField
and set the MaxLength manually.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Andrew Robinson

You answer is what I thought to be the case. Its addition would be a nice
feature for Orcas.

Guessing I will attempt to write a little "Helper" method that reads a
GridView and sets the templated field maxLength properties but guessing that
might not be possible with non-templated bound columns.

-Andy
 
W

Walter Wang [MSFT]

Hi Andrew,

For non-templated bound columns, we could use the RowDataBound event to
manually set the TextBox's MaxLength according to the bound object's
DataObjectFieldAttribute:

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Gridview1.EditIndex != -1 && e.Row.RowIndex == Gridview1.EditIndex
&& e.Row.DataItem != null)
{
Type t = e.Row.DataItem.GetType();
Dictionary<string, int> ml = new Dictionary<string,int>();

foreach (PropertyInfo pi in t.GetProperties())
{
DataObjectFieldAttribute dofa =
Attribute.GetCustomAttribute(pi, typeof(DataObjectFieldAttribute)) as
DataObjectFieldAttribute;
if (dofa != null && dofa.Length >= 0)
{
ml.Add(pi.Name, dofa.Length);
}
}

if (ml.Count > 0)
{
foreach (DataControlFieldCell dcfc in e.Row.Cells)
{
BoundField bf = dcfc.ContainingField as BoundField;
if (bf != null && dcfc.Controls.Count > 0)
{
TextBox txt = dcfc.Controls[0] as TextBox;
if (txt != null)
{
if (ml.ContainsKey(bf.DataField))
{
txt.MaxLength = ml[bf.DataField];
}
}
}
}
}
}
}


Hope this helps.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Andy,

Have you tried the workaround? Does that work for you? Please feel free to
let me know if there's anything else I can help.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top