Adding a GridView and DataSource inside of a Web Custom Control

Last post 02-27-2008 3:12 PM by biznick. 6 replies.

Sort Posts:

  • Adding a GridView and DataSource inside of a Web Custom Control

    02-25-2008, 4:24 PM
    • Member
      149 point Member
    • biznick
    • Member since 02-15-2007, 4:19 PM
    • Columbia MO
    • Posts 47

    I am working on a reusable person search control right now, and I am having trouble figuring out how to add my gridview and datasource through my control.

    Below is my RenderContents method.  Basically I have two issues.  The first, is with a grid view, even after doing RenderControl(), the grid view never appears on my page, or any html to represent it for that matter.  The test label however, does render html.  The second is with the CSLADataSource (which just inherits DataSourceControl).  How do you do a RenderControl() for DataSourceControls (that method does not exist for this).

     If anyone could help point me in the right direction to having a datasource and gridview be rendered in a web custom control, I'd appreciate it.  Thank you. 

    protected override void RenderContents(HtmlTextWriter output)
    {
        
    CslaDataSource odsPersons = new CslaDataSource();
         odsPersons.ID =
    "odsPersons_" + UniqueID;
         odsPersons.TypeAssemblyName =
    "PersonLookup";
         odsPersons.TypeName =
    "PersonLookup.PersonList";
         odsPersons.TypeSupportsPaging =
    true;
         odsPersons.TypeSupportsSorting =
    true;
         odsPersons.SelectObject +=
    new EventHandler<SelectObjectArgs>(odsPersons_OnSelectObject);

         GridView gvPersons = new GridView();
         gvPersons.DataSourceID =
    "odsPersons_" + UniqueID;
         gvPersons.AutoGenerateColumns =
    true;
         gvPersons.AllowPaging =
    true;
         gvPersons.PageSize = 10;
         gvPersons.EmptyDataText =
    "Boo, no results!";
         gvPersons.GridLines =
    GridLines.Both;

         Label test = new Label();
         test.Text =
    "<strong><span style='color:White;'>Testing Grid View #5</span></strong><br /><br />";
         test.RenderControl(output);

         gvPersons.RenderControl(output);
    }

    Please mark the post(s) that helped you as the answer. You get points, the poster gets points, and also the answer is then there to show anyone else how your problem was solved.
  • Re: Adding a GridView and DataSource inside of a Web Custom Control

    02-25-2008, 4:36 PM
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 4:00 PM
    • Kentucky
    • Posts 772

    you have to add all of the objects to the control tree during the OnInit event.

  • Re: Adding a GridView and DataSource inside of a Web Custom Control

    02-25-2008, 4:53 PM
    • Member
      149 point Member
    • biznick
    • Member since 02-15-2007, 4:19 PM
    • Columbia MO
    • Posts 47

    I understand that dynamic controls get created at the OnInit level, but I'm a bit confused.

    A web custom control does have the

    protected override void OnInit(EventArgs e)
    {
    }

    How would I take advantage of this to add my datasource and gridview?  Do you have a quick example you could show me.  Also, I'm not sure why my Label would render but my gridview would not.  Shouldn't the label need to be rendered at the OnInit level as well?

     Thank you for your help Smile

    Please mark the post(s) that helped you as the answer. You get points, the poster gets points, and also the answer is then there to show anyone else how your problem was solved.
  • Re: Adding a GridView and DataSource inside of a Web Custom Control

    02-25-2008, 7:03 PM
    Answer
    • Contributor
      4,008 point Contributor
    • Matt-dot-net
    • Member since 06-09-2007, 4:00 PM
    • Kentucky
    • Posts 772

    The GridView is not getting databound because you are not creating it at the correct time in the page lifecycle.  If you called GridView.DataBind() it might get populated with data and have something to render.  The label, doesn't need any databinding.

  • Re: Adding a GridView and DataSource inside of a Web Custom Control

    02-26-2008, 10:58 AM
    • Member
      149 point Member
    • biznick
    • Member since 02-15-2007, 4:19 PM
    • Columbia MO
    • Posts 47

    Alright, thanks to your help I'm getting very very close Smile

    Basically what I did was create four class fields

    private GridView gvPersons;
    private CslaDataSource odsPersons;
    private Panel pPersons;
    private Label lTesting;

    And then added the following code in my OnInit

    private void InitializeControls()
    {
        
    pPersons =
    new Panel();

         odsPersons =
    new CslaDataSource();
         odsPersons.ID =
    "odsPersons_" + UniqueID;
         odsPersons.TypeAssemblyName =
    "PersonSearch";
         odsPersons.TypeName =
    "PersonSearch.PersonList";
         odsPersons.TypeSupportsPaging =
    true;
         odsPersons.TypeSupportsSorting =
    true;
         odsPersons.SelectObject +=
    new EventHandler<SelectObjectArgs>(odsPersons_OnSelectObject);
     

         pPersons.Controls.Add(odsPersons);

         gvPersons =
    new GridView();
         gvPersons.ID =
    "gvPersons_" + UniqueID;
         gvPersons.DataSourceID = odsPersons.ID;
         gvPersons.AutoGenerateColumns =
    true;
         gvPersons.AllowPaging =
    true;
         gvPersons.PageSize = 10;
         gvPersons.EmptyDataText =
    "Boo, no results!";
         gvPersons.GridLines =
    GridLines.Both;

         pPersons.Controls.Add(gvPersons);
        
         lTesting =
    new Label();
         lTesting.Text =
    "<strong><span style='color:White;'>Testing Grid View #12</span></strong><br /><br />";

         pPersons.Controls.Add(lTesting);
        
        
         this
    .Controls.Add(pPersons);

        gvPersons.DataBind();
    }

    Finally I removed my old render code and replaced it with the following

    protected override void RenderContents(HtmlTextWriter output)
    {
         pPersons.RenderControl(output);
    }

    Two things..  One is that I had to add my controls to the WebCotrol first doing a this.controls.add.  This gave my gridview a naming container which it required to do a databind.  After I did that, thanks to your suggestion of moving the content to the onInit, my grid properly displayed when I loaded the page.

     The only thing I don't quite understand at this point is that when I drag my web custom control to my form editor, I will see my "Testing Grid View #12" label, but the grid will not show up in design view.  The grid only displays once I actually run the webpage.  Do you have any idea why this might happen, and is this normal or am I still missing something?

     Thank you so much for your assistance.  Your tip was very helpful Big Smile 

    Please mark the post(s) that helped you as the answer. You get points, the poster gets points, and also the answer is then there to show anyone else how your problem was solved.
  • Re: Adding a GridView and DataSource inside of a Web Custom Control

    02-27-2008, 1:14 AM
    Answer

    Hi biznick ,

    biznick:
    The only thing I don't quite understand at this point is that when I drag my web custom control to my form editor, I will see my "Testing Grid View #12" label, but the grid will not show up in design view. 

    I am not sure this can make the error correct. But I have some suggestions.

    Based on my understanding, you are creating one composite control.  I always implement composite control by extend WebControl and implement INamingContainer interface.

    And I always create child controls by writing code in CreateChildControls method which override the CreateChildControls method of Class Control instead of writing code in OnInit event.

    Please try it again.

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Big Smile [:D] Re: Adding a GridView and DataSource inside of a Web Custom Control

    02-27-2008, 3:12 PM
    • Member
      149 point Member
    • biznick
    • Member since 02-15-2007, 4:19 PM
    • Columbia MO
    • Posts 47
    Thank you for the advice Samu Zhang! I found that I had to inherit from CompositeControl and not WebControl for it to be viewed in design mode, but this also allowed me to no longer override the RenderContents method. Very helpful advice you gave me. This forum really rocks. I always find the most helpful information here. I'll try and give back some more too, by posting on some other peoples questions. Thanks again for the help! :)
    Please mark the post(s) that helped you as the answer. You get points, the poster gets points, and also the answer is then there to show anyone else how your problem was solved.
Page 1 of 1 (7 items)