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

28 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. I have no words for this great post such a awe-some information i got gathered. Thanks to Author.
    html5 video player

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

    ReplyDelete
  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

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

    Salesforce Training in Chennai|Salesforce Training|Salesforce Training Chennai

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

    digital marketing institute

    ReplyDelete
  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

    ReplyDelete
  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

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

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

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

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

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

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

    ReplyDelete

Followers