#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Help With Custom Server Controls


    Hi its my first time in this site and i hope you can help me with my problem. Stuck with this problem for the entire month. The requirement for this module is to create a dynamic table that host dynamically created placeholders. The placeholders should be able to accept controls made in the main page and persist on post back...here's my code.


    Dynamic Table Server Control
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace SFAHPOI
    {
        [ToolboxData("<{0}:NpaxDynamicTable runat=server></{0}:NpaxDynamicTable>")]
        public class NpaxDynamicTable : CompositeControl
        {
            // Declare subcontrols
            private UpdatePanel[] UP;
    
            public UpdatePanel[] uP
            {
                get { return UP; }
                set { UP = value; }
            }
    
            private NpaxPlaceHolder[] PlaceHolder;
    
            public NpaxPlaceHolder[] placeHolder
            {
                get { return PlaceHolder; }
                set { PlaceHolder = value; }
            }
          
            public void CreatePlaceHolderArray(int numberofcells)
            {
                UP = new UpdatePanel[numberofcells];
                PlaceHolder = new NpaxPlaceHolder[numberofcells];
    
                for (int ctr = 0; ctr < numberofcells; ctr++)
                {
                    //UP[ctr] = new UpdatePanel();
                    
                    PlaceHolder[ctr] = new NpaxPlaceHolder();
                    PlaceHolder[ctr].EnableViewState = true;
                    PlaceHolder[ctr].ID = "NpaxPlaceHolder_"+ctr.ToString();
                    //UP[ctr].ContentTemplateContainer.Controls.Add(PlaceHolder[ctr]);
                    
                }
            }
            public void AddItemtoPlaceHolder(int cell,Control C)
            {
                EnsureChildControls();
                PlaceHolder[cell].Controls.Clear();
                C.ID = cell.ToString();
                PlaceHolder[cell].Controls.Add(C);
            }
            private int[] Row
            {
                get
                {
                    if (ViewState["Row"] == null)
                    {
                        return new int[0];
                    }
                    else
                    {
                        return (int[])ViewState["Row"];
                    }
                }
    
                set
                {
                    ViewState["Row"] = value;
                }
            }
            public int[] row
            {
                get { return Row; }
                set { Row = value; }
            }
        
            public override ControlCollection Controls
            {
                get
                {
                    EnsureChildControls();
                    return base.Controls;
                }
            }
            protected override void OnInit(EventArgs e)
            {
                
                base.OnInit(e);
            }
            
            
            
            int LCD = 1;
            int colspan = 0;
            int numberofcells = 0;
            int placeholderidx = 0;
    
            protected override void CreateChildControls()
            {
                if (ChildControlsCreated)
                    return;
                Controls.Clear();
                #region javascript
                string script = @"<script type='text/javascript'>
                                window.onload=function() {
                                if(document.documentElement.clientHeight>100)
                                {
                                document.getElementById('dynamictable').style.height =(document.documentElement.clientHeight -100)+'px';
                                }
                                setInterval('startTime()',1000);
                                }
                                window.onresize = function() {
                                if(document.documentElement.clientHeight>100)
                                {
                                document.getElementById('dynamictable').style.height =(document.documentElement.clientHeight -100)+'px';
                                }
                                  }
                                </script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "js", script, false);
                #endregion
                // Set subcontrols behavior
                for (int ctr = 0; ctr < Row.Length; ctr++)
                {
                    numberofcells = numberofcells + Row[ctr];
                    LCD = LCD * Row[ctr];
    
                }
                CreatePlaceHolderArray(numberofcells);
                
                Controls.Add(new LiteralControl("<table id='dynamictable' border='1' width='100%' height='100%'>"));
                for (int ctr = 0; ctr < Row.Length; ctr++)
                {
                    colspan = LCD / Row[ctr];
                    Controls.Add(new LiteralControl("<tr>"));
                    for (int numberofcols = 0; numberofcols <= Row[ctr] - 1; numberofcols++)
                    {
    
                        Controls.Add(new LiteralControl("<td colspan=" + colspan + ">"));
                        PlaceHolder[placeholderidx].Controls.Add(new LiteralControl("&nbsp"));
                        Controls.Add((PlaceHolder[placeholderidx]));
                        Controls.Add(new LiteralControl("</td>"));
                        placeholderidx++;
                    }
                    Controls.Add(new LiteralControl("</tr>"));
                }
                Controls.Add(new LiteralControl("</table>"));
                
                base.CreateChildControls();
    
                ChildControlsCreated = true;
    
            }
    
    
    
        }
    }
    PlaceHolder Server Control
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace SFAHPOI
    {
        [ToolboxData("<{0}:NpaxPlaceHolder runat=server></{0}:NpaxPlaceHolder>")]
        public class NpaxPlaceHolder : PlaceHolder
        {
    
        }
    }
    Web Form Page
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="HPOI_SFA.WebForm4" %>
    
    <%@ Register Assembly="SFAHPOI" Namespace="SFAHPOI" TagPrefix="cc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link rel="Stylesheet" href="StyleSheet1.css" />
    </head>
    <body onload="startTime()">
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
        <cc1:NpaxHeaderControl ID="NpaxHeaderControl1" runat="server" ClientIDMode="AutoID" />
            <cc1:NpaxDropDown ID="NpaxDropDown1" runat="server" 
                onselectedindexchanged="NpaxDropDown1_SelectedIndexChanged" >
            </cc1:NpaxDropDown>
       
        <asp:DropDownList ID="DropDownList1" runat="server" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
            </asp:DropDownList>
        <cc1:NpaxDynamicTable ID="NpaxDynamicTable1" runat="server" />
            <cc1:NpaxButton ID="NpaxButton1" runat="server" onclick="NpaxButton1_Click2" /> 
            <cc1:NpaxButton ID="NpaxButton2" runat="server" onclick="NpaxButton2_Click2" />
        </ContentTemplate>
        </asp:UpdatePanel>
       </div>
     
    
        </form>
    </body>
    </html>
    Code Behind
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using SFAHPOI;
    using System.Data;
    using HpoiBL;
    using DevExpress.XtraCharts.Web;
    
    namespace HPOI_SFA
    {
        public partial class WebForm4 : System.Web.UI.Page
        {
            string[] usernames= new string[5];
            int s;
            ScreenMasterBL SM;
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
               
    
                if (!IsPostBack)
                {
    
                    SM = new ScreenMasterBL(1);
    
                    DropDownList1.DataSource = SM.GetScreens();
                    DropDownList1.DataValueField = "ScreenID";
                    DropDownList1.DataTextField = "ScreenName";
                    DropDownList1.DataBind();
                    NpaxDropDown1.DataSource = SM.GetScreens();
                    NpaxDropDown1.DataValueField = "ScreenID";
                    NpaxDropDown1.DataTextField = "ScreenName";
                    NpaxDropDown1.DataBind();
                    
                    DataTable dt = SM.GetScreen();
                    NpaxDynamicTable1.row = new int[dt.Rows.Count];
                    int ctr = 0;
                    foreach (int r in NpaxDynamicTable1.row)
                    {
                        NpaxDynamicTable1.row[ctr] = Convert.ToInt16(dt.Rows[ctr][2]);
                        ctr++;
                    }
                    //NpaxLogo sa = new NpaxLogo();
                    //NpaxHeaderControl1.logo = sa;
                    NpaxHeaderControl1.logoimgUrl = "~/Image/logo.png";
    
                    
                    //NpaxHeaderControl1.logo.ImageUrl = "~/Image/logo.png";
    
    
                   // NpaxHeaderControl1.CustomizeLogo("~/Image/logo.png",200,100);
    
                   
                }
                
               
                else
                {
                    
                }
                
            }
    
           
          
    
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
               
                SM = new ScreenMasterBL(Convert.ToInt16(DropDownList1.SelectedValue));
                DataTable dt = SM.GetScreen();
                NpaxDynamicTable1.row = new int[dt.Rows.Count];
                int ctr = 0;
                foreach (int r in NpaxDynamicTable1.row)
                {
                    NpaxDynamicTable1.row[ctr] = Convert.ToInt16(dt.Rows[ctr][2]);
                    ctr++;
                }
                #region javascript
                string script = @"<script type='text/javascript'>
                                if(document.documentElement.clientHeight>100)
                                {
                                document.getElementById('dynamictable').style.height =(document.documentElement.clientHeight -100)+'px';
                                }
                                </script>";
                ScriptManager.RegisterStartupScript(DropDownList1, this.GetType(), "js", script, false);
                #endregion
            }
    
            protected void NpaxButton1_Click2(object sender, EventArgs e)
            {
                Label s = new Label();
                s.ID = "sample";
                s.Text = "sample";
                NpaxDynamicTable1.AddItemtoPlaceHolder(1, s);
                NpaxButton2.Text = NpaxDynamicTable1.row.Length.ToString();
            }
    
            protected void NpaxDropDown1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            protected void NpaxButton2_Click2(object sender, EventArgs e)
            {
    
            }
    
            
            
            
        }
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0
    this works for the first time but controls on the placeholder disappear on postback

IMN logo majestic logo threadwatch logo seochat tools logo