Click here to Skip to main content
15,900,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello I am trying the following code to generate the header dynamically but not getting anything on header...Plz Help...
C#
protected void Page_Load(object sender, EventArgs e)
      {

          ArrayList al = new ArrayList();
          string[] aa = new string[] { "Abc", "Bfd", "Csd", "Has", "Pcd", "cdd", "Rep" };
          switch (Session["Username"].ToString())
          {
              case "Admin":
                  LinkButton link1 = new LinkButton();
                  link1.Text = aa[0];
                  LinkButton link2 = new LinkButton();
                  link2.Text = aa[1];
                  LinkButton link3 = new LinkButton();
                  link3.Text = aa[2];
                   LinkButton link4 = new LinkButton();
                  link4.Text = aa[3];
                   LinkButton link5 = new LinkButton();
                  link5.Text = aa[4];
                   LinkButton link6 = new LinkButton();
                  link6.Text = aa[5];
                   LinkButton link7 = new LinkButton();
                  link7.Text = aa[6];
                  break;

              case"User":
                   LinkButton link8 = new LinkButton();
                  link8.Text = aa[0];
                  LinkButton link9 = new LinkButton();
                  link9.Text = aa[1];
                  LinkButton link10 = new LinkButton();
                  link10.Text = aa[2];
                   LinkButton link11 = new LinkButton();
                  link11.Text = aa[3];
                   LinkButton link12 = new LinkButton();
                  link12.Text = aa[4];
                  break;

              case"ReadOnly":
                  LinkButton link13 = new LinkButton();
                  link13.Text = aa[0];
                  break;

          }
          foreach (LinkButton s in al)
          {
              headerMenuTab.Controls.Add(s);
          }
Posted
Updated 15-Sep-13 19:29pm
v2
Comments
syed shanu 16-Sep-13 1:16am    
You never used al - al is empty foreach (LinkButton s in al)

Hi,
you are not adding the link button to (al) array List. The items in array will be empty. No links would be binded in Header.
 
Share this answer
 
Comments
ajays3356 16-Sep-13 1:29am    
ookk...so how should i do to work properly the above code plz suggest
try this, it will save your time.

dynamic menu generation in asp.net
 
Share this answer
 
Comments
ajays3356 16-Sep-13 2:07am    
for (int i = 0; i < aa.Length; i++)
{

al.Add(aa[i]);
}
foreach (LinkButton s in al)
{
headerMenuTab.Controls.Add(s);
}


now i have added the above code but getting following error..
Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.LinkButton'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900