Question : Generate paging on a custom gridview

I have a custom control that I created that extends a GridView, it's purpose is to handle creating a floating header (which should be a property of the control IMO) and custom paging on a GridView using LinkButtons.  I am able to generate the paging correctly but the LinkButtons are not showing up as links, the href property is not being set.  I've tried executing the code to generate the LinkButtons in the OnInit and OnLoad methods but it's not working.  I've attached the code below.  

It's my first attempt at creating a custom control so I'm sure there are things that I could be doing differently or more efficiently.

Any help on this would be appreciated.

Thanks in advance.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;

/// <summary>
/// This custom control will create a gridview with a floating header and custom paging to only return the number of rows per page.
/// </summary>
namespace Controls
{
    public class ScrollPageGridView : System.Web.UI.WebControls.GridView
    {
        #region Properties
        private Int32 pagesPerGrouping = 10;

        public String PagerBackColor { get; set; }
        public Unit GridHeight { get; set; }
        public Unit GridWidth { get; set; }
        public Int32 TotalRows { get; set; }
        public Int32 PagesPerGrouping
        {
            get
            {
                return pagesPerGrouping;
            }
            set
            {
                Int32 ret = 10;
                Int32.TryParse(value.ToString(), out ret);
                pagesPerGrouping = ret;
            }
        }
        #endregion

        public event EventHandler<EventArgs> GridViewPageChangedCustom;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            HtmlTableCell th = GeneratePaging("th");
            
        }

        #region ScrollPageGridView()
        public ScrollPageGridView() { }
        #endregion
        
        #region CalculateWidth()
        private String CalculateWidth()
        {
            string strWidth = "auto";
            if (!this.GridWidth.IsEmpty)
            {
                strWidth = String.Format("{0}{1}", this.GridWidth.Value, ((this.GridWidth.Type == UnitType.Percentage) ? "%" : "px"));
            }
            return strWidth;
        }
        #endregion
        
        #region CalculateHeight()
        private String CalculateHeight()
        {
            string strHeight = "200px";
            if (!this.GridHeight.IsEmpty)
            {
                strHeight = String.Format("{0}{1}", this.GridHeight.Value, ((this.GridHeight.Type == UnitType.Percentage) ? "%" : "px"));
            }
            return strHeight;
        }
        #endregion

        #region Render
        protected override void Render(HtmlTextWriter writer)
        {
            // Render header row.
            if (this.HeaderRow != null)
            {
                GridViewRow customHeader = this.HeaderRow;

                writer.Write("<table border=\"1\" rules=\"all\" cellspacing=\"" + this.CellSpacing.ToString() + "\" cellpadding=\"" + this.CellPadding.ToString() + "\" style=\"border-collapse:collapse;\">");

                customHeader.ApplyStyle(this.HeaderStyle);
                if (AutoGenerateColumns == false)
                {
                    // Remove the existing cells from the Header.
                    for (int j = customHeader.Cells.Count - 1; j >= 0; j--)
                    {
                        if (this.Columns[j].Visible == false)
                            customHeader.Cells.RemoveAt(j);
                    }

                    Int32 i = 0;
                    foreach (DataControlField col in this.Columns)
                    {

                        if (col.Visible == true)
                        {
                            customHeader.Cells[i].ApplyStyle(col.HeaderStyle);
                            i++;
                        }
                    }
                }
                customHeader.RenderControl(writer);
                writer.Write("</table>");
                
                this.HeaderRow.Visible = false;
            }

            // Render data rows.

            // Start the Div tag for the scrolling gridview.
            writer.Write("<div id=\"div" + this.ID + "\" style=\"height:" + CalculateHeight() + ";overflow:auto;width:" + CalculateWidth() + ";\">");
            
            // Get the pager row and make invisible.
            GridViewRow customPager = this.BottomPagerRow;
            if (this.BottomPagerRow != null)
            {
                this.BottomPagerRow.Visible = false;
            }

            base.Render(writer);
            writer.Write("</div>");

            // Render pager row.
            if (customPager != null)
            {
                writer.Write("<table  border='0' cellspacing='" + this.CellSpacing.ToString() + "' cellpadding='" + this.CellPadding.ToString() + "' style='width:" + customPager.Width.ToString() + "'>");
                customPager.ApplyStyle(this.PagerStyle);
                customPager.Visible = true;
                customPager.RenderControl(writer);
                writer.Write("</table>");

                writer.Write("<table border=\"1\" rules=\"all\" cellspacing=\"" + this.CellSpacing.ToString() + "\" cellpadding=\"" + this.CellPadding.ToString() + "\" style=\"border-collapse:collapse;\">");
                writer.Write("<tr style=\"background-color:" + this.PagerBackColor + "\">");
                HtmlTableCell th = GeneratePaging("th");
                th.RenderControl(writer);
                writer.Write("</tr>");
                writer.Write("</table>");
            }
        }
        #endregion

