GridViewDesigner.GetDesignTimeHtml Method ()


Gets the markup that is used to render the associated control at design time.

Namespace:   System.Web.UI.Design.WebControls
Assembly:  System.Design (in System.Design.dll)

public override string GetDesignTimeHtml()

Return Value

Type: System.String

A String containing the markup used to render the GridView at design time.

The GetDesignTimeHtml() method does the following:

  1. Sets the AutoGenerateColumns property of the control to true, if the Columns property is empty.

  2. Sets the DataKeyNames property of the control to null, if the schema of the data source cannot be obtained.

  3. Refreshes the TypeDescriptor object to force the PreFilterProperties method to be called.

  4. Calls the base method to generate the markup.

Notes to Inheritors:

If you override the GetDesignTimeHtml method, be sure to call the base method, because it eventually, through several override levels, calls on the GridView control or a copy of the control to generate the markup.

The following code example shows how to override the GetDesignTimeHtml method in a class that is inherited from the GridViewDesigner class to change the appearance of the GridView control at design time. The example adds a new first row to the grid to contain the Caption property, if the Caption is defined. If the BorderStyle property of the control that is derived from the GridView class has the NotSet or None value, the GetDesignTimeHtml draws a blue dashed border around the control to make its extent more visible. It does not change the run-time appearance of the control.

// Generate the design-time markup.
const string capTag = "caption";
const string trOpen = "tr><td colspan=9 align=center";
const string trClose = "td></tr";

public override string GetDesignTimeHtml()
    // Make the full extent of the control more visible in the designer.
    // If the border style is None or NotSet, change the border to
    // a wide, blue, dashed line. Include the caption within the border.
    MyGridView myGV = (MyGridView)Component;
    string markup = null;
    int charX;

    // Check if the border style should be changed.
    if (myGV.BorderStyle == BorderStyle.NotSet ||
        myGV.BorderStyle == BorderStyle.None)
        BorderStyle oldBorderStyle = myGV.BorderStyle;
        Unit oldBorderWidth = myGV.BorderWidth;
        Color oldBorderColor = myGV.BorderColor;

        // Set the design-time properties and catch any exceptions.
            myGV.BorderStyle = BorderStyle.Dashed;
            myGV.BorderWidth = Unit.Pixel(3);
            myGV.BorderColor = Color.Blue;

            // Call the base method to generate the markup.
            markup = base.GetDesignTimeHtml();
        catch (Exception ex)
            markup = GetErrorDesignTimeHtml(ex);
            // Restore the properties to their original settings.
            myGV.BorderStyle = oldBorderStyle;
            myGV.BorderWidth = oldBorderWidth;
            myGV.BorderColor = oldBorderColor;
        // Call the base method to generate the markup.
        markup = base.GetDesignTimeHtml();

    // Look for a <caption> tag.
    if ((charX = markup.IndexOf(capTag)) > 0)
        // Replace the first caption with 
        // "tr><td colspan=9 align=center".
        // It is okay if the colspan exceeds the 
        // number of columns in the table.
        markup = markup.Remove(charX,
            capTag.Length).Insert(charX, trOpen);

        // Replace the second caption with "td></tr".
        if ((charX = markup.IndexOf(capTag, charX)) > 0)
            markup = markup.Remove(charX,
                capTag.Length).Insert(charX, trClose);
    return markup;

} // GetDesignTimeHtml

.NET Framework
Available since 2.0
Return to top