Re: GridView DataBind() Not Working

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Steven,

Thanks for the research. I await further results.

What do you mean by "...at least you can do custom rendering in custom control designer by manually creating a control instance"? Do
you mean to create a dummy GridView and slip it into the Controls collection for the control being designed? Or into the render
stream for GetDesignTimeHtml()?

- Mark

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: Literal control rendering empty
    ... If you still require the use of a custom control then you ... In that case you wouldn't have to render the child controls manually but you could still ... methods you override. ...
    (microsoft.public.dotnet.general)
  • Re: Literal control rendering empty
    ... If you still require the use of a custom control then you ... In that case you wouldn't have to render the child controls manually but you could still ... methods you override. ...
    (microsoft.public.dotnet.general)
  • Re: Text control?
    ... have to move to a rich edit control and use the ability of the rich edit control to render ... StreamIn and render on demand for each text object. ...
    (microsoft.public.vc.mfc)
  • Re: GridView DataBind() Not Working
    ... code(CreateChildcontrols), the designer (of your toplevel custom control) ... it hasn't a designer associated like the top ... render the control manually. ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: Literal control rendering empty
    ... I'm rendering each control manually because, ... validators as child controls, and it overrides the Render method to ... HtmlTextWriter object in the Render method to write out the extra ...
    (microsoft.public.dotnet.general)