        #region GeneratePaging()
        /// <summary>
        ///  Generate the page numbers on the grid.
        /// </summary>
        private HtmlTableCell GeneratePaging(String TableCellType)
        {
            try
            {
                HtmlTableCell th = new HtmlTableCell(TableCellType);
                th.Attributes.Add("scope", "col");
                th.Attributes.Add("style", "width:400px;text-align:left;");

                //int pagesPerGrouping = PagesPerGrouping;
                int fontSize = 10;
                int totRows = 0;
                int numPages = 1;
                int gridActSelectedPage = 1;
                int curPageGroup = 1;
                int startPageForGroup;
                int endPageForGroup;
                string litSep = "&nbsp;";

                // Get the total number of rows that were returned from the DB.
                totRows = this.TotalRows;

                // Get the current page index.
                if (this.ViewState["gv" + this.ID + "PageIndex"] != null)
                    gridActSelectedPage = Convert.ToInt32(this.ViewState["gv" + this.ID + "PageIndex"]) + 1; // Actual page index, page index is 0 based;

                // Clear out any existing controls.
                th.Controls.Clear();

                // Based on the number of rows in the DataView and the number of items per page in the grid figure out the number of pages needed.
                numPages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totRows) / Convert.ToDouble(this.PageSize)));

                // Divide the current page by the number of pages per grouping to find out which grouping we're in.
                // Subtract 1 from the current page grouping, multiply by the pages per grouping and then add 1 to get the starting page for the group.
                curPageGroup = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(gridActSelectedPage) / Convert.ToDecimal(pagesPerGrouping)));
                startPageForGroup = ((curPageGroup - 1) * pagesPerGrouping) + 1;

                // Get the last page in the page group.
                endPageForGroup = (startPageForGroup + pagesPerGrouping - 1 < numPages ? startPageForGroup + pagesPerGrouping - 1 : numPages);

                // Add page nav buttons when there are more pages then what can show on the screen.
                if (startPageForGroup != 1)
                {
                    LinkButton lnk = new LinkButton();
                    lnk.ID = "lbl" + this.ID + "Pg" + (startPageForGroup - 1).ToString();
                    lnk.Text = "<<";
                    lnk.CommandArgument = (startPageForGroup - 1).ToString();
                    lnk.Font.Size = FontUnit.Point(fontSize);
                    lnk.Click += new EventHandler(gvPage_Click);
                    th.Controls.Add(lnk);

                    // Add a separator between page numbers.
                    Literal litSeparator = new Literal();
                    litSeparator.Text = litSep;
                    litSeparator.ID = "litSeparatorLT";
                    th.Controls.Add(litSeparator);
                }

                // Loop through the active page set and display the numbers across the footer.
                for (int i = startPageForGroup; i <= endPageForGroup; i++)
                {
                    // For the active page disable the link.
                    LinkButton lnk = new LinkButton();
                    lnk.ID = "lbl" + this.ID + "Pg" + i.ToString();
                    lnk.Text = i.ToString();
                    lnk.CommandArgument = i.ToString();
                    lnk.Font.Size = FontUnit.Point(fontSize);
                    if (i == gridActSelectedPage)
                    {
                        lnk.Enabled = false;
                    }
                    else
                    {
                        lnk.Click += new EventHandler(gvPage_Click);
                        lnk.Command += new CommandEventHandler(gvPage_Command);
                    }
                    th.Controls.Add(lnk);


                    // Add a separator between page numbers.
                    Literal litSeparator = new Literal();
                    litSeparator.Text = litSep;
                    litSeparator.ID = "litSeparator" + i.ToString();
                    th.Controls.Add(litSeparator);
                }

                // If the number of total pages is more than the grouping then put in >> for navigation.
                if (numPages > endPageForGroup)
                {
                    LinkButton lnk = new LinkButton();
                    lnk.ID = "lbl" + this.ID + "Pg" + (endPageForGroup + 1).ToString();
                    lnk.Text = ">>";
                    lnk.CommandArgument = (endPageForGroup + 1).ToString();
                    lnk.Font.Size = FontUnit.Point(fontSize);
                    lnk.Click += new EventHandler(gvPage_Click);
                    th.Controls.Add(lnk);
                }

                return th;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region gvPage_Click
        private void gvPage_Click(object sender, EventArgs e)
        {
            if (GridViewPageChangedCustom != null)
            {
                GridViewPageChangedCustom(this, new EventArgs());
            }
        }
        #endregion
    }
}

Answer : Generate paging on a custom gridview

Before adding the volume you were able to LM the VM between the nodes?

can you test this and tell me the results.
1. Shutdown the VM on node where is it running, let me call it NodeA and try to live migrate it to NodeB and see if it succeeds. If it does try bringing it up on NodeB, see it succeeds.
2.Try to quick migrate the VM from NodeA to NodeB and see if it succeeds.

 
Random Solutions  
 
programming4us programming4us