none
Event handler for dynamically created linkbuttons in custom webpart

    Soru

  • Hi All,

    I have created dynamic linkbuttons in custom webpart. All the buttons are getting displayed as expected. Problem here is that i'm trying to execute same event based on the commandname property of the button.

    When i click on the button it all the linkbutton objects gets initialized once again and all the properties are getting reset.

    Below is the code i have written so far:

    public class DynamicQuarter : System.Web.UI.WebControls.WebParts.WebPart 
        {       
             
                string siteUrl = string.Empty; 
                string rootUrl = string.Empty; 
                string siteName = string.Empty; 
     
                private bool _error = false; 
     
     
                Label lblyr1 = new Label(); 
                Label lblyr2 = new Label(); 
                Label lblyr3 = new Label(); 
                Label lblyr4 = new Label(); 
     
                 
     
                DataTable dt = new DataTable(); 
                LinkButton lnkDynamic; 
     
             
            protected override void CreateChildControls() 
            { 
                dt = getdt(); 
      
                bool hascurrentyr = false; 
     
                for (int i = 0; i < dt.Rows.Count-1; i++) 
                { 
                    if (dt.Rows[i]["year"].Equals(System.DateTime.Now.Year.ToString())) 
                    { 
                        hascurrentyr = true; 
                        break; 
                    } 
                } 
     
                if (hascurrentyr == false) 
                { 
                    lblyr1.Text = DateTime.Now.Date.AddYears(-3).Year.ToString(); 
                    Controls.Add(lblyr1); 
                } 
                
                
                string beforeSubmitJS = "/nvar exportRequested = false; /n"; 
                beforeSubmitJS += "var beforeFormSubmitFunction = theForm.onsubmit;/n"; 
                beforeSubmitJS += "theForm.onsubmit = function(){ /n"; 
                beforeSubmitJS += "var returnVal = beforeFormSubmitFunction(); /n"; 
                beforeSubmitJS += "if(exportRequested && returnVal) {_spFormOnSubmitCalled=false; exportRequested=false;} /n"; 
                beforeSubmitJS += "return returnVal; /n"; 
                beforeSubmitJS += "}; /n"; 
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true); 
     
                
     
                lblyr2.Text = DateTime.Now.Date.AddYears(-2).Year.ToString(); 
                Controls.Add(lblyr2); 
     
               
                 
     
                lblyr3.Text = DateTime.Now.Date.AddYears(-1).Year.ToString(); 
                Controls.Add(lblyr3); 
     
                
     
                if (hascurrentyr == true) 
                { 
                    lblyr4.Text = DateTime.Now.Date.Year.ToString(); 
                    Controls.Add(lblyr4); 
                } 
                 
               
                 
                 
              
            } 
     
            protected void lnkDynamic_Command(object sender, EventArgs e) 
            { 
               
                
     
            } 
     
            
     
            public DataTable getdt() 
            { 
                // 
                //Code to find GUID of the list Item 
                SPSite site = SPContext.Current.Site; 
                SPWeb web = site.OpenWeb(); 
                SPList objlist = web.Lists["DownloadContents"]; 
     
                string guid = string.Empty; 
                foreach (SPListItem item in objlist.Items) 
                { 
                    if (item.Title == "Index of Customers for 3 years") 
                    { 
                        guid = item.UniqueId.ToString(); 
                    } 
                } 
     
                // 
     
                //creating datatable to enter 3 yeards data with 4 quarters and querystring 
                DataTable dt = new DataTable(); 
                DataRow dr; 
     
                dt.Columns.Add("year", typeof(string)); 
                dt.Columns.Add("quarter", typeof(string));             
                dt.Columns.Add("querystring", typeof(string)); 
                           
     
                //code to find current years current quarter 
                string quarter = string.Empty; 
     
                if (System.DateTime.Now.Month.Equals(1)||System.DateTime.Now.Month.Equals(2)||System.DateTime.Now.Month.Equals(3)) 
                { 
                    quarter = "Q1"; 
                } 
                else if (System.DateTime.Now.Month.Equals(4) || System.DateTime.Now.Month.Equals(5) || System.DateTime.Now.Month.Equals(6)) 
                { 
                    quarter = "Q2"; 
                } 
                else if (System.DateTime.Now.Month.Equals(7)||System.DateTime.Now.Month.Equals(8)||System.DateTime.Now.Month.Equals(9)) 
                { 
                    quarter = "Q3"; 
                } 
                else if (System.DateTime.Now.Month.Equals(10) || System.DateTime.Now.Month.Equals(11) || System.DateTime.Now.Month.Equals(12)) 
                { 
                    quarter = "Q4"; 
                } 
     
                //code to enter current years data based on current quarter 
                if (quarter.Contains("Q1")) 
                { 
                    //if current quarter is Q1 means 12 quarters are already appearing on webpart 
                     
                } 
                else if (quarter.Contains("Q2")) 
                { 
                    //if current quarter is Q2 that means need to enter 1st quarter of the current year and remove Q1 of the (year-3) 
     
                    dr = dt.NewRow(); 
                    dr["year"] = System.DateTime.Now.Year.ToString(); 
                    dr["quarter"] = "Q1"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q1"; 
     
                    dt.Rows.Add(dr); 
                } 
                else if (quarter.Contains("Q3")) 
                { 
                    //if current quarter is Q3 that means need to enter 1st & 2nd quarter of the current year and remove Q1,Q2 of the (year-3) 
                    dr = dt.NewRow(); 
                    dr["year"] = System.DateTime.Now.Year.ToString(); 
                    dr["quarter"] = "Q1"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q1"; 
     
                    dt.Rows.Add(dr); 
                    dr = dt.NewRow(); 
     
                    dr["year"] = System.DateTime.Now.Year.ToString(); 
                    dr["quarter"] = "Q2"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q2"; 
     
                    dt.Rows.Add(dr); 
                    
                } 
                else if (quarter.Contains("Q4")) 
                { 
                    //if current quarter is Q4 that means need to enter 1st,2nd & 3rd quarter of the current year and remove Q1,Q2 & Q3 of the (year-3) 
                    dr = dt.NewRow(); 
                    dr["year"] = System.DateTime.Now.Year.ToString(); 
                    dr["quarter"] = "Q1"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q1"; 
     
                    dt.Rows.Add(dr); 
                    dr = dt.NewRow(); 
     
                    dr["year"] = System.DateTime.Now.Year.ToString(); 
                    dr["quarter"] = "Q2"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q2"; 
     
                    dt.Rows.Add(dr); 
                    dr = dt.NewRow(); 
     
                    dr["year"] = System.DateTime.Now.Year.ToString(); 
                    dr["quarter"] = "Q3"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q3"; 
     
                    dt.Rows.Add(dr); 
                     
                } 
     
                // 
                //Code to enter previous years(current year - 1 ) data in datatable 
                for (int j = 1; j <= 4; j++) 
                { 
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 1); 
                    dr["quarter"] = "Q" + j.ToString(); 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 1) + "Q" + j.ToString(); 
     
                    dt.Rows.Add(dr); 
                } 
                // 
     
                // 
                //code to check how many quarter(s) of the current year are entered in Datatable 
                string sNoOfQuartersinCurrentYear = string.Empty; 
                for (int i = 0; i < dt.Rows.Count - 1; i++) 
                { 
                    if (dt.Rows[i]["year"].Equals(System.DateTime.Now.Year.ToString())) 
                    { 
                        sNoOfQuartersinCurrentYear += "," +  dt.Rows[i]["quarter"].ToString(); 
                    } 
                } 
                // 
     
                // 
                //Code to enter previous 2 years(current year - 2 ) data in datatable 
                if (sNoOfQuartersinCurrentYear.Contains("Q4")) 
                { 
                    return dt; 
                } 
                else if (sNoOfQuartersinCurrentYear.Contains("Q3")) 
                { 
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                    dr["quarter"] = "Q4"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q4"; 
     
                    dt.Rows.Add(dr); 
     
                    return dt; 
     
                } 
                else if (sNoOfQuartersinCurrentYear.Contains("Q2")) 
                { 
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                    dr["quarter"] = "Q3"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q3"; 
     
                    dt.Rows.Add(dr); 
     
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                    dr["quarter"] = "Q4"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q4"; 
     
                    dt.Rows.Add(dr); 
     
                    return dt; 
     
                } 
                else if (sNoOfQuartersinCurrentYear.Contains("Q1")) 
                { 
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                    dr["quarter"] = "Q2"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q2"; 
     
                    dt.Rows.Add(dr); 
     
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                    dr["quarter"] = "Q3"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q3"; 
     
                    dt.Rows.Add(dr); 
     
                    dr = dt.NewRow(); 
     
                    dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                    dr["quarter"] = "Q4"; 
                    dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q4"; 
     
                    dt.Rows.Add(dr); 
     
                    return dt; 
     
                } 
                else if (string.IsNullOrEmpty(sNoOfQuartersinCurrentYear)) 
                { 
                     
                    for (int j = 1; j <= 4; j++) 
                    { 
                        dr = dt.NewRow(); 
     
                        dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); 
                        dr["quarter"] = "Q" + j.ToString(); 
                        dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q" + j.ToString(); 
     
                        dt.Rows.Add(dr); 
                    } 
     
                    for (int j = 1; j <= 4; j++) 
                    { 
                        dr = dt.NewRow(); 
     
                        dr["year"] = Convert.ToString(System.DateTime.Now.Year - 3); 
                        dr["quarter"] = "Q" + j.ToString(); 
                        dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 3) + "Q" + j.ToString(); 
     
                        dt.Rows.Add(dr); 
                    } 
     
                    return dt; 
                } 
                 
     
                return dt; 
     
                
            } 
     
            public override void RenderControl(HtmlTextWriter writer) 
            { 
                lblyr1.RenderControl(writer); 
                writer.WriteBreak(); 
     
     
                string expression = "year='" + lblyr1.Text + "'"; 
                int i = 1; 
                // 
     
                foreach (DataRow row in dt.Select(expression)) 
                { 
                    lnkDynamic.ID = "lnkDynamic_" + i; 
                    lnkDynamic.Text = row["quarter"].ToString();  
                    lnkDynamic.CommandName = row["year"].ToString(); 
                    lnkDynamic.EnableViewState = true; 
                    lnkDynamic.RenderControl(writer); 
                    writer.Write("&nbsp;"); 
                    i += 1; 
                } 
     
                i = 1; 
                writer.WriteBreak(); 
                writer.WriteBreak(); 
     
                lblyr2.RenderControl(writer); 
                writer.WriteBreak(); 
     
                expression = "year='" + lblyr2.Text + "'"; 
                foreach (DataRow row in dt.Select(expression)) 
                { 
     
                    lnkDynamic.ID = "lnkDynamic_" + i; 
                    lnkDynamic.Text = row["quarter"].ToString();  
                    lnkDynamic.CommandName = row["year"].ToString(); 
                    lnkDynamic.EnableViewState = true; 
                    lnkDynamic.RenderControl(writer); 
                    writer.Write("&nbsp;"); 
                    i += 1; 
                } 
     
                i = 1; 
                writer.WriteBreak(); 
                writer.WriteBreak(); 
     
                lblyr3.RenderControl(writer); 
                writer.WriteBreak(); 
     
                expression = "year='" + lblyr3.Text + "'"; 
                foreach (DataRow row in dt.Select(expression)) 
                { 
                    lnkDynamic.ID = "lnkDynamic_" + i; 
                    lnkDynamic.Text = row["quarter"].ToString();  
                    lnkDynamic.CommandName = row["year"].ToString(); 
                    lnkDynamic.EnableViewState = true; 
                    lnkDynamic.RenderControl(writer); 
                    writer.Write("&nbsp;"); 
                    i += 1; 
                } 
                i = 1; 
                writer.WriteBreak(); 
                writer.WriteBreak(); 
                 
                lblyr4.RenderControl(writer); 
                writer.WriteBreak(); 
                expression = "year='" + lblyr4.Text + "'"; 
                foreach (DataRow row in dt.Select(expression)) 
                { 
                    lnkDynamic.ID = "lnkDynamic_" + i; 
                    lnkDynamic.Text = row["quarter"].ToString();  
                    lnkDynamic.CommandName = row["year"].ToString(); 
                    lnkDynamic.EnableViewState = true; 
                    lnkDynamic.RenderControl(writer); 
                    writer.Write("&nbsp;"); 
                    i += 1; 
                } 
               
     
            } 
     
            private void HandleException(Exception ex) 
            { 
                this._error = true; 
                this.Controls.Clear(); 
                this.Controls.Add(new LiteralControl(ex.Message)); 
            } 
     
            protected override void OnLoad(EventArgs e) 
            { 
                if (!_error) 
                { 
                    try 
                    { 
                        base.OnLoad(e); 
                       
                        
                    } 
                    catch (Exception ex) 
                    { 
                        HandleException(ex); 
                    } 
                } 
            } 
             
            protected override void LoadViewState(object savedState) 
            { 
                base.LoadViewState(savedState); 
                EnsureChildControls(); 
            } 
     
            protected override object SaveViewState() 
            { 
                return new Pair(base.SaveViewState(), null); 
            } 
     
            protected override void OnInit(EventArgs e) 
            { 
                dt = getdt(); 
                for (int i = dt.Rows.Count - 1; i >= 0; i--) 
                { 
                    lnkDynamic = new LinkButton(); 
                    lnkDynamic.EnableViewState = true; 
                    lnkDynamic.ID = "lnkDynamic_" + i; 
                    lnkDynamic.Text = "Q" + i.ToString(); 
                    lnkDynamic.Click += new EventHandler(lnkDynamic_Command); 
                    lnkDynamic.CommandName = dt.Rows[i]["year"].ToString(); 
                    lnkDynamic.Visible = true; 
                    this.Controls.Add(lnkDynamic); 
     
                } 
                    base.OnInit(e); 
            } 
     
         
        } 
    }

    Appreciate your feedback.

    Thanks, Nilesh


    23 Şubat 2012 Perşembe 16:41

