How to Databind ASP.NET GridView in Custom Module

Posted by Community Admin on 03-Aug-2018 09:06

How to Databind ASP.NET GridView in Custom Module

All Replies

Posted by Community Admin on 13-Apr-2011 00:00

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>


This is what my BackendOverviewControl class looks like:

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.PublicControls
14.
15.    [RequireScriptManager]
16.    public class BackendOverviewControl : SimpleView
17.    
18. 
19.        protected override string LayoutTemplateName
20.        
21. 
22.            get
23.            
24.                return this.layoutTemplateName;
25.            
26.        
27. 
28.        protected virtual GridView JobsGrid
29.        
30.            get
31.            
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.

Thanks

Posted by Community Admin on 13-Apr-2011 00:00

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


Posted by Community Admin on 13-Apr-2011 00:00

Hi Ivan,

I moved the databinding code inside InitializeControls but now I get exception when I browse the module page from the Content menu.

The container must be added to the Page controls collection prior to data binding


I have attached the screenshot.

Posted by Community Admin on 13-Apr-2011 00:00

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


Posted by Community Admin on 21-Jan-2012 00:00

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

Posted by Community Admin on 26-Jan-2012 00:00

Hello,

Could you tell us which control you use with SelectedIndexChanged ?

Greetings,
Ivan Dimitrov
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed