Re: GridView DataBind() Not Working



Thanks for your reply Mark,

yes, based on my research, if you completely add the rendering code
logic(both for runtime and design-time) in your control's
code(CreateChildcontrols), the designer (of your toplevel custom control)
will not render the child GridView. Also, as you discussed in another
thread with my colleague Walter, for child controls added into top custom
control's controls collection, it hasn't a designer associated like the top
level control.

Currently, what I'm still trying is use our own custom control designer and
render the control (and its child controls) manually. And I've got the
following result so far:

*in our custom control designer, we can create a new instance of our custom
control type and render it(or you can set some additional attributes on it)
e.g.

=================================
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyComposite runat=server></{0}:MyComposite>")]
[Designer(typeof(MyCompositeDesigner))]
public class MyComposite : CompositeControl
{
Label _label;
GridView _grid;


[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}



protected override void CreateChildControls()
{
Controls.Clear();

_label = new Label();
_label.ID = "lblMessage";
Controls.Add(_label);

_label.Text = Text;

_grid = new GridView();
_grid.ID = "gvData";
Controls.Add(_grid);

_grid.AutoGenerateColumns = true;

_grid.DataSource = HelperUtil.GetDummyListItems();
_grid.DataBind();


}




}

internal class HelperUtil
{
internal static ListItem[] GetDummyListItems()
{
ListItem[] items = new ListItem[5];
for (int i = 0; i < items.Length; i++)
{
items[i] = new ListItem("key" + i, "value" + i);
}

return items;
}
}

internal class MyCompositeDesigner : CompositeControlDesigner
{

public override string GetDesignTimeHtml()
{
string html = string.Empty;

MyCompositeControl mc1 = new MyCompositeControl();


ICompositeControlDesignerAccessor accessor = mc1 as
ICompositeControlDesignerAccessor;

accessor.RecreateChildControls();



StringWriter sw = new StringWriter();
XhtmlTextWriter hw = new XhtmlTextWriter(sw);

mc1.RenderControl(hw);

html+= sw.ToString();


return html;
}
}
==========================

However, if I try using the control instance from the designer's
"ViewControl" property to render, the child GridView always not display.
e.g.
======================
public override string GetDesignTimeHtml()
{
string html = string.Empty;



WebControl mc1 = new MyComposite();

mc1 = this.ViewControl as WebControl;


ICompositeControlDesignerAccessor accessor = mc1 as
ICompositeControlDesignerAccessor;

accessor.RecreateChildControls();




StringWriter sw = new StringWriter();
XhtmlTextWriter hw = new XhtmlTextWriter(sw);

mc1.RenderControl(hw);

html+= sw.ToString();


return html;
}
=======================

I will do some further research on this to see whether it does be not
supported to use the original instance for manual rendering in designer or
if there is any other means. Anyway, at least you can do custom rendering
in custom control designer by manually creating a control instance. I'll
update you when I get any progress at the earliest time.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



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




.



Relevant Pages

  • Re: Custom ContentPlaceHolder
    ... Render method without infinite recursion? ... It also worth noting that the custom ContentPlaceHolder control works just ... this a 100% designer issue. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: CreateChildControls(...) or Render(...)?
    ... You can simply associate your control to a custom control designer class ... method into another sub and have your createchildcontrols method call this. ... This way you can call this new sub from your designer class and render the ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • RE: DataSourceID not working in composite co
    ... > It seems you're going on for your custom webcontrols:). ... > DataBoundControl class which has implemented some of the designer ... > Actions(in the Designer) so that the child control classes and utilize. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: About the "Page.RegisterRequiresRaiseEvent and Page.RequiresPo
    ... By the way,the submit button is rendered in the custom control's Render method. ... > control with the same UniqueID in the requested page.because the latter has ... >>> That line of code is because implementing IPostBackDataHandler goes over ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: Designtime Attributes - next one...
    ... My app needs access to the control props attributes, ... custom properties AND some designer / VS type (the usual suspects, Category, ... uses both assemblies, ControlLibrary1.dll and ...
    (microsoft.public.dotnet.framework.compactframework)