Yanıtlar

Tüm Yanıtlar

  • Hi,

    I faced the same issue while creating dynamic table. Then I resolved it overiding the loadviewstate of dynamic controls using:

           protected override void LoadViewState(object savedState)
            {
                base.LoadViewState(savedState);

            }

    Thanks!!


    23 Şubat 2012 Perşembe 19:32
  • can you pl' post complete code.

    Above code did not solve my problem. not sure if i have placed it wrongly.

    I have just add this in my class library.


    Thanks, Nilesh

    24 Şubat 2012 Cuma 06:34
  • Hi Tanvi,

    I am getting below error for type casting:


    Thanks, Nilesh

    24 Şubat 2012 Cuma 16:41
  • Hi,

    Sorry, I missed some changes in the code.

    Please try it now,

    public class DynamicQuarter : System.Web.UI.WebControls.WebParts.WebPart { string siteUrl = string.Empty; string rootUrl = string.Empty; string siteName = string.Empty; private bool _error = false; Label lblyr1 = new Label(); Label lblyr2 = new Label(); Label lblyr3 = new Label(); Label lblyr4 = new Label(); DataTable dt = new DataTable(); LinkButton lnkDynamic; List<string> myControlList; protected override void CreateChildControls() { myControlList = new List<string>(); ViewState["myControlList"] = myControlList; dt = getdt(); bool hascurrentyr = false; for (int i = 0; i < dt.Rows.Count-1; i++) { if (dt.Rows[i]["year"].Equals(System.DateTime.Now.Year.ToString())) { hascurrentyr = true; break; } } if (hascurrentyr == false) { lblyr1.Text = DateTime.Now.Date.AddYears(-3).Year.ToString(); Controls.Add(lblyr1); } string beforeSubmitJS = "/nvar exportRequested = false; /n"; beforeSubmitJS += "var beforeFormSubmitFunction = theForm.onsubmit;/n"; beforeSubmitJS += "theForm.onsubmit = function(){ /n"; beforeSubmitJS += "var returnVal = beforeFormSubmitFunction(); /n"; beforeSubmitJS += "if(exportRequested && returnVal) {_spFormOnSubmitCalled=false; exportRequested=false;} /n"; beforeSubmitJS += "return returnVal; /n"; beforeSubmitJS += "}; /n"; this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true); lblyr2.Text = DateTime.Now.Date.AddYears(-2).Year.ToString(); Controls.Add(lblyr2); lblyr3.Text = DateTime.Now.Date.AddYears(-1).Year.ToString(); Controls.Add(lblyr3); if (hascurrentyr == true) { lblyr4.Text = DateTime.Now.Date.Year.ToString(); Controls.Add(lblyr4); } } protected void lnkDynamic_Command(object sender, EventArgs e) { } public DataTable getdt() { // //Code to find GUID of the list Item SPSite site = SPContext.Current.Site; SPWeb web = site.OpenWeb(); SPList objlist = web.Lists["DownloadContents"]; string guid = string.Empty; foreach (SPListItem item in objlist.Items) { if (item.Title == "Index of Customers for 3 years") { guid = item.UniqueId.ToString(); } } // //creating datatable to enter 3 yeards data with 4 quarters and querystring DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add("year", typeof(string)); dt.Columns.Add("quarter", typeof(string)); dt.Columns.Add("querystring", typeof(string)); //code to find current years current quarter string quarter = string.Empty; if (System.DateTime.Now.Month.Equals(1)||System.DateTime.Now.Month.Equals(2)||System.DateTime.Now.Month.Equals(3)) { quarter = "Q1"; } else if (System.DateTime.Now.Month.Equals(4) || System.DateTime.Now.Month.Equals(5) || System.DateTime.Now.Month.Equals(6)) { quarter = "Q2"; } else if (System.DateTime.Now.Month.Equals(7)||System.DateTime.Now.Month.Equals(8)||System.DateTime.Now.Month.Equals(9)) { quarter = "Q3"; } else if (System.DateTime.Now.Month.Equals(10) || System.DateTime.Now.Month.Equals(11) || System.DateTime.Now.Month.Equals(12)) { quarter = "Q4"; } //code to enter current years data based on current quarter if (quarter.Contains("Q1")) { //if current quarter is Q1 means 12 quarters are already appearing on webpart } else if (quarter.Contains("Q2")) { //if current quarter is Q2 that means need to enter 1st quarter of the current year and remove Q1 of the (year-3) dr = dt.NewRow(); dr["year"] = System.DateTime.Now.Year.ToString(); dr["quarter"] = "Q1"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q1"; dt.Rows.Add(dr); } else if (quarter.Contains("Q3")) { //if current quarter is Q3 that means need to enter 1st & 2nd quarter of the current year and remove Q1,Q2 of the (year-3) dr = dt.NewRow(); dr["year"] = System.DateTime.Now.Year.ToString(); dr["quarter"] = "Q1"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["year"] = System.DateTime.Now.Year.ToString(); dr["quarter"] = "Q2"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q2"; dt.Rows.Add(dr); } else if (quarter.Contains("Q4")) { //if current quarter is Q4 that means need to enter 1st,2nd & 3rd quarter of the current year and remove Q1,Q2 & Q3 of the (year-3) dr = dt.NewRow(); dr["year"] = System.DateTime.Now.Year.ToString(); dr["quarter"] = "Q1"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["year"] = System.DateTime.Now.Year.ToString(); dr["quarter"] = "Q2"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q2"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["year"] = System.DateTime.Now.Year.ToString(); dr["quarter"] = "Q3"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + System.DateTime.Now.Year.ToString() + "Q3"; dt.Rows.Add(dr); } // //Code to enter previous years(current year - 1 ) data in datatable for (int j = 1; j <= 4; j++) { dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 1); dr["quarter"] = "Q" + j.ToString(); dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 1) + "Q" + j.ToString(); dt.Rows.Add(dr); } // // //code to check how many quarter(s) of the current year are entered in Datatable string sNoOfQuartersinCurrentYear = string.Empty; for (int i = 0; i < dt.Rows.Count - 1; i++) { if (dt.Rows[i]["year"].Equals(System.DateTime.Now.Year.ToString())) { sNoOfQuartersinCurrentYear += "," + dt.Rows[i]["quarter"].ToString(); } } // // //Code to enter previous 2 years(current year - 2 ) data in datatable if (sNoOfQuartersinCurrentYear.Contains("Q4")) { return dt; } else if (sNoOfQuartersinCurrentYear.Contains("Q3")) { dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q4"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q4"; dt.Rows.Add(dr); return dt; } else if (sNoOfQuartersinCurrentYear.Contains("Q2")) { dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q3"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q3"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q4"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q4"; dt.Rows.Add(dr); return dt; } else if (sNoOfQuartersinCurrentYear.Contains("Q1")) { dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q2"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q2"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q3"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q3"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q4"; dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q4"; dt.Rows.Add(dr); return dt; } else if (string.IsNullOrEmpty(sNoOfQuartersinCurrentYear)) { for (int j = 1; j <= 4; j++) { dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 2); dr["quarter"] = "Q" + j.ToString(); dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 2) + "Q" + j.ToString(); dt.Rows.Add(dr); } for (int j = 1; j <= 4; j++) { dr = dt.NewRow(); dr["year"] = Convert.ToString(System.DateTime.Now.Year - 3); dr["quarter"] = "Q" + j.ToString(); dr["querystring"] = SPContext.Current.Site.Url + "/" + web.Name + "/download.aspx?id=" + guid + "&listname=DownloadContents&siteurl=/" + web.Name + "&Quarter=" + Convert.ToString(System.DateTime.Now.Year - 3) + "Q" + j.ToString(); dt.Rows.Add(dr); } return dt; } return dt; } public override void RenderControl(HtmlTextWriter writer) { lblyr1.RenderControl(writer); writer.WriteBreak(); string expression = "year='" + lblyr1.Text + "'"; int i = 1; // foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = "lnkDynamic_" + i; lnkDynamic.Text = row["quarter"].ToString(); lnkDynamic.CommandName = row["year"].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write("&nbsp;"); i += 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr2.RenderControl(writer); writer.WriteBreak(); expression = "year='" + lblyr2.Text + "'"; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = "lnkDynamic_" + i; lnkDynamic.Text = row["quarter"].ToString(); lnkDynamic.CommandName = row["year"].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write("&nbsp;"); i += 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr3.RenderControl(writer); writer.WriteBreak(); expression = "year='" + lblyr3.Text + "'"; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = "lnkDynamic_" + i; lnkDynamic.Text = row["quarter"].ToString(); lnkDynamic.CommandName = row["year"].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write("&nbsp;"); i += 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr4.RenderControl(writer); writer.WriteBreak(); expression = "year='" + lblyr4.Text + "'"; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = "lnkDynamic_" + i; lnkDynamic.Text = row["quarter"].ToString(); lnkDynamic.CommandName = row["year"].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write("&nbsp;"); i += 1; } } private void HandleException(Exception ex) { this._error = true; this.Controls.Clear(); this.Controls.Add(new LiteralControl(ex.Message)); } protected override void OnLoad(EventArgs e) { if (!_error) { try { base.OnLoad(e); } catch (Exception ex) { HandleException(ex); } } } protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); myControlList = (List<string>)ViewState["myControlList"]; if (myControlList.Count > 0) { foreach (string LnkID in myControlList) { lnkDynamic.ID = LnkID; } } } protected override void OnInit(EventArgs e) { List<string> ControlList = (List<string>)ViewState["myControlList"]; dt = getdt(); for (int i = dt.Rows.Count - 1; i >= 0; i--) { lnkDynamic = new LinkButton(); // lnkDynamic.EnableViewState = true; lnkDynamic.ID = "lnkDynamic_" + i; lnkDynamic.Text = "Q" + i.ToString(); lnkDynamic.Click += new EventHandler(lnkDynamic_Command); lnkDynamic.CommandName = dt.Rows[i]["year"].ToString(); lnkDynamic.Visible = true; this.Controls.Add(lnkDynamic); ControlList.Add("lnkDynamic_" + i); ViewState["myControlList"] = ControlList; } base.OnInit(e); } }

    Thanks!!

    Tanvi

    Please mark the post as helpful if it works or propose it as answer.

    25 Şubat 2012 Cumartesi 10:05
  • Hello,

    I tried this and I'm getting below error when the for the first item(dt.records.count-1) in the for loop:


    Thanks, Nilesh

    25 Şubat 2012 Cumartesi 12:58
  • Hi,

    While debugging check the value of lnkDynamic.ID and if it not null then replace ControlList.Add("lnkDynamic_" + i) with ControlList.Add(lnkDynamic.ID).

    25 Şubat 2012 Cumartesi 13:21
  • No luck :(


    Thanks, Nilesh

    25 Şubat 2012 Cumartesi 15:12
  • I made slight chnages(line with break points) and got rid of object reference.... issue

    But my actual problem has not yet solved. ***banging head***


    Thanks, Nilesh

    25 Şubat 2012 Cumartesi 16:23
  • Hi,

    Remove the changes you made and

    In Create ChildControls() method,add the below code in not postback..i think it was not there in notpostback.

    if (!Page.IsPostBack)
                {
                    myControlList = new List<string>();
                    ViewState["myControlList"] = myControlList;

    }

    one more attempt.

    and one more thing,you have created the same control in onrender method also..why the same control is created thrice?


    25 Şubat 2012 Cumartesi 19:13
  • Hi,

    I have tried above changes and still getting multiple controls can not be added........ error. Seems controls are getting added more than once.

    Also i have created controls only once i.e. in OnInit Event and in Rendercontrol event i am adding these controls.


    Thanks, Nilesh


    27 Şubat 2012 Pazartesi 12:02
  • Can any one help me in this?

    Thanks, Nilesh

    23 Mart 2012 Cuma 11:07
  • We can not create event handlers in this scenario.

    I have used datagrid control to achieve this.


    Thanks, Nilesh

    18 Mayıs 2012 Cuma 04:05