I'm using DynamicField and DynamicControl in a GridView and in RowCommand event I want to access values of these columns. How can I do that because GridViewRow FindControl method needs an Id and DynamicField has no Id. This method returns DynamicControl
for templated fields but I cannot find the value! the followng is a sample of columns:
Hi Ali, I developed a FindControl extension method for this:
/// <summary>
/// Get the control by searching recursively for it.
/// </summary>
/// <param name="Root">The control to start the search at.</param>
/// <param name="Id">The ID of the control to find</param>
/// <returns>The control the was found or NULL if not found</returns>
public static Control FindControlRecursive(this Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
/// <summary>
/// Finds the control recursive.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="root">The root.</param>
/// <returns></returns>
public static T FindControlRecursive<T>(this Control root) where T : Control
{
var control = root as T;
if (control != null)
return control;
foreach (Control Ctl in root.Controls)
{
T FoundCtl = Ctl.FindControlRecursive<T>();
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
you can search for the control with the field name or you can get a particular Type i.e. just of Type FieldTemplate or the actual type of the specific field template.
Now to why are you doing this? I only ask because there may be an easier way than what you are doing :)
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
The case is that I have a class and it has nothing to do with EF and other DB related technologies. It is populated and I want to use GridView to list the data. I thought it's a good idea to use DD to show fields and it worked partially.
I think there is no way to access a field data when the DynamicField is used. In the case of using DynamicControl the first method returns null but the FindControl do find the DynamicControl but there is no way to access the data in the control. I remember
when I worked on field templates, there was FieldValue that had the data. It seems that DynamicControl does not contain the data but is responsible for finding appropriate field template. Is there any way?
Thanks Steve. I told you what I'm trying to do. There is no problem because I used non DD controls and they are working fine. I was just curious about the possibility and the reason of using DD controls was that I like DD and the way of DD programming very
much.
Member
130 Points
229 Posts
Get DynamicField and DynamicControl value in Gridview
Sep 06, 2014 07:51 AM|IranianCuriousBoy|LINK
Hi
I'm using DynamicField and DynamicControl in a GridView and in RowCommand event I want to access values of these columns. How can I do that because GridViewRow FindControl method needs an Id and DynamicField has no Id. This method returns DynamicControl for templated fields but I cannot find the value! the followng is a sample of columns:
Thanks
All-Star
17916 Points
5681 Posts
MVP
Re: Get DynamicField and DynamicControl value in Gridview
Sep 07, 2014 07:04 AM|sjnaughton|LINK
Hi Ali, I developed a FindControl extension method for this:
you can search for the control with the field name or you can get a particular Type i.e. just of Type FieldTemplate or the actual type of the specific field template.
Now to why are you doing this? I only ask because there may be an easier way than what you are doing :)
Always seeking an elegant solution.
Member
130 Points
229 Posts
Re: Get DynamicField and DynamicControl value in Gridview
Sep 08, 2014 06:11 AM|IranianCuriousBoy|LINK
Hi Steve
The case is that I have a class and it has nothing to do with EF and other DB related technologies. It is populated and I want to use GridView to list the data. I thought it's a good idea to use DD to show fields and it worked partially.
I think there is no way to access a field data when the DynamicField is used. In the case of using DynamicControl the first method returns null but the FindControl do find the DynamicControl but there is no way to access the data in the control. I remember when I worked on field templates, there was FieldValue that had the data. It seems that DynamicControl does not contain the data but is responsible for finding appropriate field template. Is there any way?
Thanks
All-Star
17916 Points
5681 Posts
MVP
Re: Get DynamicField and DynamicControl value in Gridview
Sep 08, 2014 07:23 AM|sjnaughton|LINK
Hi you are correct you can still get the value however by using the DataControl property and parse the control type.
Always seeking an elegant solution.
Member
130 Points
229 Posts
Re: Get DynamicField and DynamicControl value in Gridview
Sep 09, 2014 09:33 AM|IranianCuriousBoy|LINK
Hi Steve
DynamicControl does not have DataControl property. Please elaborate.
Thanks
All-Star
17916 Points
5681 Posts
MVP
Re: Get DynamicField and DynamicControl value in Gridview
Sep 09, 2014 10:56 AM|sjnaughton|LINK
But that is just a wrapper for FieldTemplateUserControl :)
Always seeking an elegant solution.
Member
130 Points
229 Posts
Re: Get DynamicField and DynamicControl value in Gridview
Sep 09, 2014 12:52 PM|IranianCuriousBoy|LINK
Where can I find this wrapper for FieldTemplateUserControl(DataControl Property)?
All-Star
17916 Points
5681 Posts
MVP
Re: Get DynamicField and DynamicControl value in Gridview
Sep 10, 2014 03:45 AM|sjnaughton|LINK
if you debug look at the DynamicControl you will see your Field Template inside.
Always seeking an elegant solution.
Member
130 Points
229 Posts
Re: Get DynamicField and DynamicControl value in Gridview
Sep 13, 2014 06:38 AM|IranianCuriousBoy|LINK
Hi Steve
I did not find the field value. Have you got any practical use of it(in the forms of an article, etc)
Thanks
All-Star
17916 Points
5681 Posts
MVP
Re: Get DynamicField and DynamicControl value in Gridview
Sep 15, 2014 05:40 AM|sjnaughton|LINK
Hi Ali, no I never get a value like that, if you can tell me what you are trying to achieve I may have another method I can direct you to.
Always seeking an elegant solution.
Member
130 Points
229 Posts
Re: Get DynamicField and DynamicControl value in Gridview
Sep 15, 2014 01:07 PM|IranianCuriousBoy|LINK
Thanks Steve. I told you what I'm trying to do. There is no problem because I used non DD controls and they are working fine. I was just curious about the possibility and the reason of using DD controls was that I like DD and the way of DD programming very much.
Regards