Wednesday, June 29, 2011

Dynamic LinkButton Controls in Asp.Net


In this post I will explain how to create and add dynamic table with dynamic link button in code behind. My previous post Error Logging in C#


In the design page, we just add how many controls we need and where it should be placed. These controls are static and having the data what we gave. In some cases we have to dynamically get data from the database and create dynamic controls for the data.

Difference between static and dynamic

Static -> we can mention how many things we need.
Dynamic ->we cannot mention it generates depends upon the requirements.

Creating Dynamic LinkButton in Asp.Net is very simple. After creating the dynamic controls just add the controls in a PlaceHolder or Panel control.

Step 1: In the Design Page just create a panel named "pnlDynamic".
Step 2: Add the namespace in the code behind.
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
Step 3: Create an object for HtmlTable.
HtmlTable tblDynamic = new HtmlTable();
Step 4: In the Page_Load event, just call the dynamically creating control method BindEmployeeData(); Call the method outside the IsPostBack() method. Because our dynamic controls should recreate each and every time when a postback call.

protected void Page_Load(object sender, EventArgs e)
    BindEmployeeData ();
Step 5: Method for creating the dynamic LinkButton control depends on the database data.

public void BindEmployeeData()
      SqlConnection SqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=mytestDB;User ID=xxxxxxx;Password=zzzzzzzzz");
      SqlDataAdapter SqlDap = new SqlDataAdapter("select * from Employees", SqlConn);
      DataTable objDT = new DataTable();

      tblDynamic.ID = "tblEmployee";
      tblDynamic.Width = "100%";
      tblDynamic.CellPadding = 3;
      int intRowCount = objDT.Rows.Count;

      if ((intRowCount > 0))
          int sCount = (intRowCount / 2) + 1;
          int intCurrentCount = 0;
          for (int i = 1; i <= sCount; i++)                
   HtmlTableRow tblRowDef = new HtmlTableRow();
   tblRowDef.Align = "left";                              
if ((intRowCount > intCurrentCount))
                  for (int l = 1; l <= 2; l++)                        
 {                             if ((intRowCount > intCurrentCount))
                          HtmlTableCell tblCellDef = new HtmlTableCell();
                          tblCellDef.ID = "0" + objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          tblCellDef.Width = "400px";
                          LinkButton lnkEmployee = new LinkButton();
                          lnkEmployee.ID = "lnkEmployee0" + objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          lnkEmployee.Text = objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          lnkEmployee.CommandName = objDT.Rows[intCurrentCount]["EmpID"].ToString();
                          lnkEmployee.Font.Bold = true;
                          lnkEmployee.Font.Size = 13;
                          lnkEmployee.Width = 10;
                          lnkEmployee.Click += new EventHandler(lnkEmployeeWhole_Click);
                          intCurrentCount += 1;

Step 6: Write the handler to fire the event for the particular link button control

protected void lnkEmployeeWhole_Click(object sender, EventArgs e)
      LinkButton lnkEmployee = (LinkButton)sender;

Dynamic link button will disappear if you put LoadBookData() method within IspostBack() method.I Hope these Code will help you to understand the Dynamic LinkButton Controls in Asp.Net.My previous post Error Logging in C#


  1. Ya really good work man.It helped me for my project.

    Thanks Keep sharing

  2. This article gave me an insight into Asp.Net Link Button control.Its Simple to Understand and complete.Thanks for sharing with us. Check this helpful link too....

    Its also having nice post with wonderful explanation on Asp.Net controls.

  3. I have no words for this great post such a awe-some information i got gathered. Thanks to Author.
    html5 video player

  4. Great article thanks for sharing

    We can also submit our .net related links on to increase traffics of your website / blog

    Its kind of social networking for .Net professionals only

  5. Have you ever thought about including a little bit more than just
    your articles? I mean, what you say is important and all.
    But think of if you added some great graphics or video clips to give your posts more, "pop"!
    Your content is excellent but with pics and videos, this
    blog could certainly be one of the very best in its field.

    Awesome blog!

    Feel free to surf to my web blog ... attorneys in phoenix arizona

  6. Thanks for the article. It has given me so much information that I shared with my students in the class. Come up with more such articles.
    Dot Net training Chennai | Dot Net training Chennai | Dot Net training Chennai

  7. Hello admin, thank you for your informative post on hadoop training in Chennai. It helped a lot in training my students during our hadoop training Chennai sessions. We at Fita, provide big data training in Chennai for students who are interested in choosing a career in big data.

  8. Thank you so much... i didn’t have the knowledge in this now i get an idea about this.. thks a lot:-) Best Dot Net Training in Chennai

  9. This blog explain the details of the dynamic link button control in . I know the details of dynamic link button. All the given information that is really interesting. Thanks for this blog.
    Dotnet Training in Chennai

  10. Excellent post!!! In this competitive market, customer relationship management plays a significant role in determining a business success. That too, cloud based CRM product offer more flexibility to business owners to main strong relationship with the consumers.

    Salesforce Training in Chennai|Salesforce Training|Salesforce Training Chennai

  11. Very interesting to read. I like your blog. Thanks for sharing.

    digital marketing institute

  12. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Salesforce Training in Chennai | Salesforce Course in Chennai

  13. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai

    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

  14. Thank you for taking time to provide us some of the useful and exclusive information with us.
    dotnet courses in Chennai | .net training Chennai | .net course in Chennai

  15. his article is very nice and easy to understand, it was a very weighty once thanks a lot admin can hopefully be improved in the future and pardon me permission to share the article here : Cara menyembuhkan gagal jantung

  16. Excellent post. Thanks for your great information! Its interesting and informative. Keep Updating.

  17. Thank you for this valuable information. We are the best erp software solutions in chennai. Contact us on in chennai

  18. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp software solutions in chennai.

  19. Excellent post. I have read your blog it's very interesting and informative. Keep sharing.
    erp providers in chennai

  20. Excellent post. I have read your blog it's very interesting and informative. Keep sharing.
    erp software in chennai

  21. Nice blog with Smart content. Thanks for sharing such a nice blog. Software Testing Training in Chennai | Selenium Training in Chennai

  22. Dot Net is one of the latest technology, I searched lot of sites to known about Dot Net but after read your blog know I got lot of information about Dot Net Technology. Thank you for sharing.. Software Testing Training Institute in Chennai | Selenium Training Institute in Chennai | ISTQB Training Institute in Chennai

  23. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…
    No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | ISTQB Certification Center in Velachery

  24. Awesome blog. I enjoyed reading your articles. This is truly a great read for me.
    Suchmaschinenoptimierung in Lüdenscheid

  25. Thanks For Your valuable posting, it was very informative

  26. The website is looking bit flashy and it catches the visitors eyes. A design is pretty simple .
    office 2010 professional plus key deutsch


  27. Your website content nice nice and interesting to observe.
    suchmaschinenoptimierung lüdenscheid

  28. Thanks For Your valuable posting, it was very informative
    office 2010 professional download

  29. Thanks for your marvelous posting! It is very useful and good. Come on. I want to introduce the best aso services for you, I try it and I feel it is so good to rank app to top in app store search results, have you ever heard it?