How to Databind ASP.NET GridView in Custom Module
Hi,
I'm trying to databind an ASP.NET GridView(not RadGrid) control inside the view class of my custom module.
In the template file BackendOverviewTemplate.ascx file I have a simple GridView:
01.<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"02. CellPadding="4" ForeColor="#333333" GridLines="None">03. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />04. <Columns>05. <asp:TemplateField HeaderText="First Name">06. <ItemTemplate>07. <%# Eval("FirstName") %>08. </ItemTemplate>09. </asp:TemplateField>10. <asp:TemplateField HeaderText="Last Name">11. <ItemTemplate>12. <%# Eval("LastName") %>13. </ItemTemplate>14. </asp:TemplateField>15. </Columns>16. <EditRowStyle BackColor="#999999" />17. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />18. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />19. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />20. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />21. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />22. <SortedAscendingCellStyle BackColor="#E9E7E2" />23. <SortedAscendingHeaderStyle BackColor="#506C8C" />24. <SortedDescendingCellStyle BackColor="#FFFDF8" />25. <SortedDescendingHeaderStyle BackColor="#6F8DAE" />26.</asp:GridView>01.using System;02.using System.Collections.Generic;03.using System.Linq;04.using System.Text;05.using Telerik.Sitefinity.Modules.Pages.Web.UI;06.using Telerik.Sitefinity.Web.UI;07.using Telerik.Web.UI;08.using Telerik.Sitefinity.Libraries.Model;09.using Telerik.Sitefinity.Model;10.using System.Web.UI.WebControls;11. 12. 13.namespace Jobs.PublicControls14.15. [RequireScriptManager]16. public class BackendOverviewControl : SimpleView17. 18. 19. protected override string LayoutTemplateName20. 21. 22. get23. 24. return this.layoutTemplateName;25. 26. 27. 28. protected virtual GridView JobsGrid29. 30. get31. 32. return base.Container.GetControl<GridView>("GridView1", true);33. 34. 35. 36. protected override void InitializeControls(GenericContainer controlContainer)37. 38. JobsGrid.DataBinding += new EventHandler(JobsGrid_DataBinding);39. 40. 41. void JobsGrid_DataBinding(object sender, EventArgs e)42. 43. var module = new Jobs_Module();44. JobsGrid.DataSource = module.getDocuments();45. JobsGrid.DataBind();46. 47. 48. private string layoutTemplateName = "Jobs.Resources.Views.BackendOverviewTemplate.ascx";49. 50.Hello Raihan,
Move the following lines inside InitilalizeControls
var module = new Jobs_Module();
JobsGrid.DataSource = module.getDocuments();
JobsGrid.DataBind()
Kind regards,
Ivan Dimitrov
the Telerik team
Hi Ivan,
I moved the databinding code inside InitializeControls but now I get exception when I browse the module page from the Content menu.
Hi Raihan,
Override OnPreRender of the SimpleView and there bind the grid
var module = new Jobs_Module();
JobsGrid.DataSource = module.getDocuments();
JobsGrid.DataBind();
Best wishes,
Ivan Dimitrov
the Telerik team
Hi Ivan,
I've been running into the same problem (with a RadRotator as container control). To move the call to DataBind() into the OnPreRender override solved the problem mentioned here. BUT: now I'm not able to bind the SelectedIndexChanged event to a method wizthin the codebehind class. It simply doesnt fire at all although the postback occurs.
How do I have o cope with controls that have to raise a postback within a container hierarchy on a control derived from SimpleView?
Thanks, alf
Hello,
Could you tell us which control you use with SelectedIndexChanged ?
Greetings,
Ivan Dimitrov
the Telerik team