Event handler for dynamically created linkbuttons in custom webparthttp://forums.asp.net/t/1773467.aspx/1?Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartTue, 28 Feb 2012 14:41:14 -050017734674849469http://forums.asp.net/p/1773467/4849469.aspx/1?Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartEvent handler for dynamically created linkbuttons in custom webpart <p>Hi All,</p> <p>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.</p> <p>When i click on the button it all the linkbutton objects gets initialized once again and all the properties are getting reset.</p> <p>Below is the code i have written so far:</p> <pre class="prettyprint">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 &lt; dt.Rows.Count-1; i&#43;&#43;) { if (dt.Rows[i][&quot;year&quot;].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 = &quot;/nvar exportRequested = false; /n&quot;; beforeSubmitJS &#43;= &quot;var beforeFormSubmitFunction = theForm.onsubmit;/n&quot;; beforeSubmitJS &#43;= &quot;theForm.onsubmit = function(){ /n&quot;; beforeSubmitJS &#43;= &quot;var returnVal = beforeFormSubmitFunction(); /n&quot;; beforeSubmitJS &#43;= &quot;if(exportRequested &amp;&amp; returnVal) {_spFormOnSubmitCalled=false; exportRequested=false;} /n&quot;; beforeSubmitJS &#43;= &quot;return returnVal; /n&quot;; beforeSubmitJS &#43;= &quot;}; /n&quot;; this.Page.ClientScript.RegisterStartupScript(this.GetType(), &quot;alterFormSubmitEvent&quot;, 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[&quot;DownloadContents&quot;]; string guid = string.Empty; foreach (SPListItem item in objlist.Items) { if (item.Title == &quot;Index of Customers for 3 years&quot;) { 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(&quot;year&quot;, typeof(string)); dt.Columns.Add(&quot;quarter&quot;, typeof(string)); dt.Columns.Add(&quot;querystring&quot;, 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 = &quot;Q1&quot;; } else if (System.DateTime.Now.Month.Equals(4) || System.DateTime.Now.Month.Equals(5) || System.DateTime.Now.Month.Equals(6)) { quarter = &quot;Q2&quot;; } else if (System.DateTime.Now.Month.Equals(7)||System.DateTime.Now.Month.Equals(8)||System.DateTime.Now.Month.Equals(9)) { quarter = &quot;Q3&quot;; } else if (System.DateTime.Now.Month.Equals(10) || System.DateTime.Now.Month.Equals(11) || System.DateTime.Now.Month.Equals(12)) { quarter = &quot;Q4&quot;; } //code to enter current years data based on current quarter if (quarter.Contains(&quot;Q1&quot;)) { //if current quarter is Q1 means 12 quarters are already appearing on webpart } else if (quarter.Contains(&quot;Q2&quot;)) { //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[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q1&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q1&quot;; dt.Rows.Add(dr); } else if (quarter.Contains(&quot;Q3&quot;)) { //if current quarter is Q3 that means need to enter 1st &amp; 2nd quarter of the current year and remove Q1,Q2 of the (year-3) dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q1&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q1&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q2&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q2&quot;; dt.Rows.Add(dr); } else if (quarter.Contains(&quot;Q4&quot;)) { //if current quarter is Q4 that means need to enter 1st,2nd &amp; 3rd quarter of the current year and remove Q1,Q2 &amp; Q3 of the (year-3) dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q1&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q1&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q2&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q2&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q3&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q3&quot;; dt.Rows.Add(dr); } // //Code to enter previous years(current year - 1 ) data in datatable for (int j = 1; j &lt;= 4; j&#43;&#43;) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 1); dr[&quot;quarter&quot;] = &quot;Q&quot; &#43; j.ToString(); dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 1) &#43; &quot;Q&quot; &#43; 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 &lt; dt.Rows.Count - 1; i&#43;&#43;) { if (dt.Rows[i][&quot;year&quot;].Equals(System.DateTime.Now.Year.ToString())) { sNoOfQuartersinCurrentYear &#43;= &quot;,&quot; &#43; dt.Rows[i][&quot;quarter&quot;].ToString(); } } // // //Code to enter previous 2 years(current year - 2 ) data in datatable if (sNoOfQuartersinCurrentYear.Contains(&quot;Q4&quot;)) { return dt; } else if (sNoOfQuartersinCurrentYear.Contains(&quot;Q3&quot;)) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q4&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q4&quot;; dt.Rows.Add(dr); return dt; } else if (sNoOfQuartersinCurrentYear.Contains(&quot;Q2&quot;)) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q3&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q3&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q4&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q4&quot;; dt.Rows.Add(dr); return dt; } else if (sNoOfQuartersinCurrentYear.Contains(&quot;Q1&quot;)) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q2&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q2&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q3&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q3&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q4&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q4&quot;; dt.Rows.Add(dr); return dt; } else if (string.IsNullOrEmpty(sNoOfQuartersinCurrentYear)) { for (int j = 1; j &lt;= 4; j&#43;&#43;) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q&quot; &#43; j.ToString(); dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q&quot; &#43; j.ToString(); dt.Rows.Add(dr); } for (int j = 1; j &lt;= 4; j&#43;&#43;) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 3); dr[&quot;quarter&quot;] = &quot;Q&quot; &#43; j.ToString(); dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 3) &#43; &quot;Q&quot; &#43; j.ToString(); dt.Rows.Add(dr); } return dt; } return dt; } public override void RenderControl(HtmlTextWriter writer) { lblyr1.RenderControl(writer); writer.WriteBreak(); string expression = &quot;year='&quot; &#43; lblyr1.Text &#43; &quot;'&quot;; int i = 1; // foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr2.RenderControl(writer); writer.WriteBreak(); expression = &quot;year='&quot; &#43; lblyr2.Text &#43; &quot;'&quot;; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr3.RenderControl(writer); writer.WriteBreak(); expression = &quot;year='&quot; &#43; lblyr3.Text &#43; &quot;'&quot;; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr4.RenderControl(writer); writer.WriteBreak(); expression = &quot;year='&quot; &#43; lblyr4.Text &#43; &quot;'&quot;; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 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 &gt;= 0; i--) { lnkDynamic = new LinkButton(); lnkDynamic.EnableViewState = true; lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = &quot;Q&quot; &#43; i.ToString(); lnkDynamic.Click &#43;= new EventHandler(lnkDynamic_Command); lnkDynamic.CommandName = dt.Rows[i][&quot;year&quot;].ToString(); lnkDynamic.Visible = true; this.Controls.Add(lnkDynamic); } base.OnInit(e); } } }</pre> 2012-02-24T14:10:42-05:004850964http://forums.asp.net/p/1773467/4850964.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p>Hello</p> <p>In my mindI think it's because of the OnInit problemit will recreate controls again and againI mean you can try to set a property called &quot;Flag&quot; with viewstate to check whether it's the first load or notand say if(Flag)</p> <p>And anywayI think you are using&nbsp;CreateChildControls method to be overridenSo please do to set ChildControlCreated=true in order to avoid being recreated again</p> <p>Reguards</p> 2012-02-26T00:05:21-05:004853038http://forums.asp.net/p/1773467/4853038.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p>Hi,</p> <p>Could you assist me as where to set the property:</p> <p>ChildControlsCreated=true</p> 2012-02-27T13:34:06-05:004853750http://forums.asp.net/p/1773467/4853750.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p></p> <blockquote><span class="icon-blockquote"></span> <h4>nileshvk</h4> Could you assist me as where to set the property:</blockquote> <p></p> <p>In the overriden method</p> <pre class="prettyprint">CreateChildControls</pre> 2012-02-28T00:15:31-05:004854194http://forums.asp.net/p/1773467/4854194.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p>How should the code look like in OnInit event?</p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-02-28T06:49:18-05:004854233http://forums.asp.net/p/1773467/4854233.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p></p> <blockquote><span class="icon-blockquote"></span> <h4>nileshvk</h4> <p></p> <p>How should the code look like in OnInit event</p> <p></p> </blockquote> <p></p> <p>WhatNoPlz put it in the&nbsp;</p> <pre class="prettyprint">protected override void CreateChildControls() { ChildControlCreated=True }</pre> 2012-02-28T07:13:10-05:004854626http://forums.asp.net/p/1773467/4854626.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p>Hey , i know this needs to be added in CreateChildControl() event.</p> <p>I need assistance on OnInit() Event as i am not able to catch correct commandname property for each link button.</p> <p>Also as per your suggestion can you send some code for If(Flag) {...}</p> 2012-02-28T10:24:30-05:004854689http://forums.asp.net/p/1773467/4854689.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p></p> <blockquote><span class="icon-blockquote"></span> <h4>nileshvk</h4> I need assistance on OnInit() Event as i am not able to catch correct commandname property for each link button.</blockquote> <p></p> <p>Maybe you should save a List&lt;string&gt; into ViewState, where in the List&lt;string&gt; there are each name of linkbuttons.....</p> 2012-02-28T10:54:25-05:004855107http://forums.asp.net/p/1773467/4855107.aspx/1?Re+Event+handler+for+dynamically+created+linkbuttons+in+custom+webpartRe: Event handler for dynamically created linkbuttons in custom webpart <p>I have already tried this per sugestion from different forum:</p> <pre class="prettyprint">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&lt;string&gt; myControlList; protected override void CreateChildControls() { if (!Page.IsPostBack) { myControlList = new List&lt;string&gt;(); ViewState[&quot;myControlList&quot;] = myControlList; } myControlList = new List&lt;string&gt;(); ViewState[&quot;myControlList&quot;] = myControlList; dt = getdt(); bool hascurrentyr = false; for (int i = 0; i &lt; dt.Rows.Count - 1; i&#43;&#43;) { if (dt.Rows[i][&quot;year&quot;].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 = &quot;/nvar exportRequested = false; /n&quot;; beforeSubmitJS &#43;= &quot;var beforeFormSubmitFunction = theForm.onsubmit;/n&quot;; beforeSubmitJS &#43;= &quot;theForm.onsubmit = function(){ /n&quot;; beforeSubmitJS &#43;= &quot;var returnVal = beforeFormSubmitFunction(); /n&quot;; beforeSubmitJS &#43;= &quot;if(exportRequested &amp;&amp; returnVal) {_spFormOnSubmitCalled=false; exportRequested=false;} /n&quot;; beforeSubmitJS &#43;= &quot;return returnVal; /n&quot;; beforeSubmitJS &#43;= &quot;}; /n&quot;; this.Page.ClientScript.RegisterStartupScript(this.GetType(), &quot;alterFormSubmitEvent&quot;, 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[&quot;DownloadContents&quot;]; string guid = string.Empty; foreach (SPListItem item in objlist.Items) { if (item.Title == &quot;Index of Customers for 3 years&quot;) { 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(&quot;year&quot;, typeof(string)); dt.Columns.Add(&quot;quarter&quot;, typeof(string)); dt.Columns.Add(&quot;querystring&quot;, 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 = &quot;Q1&quot;; } else if (System.DateTime.Now.Month.Equals(4) || System.DateTime.Now.Month.Equals(5) || System.DateTime.Now.Month.Equals(6)) { quarter = &quot;Q2&quot;; } else if (System.DateTime.Now.Month.Equals(7) || System.DateTime.Now.Month.Equals(8) || System.DateTime.Now.Month.Equals(9)) { quarter = &quot;Q3&quot;; } else if (System.DateTime.Now.Month.Equals(10) || System.DateTime.Now.Month.Equals(11) || System.DateTime.Now.Month.Equals(12)) { quarter = &quot;Q4&quot;; } //code to enter current years data based on current quarter if (quarter.Contains(&quot;Q1&quot;)) { //if current quarter is Q1 means 12 quarters are already appearing on webpart } else if (quarter.Contains(&quot;Q2&quot;)) { //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[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q1&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q1&quot;; dt.Rows.Add(dr); } else if (quarter.Contains(&quot;Q3&quot;)) { //if current quarter is Q3 that means need to enter 1st &amp; 2nd quarter of the current year and remove Q1,Q2 of the (year-3) dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q1&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q1&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q2&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q2&quot;; dt.Rows.Add(dr); } else if (quarter.Contains(&quot;Q4&quot;)) { //if current quarter is Q4 that means need to enter 1st,2nd &amp; 3rd quarter of the current year and remove Q1,Q2 &amp; Q3 of the (year-3) dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q1&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q1&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q2&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q2&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = System.DateTime.Now.Year.ToString(); dr[&quot;quarter&quot;] = &quot;Q3&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; System.DateTime.Now.Year.ToString() &#43; &quot;Q3&quot;; dt.Rows.Add(dr); } // //Code to enter previous years(current year - 1 ) data in datatable for (int j = 1; j &lt;= 4; j&#43;&#43;) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 1); dr[&quot;quarter&quot;] = &quot;Q&quot; &#43; j.ToString(); dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 1) &#43; &quot;Q&quot; &#43; 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 &lt; dt.Rows.Count - 1; i&#43;&#43;) { if (dt.Rows[i][&quot;year&quot;].Equals(System.DateTime.Now.Year.ToString())) { sNoOfQuartersinCurrentYear &#43;= &quot;,&quot; &#43; dt.Rows[i][&quot;quarter&quot;].ToString(); } } // // //Code to enter previous 2 years(current year - 2 ) data in datatable if (sNoOfQuartersinCurrentYear.Contains(&quot;Q4&quot;)) { return dt; } else if (sNoOfQuartersinCurrentYear.Contains(&quot;Q3&quot;)) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q4&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q4&quot;; dt.Rows.Add(dr); return dt; } else if (sNoOfQuartersinCurrentYear.Contains(&quot;Q2&quot;)) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q3&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q3&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q4&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q4&quot;; dt.Rows.Add(dr); return dt; } else if (sNoOfQuartersinCurrentYear.Contains(&quot;Q1&quot;)) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q2&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q2&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q3&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q3&quot;; dt.Rows.Add(dr); dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q4&quot;; dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q4&quot;; dt.Rows.Add(dr); return dt; } else if (string.IsNullOrEmpty(sNoOfQuartersinCurrentYear)) { for (int j = 1; j &lt;= 4; j&#43;&#43;) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 2); dr[&quot;quarter&quot;] = &quot;Q&quot; &#43; j.ToString(); dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 2) &#43; &quot;Q&quot; &#43; j.ToString(); dt.Rows.Add(dr); } for (int j = 1; j &lt;= 4; j&#43;&#43;) { dr = dt.NewRow(); dr[&quot;year&quot;] = Convert.ToString(System.DateTime.Now.Year - 3); dr[&quot;quarter&quot;] = &quot;Q&quot; &#43; j.ToString(); dr[&quot;querystring&quot;] = SPContext.Current.Site.Url &#43; &quot;/&quot; &#43; web.Name &#43; &quot;/download.aspx?id=&quot; &#43; guid &#43; &quot;&amp;listname=DownloadContents&amp;siteurl=/&quot; &#43; web.Name &#43; &quot;&amp;Quarter=&quot; &#43; Convert.ToString(System.DateTime.Now.Year - 3) &#43; &quot;Q&quot; &#43; j.ToString(); dt.Rows.Add(dr); } return dt; } return dt; } public override void RenderControl(HtmlTextWriter writer) { lblyr1.RenderControl(writer); writer.WriteBreak(); string expression = &quot;year='&quot; &#43; lblyr1.Text &#43; &quot;'&quot;; int i = 1; // foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr2.RenderControl(writer); writer.WriteBreak(); expression = &quot;year='&quot; &#43; lblyr2.Text &#43; &quot;'&quot;; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr3.RenderControl(writer); writer.WriteBreak(); expression = &quot;year='&quot; &#43; lblyr3.Text &#43; &quot;'&quot;; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 1; } i = 1; writer.WriteBreak(); writer.WriteBreak(); lblyr4.RenderControl(writer); writer.WriteBreak(); expression = &quot;year='&quot; &#43; lblyr4.Text &#43; &quot;'&quot;; foreach (DataRow row in dt.Select(expression)) { lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = row[&quot;quarter&quot;].ToString(); lnkDynamic.CommandName = row[&quot;year&quot;].ToString(); lnkDynamic.EnableViewState = true; lnkDynamic.RenderControl(writer); writer.Write(&quot;&amp;nbsp;&quot;); i &#43;= 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&lt;string&gt;)ViewState[&quot;myControlList&quot;]; if (myControlList.Count &gt; 0) { foreach (string LnkID in myControlList) { lnkDynamic.ID = LnkID; } } } protected override void OnInit(EventArgs e) { List&lt;string&gt; ControlList = (List&lt;string&gt;)ViewState[&quot;myControlList&quot;]; dt = getdt(); for (int i = dt.Rows.Count - 1; i &gt;= 0; i--) { lnkDynamic = new LinkButton(); // lnkDynamic.EnableViewState = true; lnkDynamic.ID = &quot;lnkDynamic_&quot; &#43; i; lnkDynamic.Text = &quot;Q&quot; &#43; i.ToString(); lnkDynamic.Click &#43;= new EventHandler(lnkDynamic_Command); lnkDynamic.CommandName = dt.Rows[i][&quot;year&quot;].ToString(); lnkDynamic.Visible = true; this.Controls.Add(lnkDynamic); if (!ControlList.Contains(&quot;lnkDynamic_&quot; &#43; i)) { ControlList.Add(&quot;lnkDynamic_&quot; &#43; i); } //if (lnkDynamic.ID!=null) //{ // ControlList.Add(&quot;lnkDynamic_&quot; &#43; i); //} //ViewState[&quot;myControlList&quot;] = ControlList; } base.OnInit(e); } }</pre> <p>&nbsp;And I'm getting &quot;</p> <h2>Multiple controls with the same ID 'lnkDynamic_11' were found. FindControl requires that controls have unique IDs.&quot; error.</h2> <p>I further google this and landed on below link</p> <p><a href="http://support.microsoft.com/kb/834608">http://support.microsoft.com/kb/834608</a></p> <p>Any thought?</p> <p>&nbsp;</p> 2012-02-28T14:41:14-05:00