locked
Get DynamicField and DynamicControl value in Gridview RRS feed

  • Question

  • User539648229 posted

    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:

                    <Columns>
                        <asp:DynamicField DataField="MonthDay" />
                        <asp:TemplateField HeaderText="Corrected Day">
                           <ItemTemplate>
                               <asp:DynamicControl runat="server" ID="CorrectedDayDynamicControl"
                                   DataField="CorrectedDay" Mode="Edit" />
                           </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

    Thanks

    Saturday, September 6, 2014 7:51 AM

All replies

  • User-330204900 posted

    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 :)

    Sunday, September 7, 2014 7:04 AM
  • User539648229 posted

    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

    Monday, September 8, 2014 6:11 AM
  • User-330204900 posted

    Hi you are correct you can still get the value however by using the DataControl property and parse the control type.

    Monday, September 8, 2014 7:23 AM
  • User539648229 posted

    Hi Steve

    DynamicControl does not have DataControl property. Please elaborate.

    Thanks

    Tuesday, September 9, 2014 9:33 AM
  • User-330204900 posted

    But that is just a wrapper for FieldTemplateUserControl :)

    Tuesday, September 9, 2014 10:56 AM
  • User539648229 posted

    Where can I find this wrapper for FieldTemplateUserControl(DataControl Property)?

    Tuesday, September 9, 2014 12:52 PM
  • User-330204900 posted

    if you debug look at the DynamicControl you will see your Field Template inside.

    Wednesday, September 10, 2014 3:45 AM
  • User539648229 posted

    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

    Saturday, September 13, 2014 6:38 AM
  • User-330204900 posted

    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.

    Monday, September 15, 2014 5:40 AM
  • User539648229 posted

    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

    Monday, September 15, 2014 1:07 PM