Wednesday, June 29, 2011

Dynamic LinkButton Controls in Asp.Net

Introduction

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

Explanation

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.

Coding
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)
  {
    If(!IsPostBack())
    {                  
    }
    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();
      SqlDap.Fill(objDT);

      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);
                          tblCellDef.Controls.Add(lnkEmployee);
                          tblRowDef.Controls.Add(tblCellDef);
                          intCurrentCount += 1;
                      }
                  }
              }
              tblDynamic.Controls.Add(tblRowDef);
          }

      }
     pnlDynamic.Controls.Add(tblDynamic);
  }
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;
      Response.Redirect("SomePage.aspx?ID="+lnkEmployee.CommandName);
  }

Output
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# Asp.net.
dynamiclnk

53 comments:

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

    Thanks Keep sharing

    ReplyDelete
  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....

    http://mindstick.com/Articles/3fc2fb01-4c68-4654-a3c8-385b9a4d5973/?LinkButton%20in%20ASP.Net

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

    ReplyDelete
  3. Great article thanks for sharing

    We can also submit our .net related links on http://www.dotnettechy.com to increase traffics of your website / blog

    Its kind of social networking for .Net professionals only

    ReplyDelete
  4. Thanks all for your comments

    ReplyDelete
  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

    ReplyDelete
  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.
    Shashaa
    Dot Net training Chennai | Dot Net training Chennai | Dot Net training Chennai

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

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

    digital marketing institute

    ReplyDelete
  9. 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

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

    ReplyDelete
  11. 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

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

    ReplyDelete
  13. Thank you for this valuable information. We are the best erp software solutions in chennai. Contact us on info@bravetechnologies.in.erp in chennai

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

    ReplyDelete
  15. 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

    ReplyDelete
  16. Thanks For Your valuable posting, it was very informative
    personalrekrutierung

    ReplyDelete

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

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

    ReplyDelete
  19. Great blog.. I found some useful info in your article, it was awesome to read, thanks for sharing..
    Embedded Project Center in Chennai | Embedded Project Center in Velachery

    ReplyDelete
  20. Informative and impressive. Keep Updating
    Jobvermittlung

    ReplyDelete
  21. Thanks For Your valuable posting, it was very informative
    suchmaschinenmarketing lüdenscheid

    ReplyDelete
  22. Thank you for information you been putting on making your site such an interesting. I gave something for my information.
    No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | Java Training in Chennai

    ReplyDelete
  23. Thanks for making me this article. You have done a great job by sharing this content in here. Keep writing article like this. Graphic Design Training in Chennai | Multimedia Training Institute in Chennai | Adobe Photoshop Training in Chennai

    ReplyDelete
  24. Awesome blog, you explain everything in detailed manner, it was very interesting to read. Thanks for sharing such an informative blog. Excel Macro Training in Chennai | VB Training Institute in Chennai | Tally ERP9 Training Institute in Chennai

    ReplyDelete
  25. Very nice article.Austere Technologies provide Best IT services in World wide.For more information and services please visit www.austeretech.com

    ReplyDelete
  26. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    NO.1 CLOUD SERVICES | ORACLE CLOUD SERVICES FOR APPLICATION DEVELOPMENT

    ReplyDelete
  27. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.digital marketing training in chennai

    ReplyDelete
  28. Nice and good blog to read.... really I gathered some information thanks for sharing..
    B.Com Project Center in Chennai | B.Com Project Center in Velachery

    ReplyDelete
  29. This post was awesome and interesting to read. Congrats for your work. Thanks for sharing..Embedded Project Center in Chennai | Embedded Project Center in Velachery

    ReplyDelete
  30. Nice blog with excellent information. Thank you, keep sharing.

    Join in Avinash College Of Commerce for Best career in commerce

    ReplyDelete
  31. Wow...What an excellent informative blog, really helpful. Thank you.

    Software Testing Services | Austere Technology

    ReplyDelete
  32. Great article, really very helpful content you made. Thank you, keep sharing.

    chartered accountant | Avinash college of commerce

    ReplyDelete
  33. Thank you for sharing this valuable information. But get out of this busy life and find some peace with a beautiful trip book Andaman family tour packages

    ReplyDelete
  34. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book ANDAMAN BUDGET PACKAGES @ 4999/-

    ReplyDelete
  35. If you are searching for the best norton installation support in USA and Canada then dial our toll-free number 1-844-797-8692 and get in touch of an expert they will short your all problem.
    We are the best IT support provider offering service in the USA and canada from last 8 year you can also contact us when you have problem relatec to mcafee.com/activate

    ReplyDelete
  36. 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.

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai


    ReplyDelete

Followers