Monday, September 22, 2008

Coding For Creating Main menu and Sub menus at run time

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class samples : System.Web.UI.Page
{
#region variables_used
static int i;
static string names;
string main_menu_name;
int j;
string[] arr_name = new string[4];
string value;
string index_value;
#endregion

#region SQL_CONNECTION_STRING
SqlConnection con_header = new SqlConnection("Data Source=.;Initial Catalog=ctsi_web;Integrated Security=True");
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ctsi_web;Integrated Security=True");
#endregion

#region Methods_Used

public void sub_menu()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from master_submenus where Menu_Index='" + names.ToString() + "'", con);
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{


TableRow tr = new TableRow();
tr.BackColor = System.Drawing.Color.Tomato;


TableCell tc = new TableCell();
tc.ForeColor = System.Drawing.Color.White;
tc.BackColor = System.Drawing.Color.Tomato;
string sub_menu = dr["Sub_Menu_Names"].ToString();
string url = dr["URL"].ToString();
tc.Text = "" + sub_menu.ToString() + "";

string name = dr["Sub_Menu_Names"].ToString();
tr.Cells.Add(tc);

Table2.Rows.Add(tr);

Response.Write("
");



}
//table design style
TableItemStyle tableStyle = new TableItemStyle();
tableStyle.HorizontalAlign = HorizontalAlign.Left;
tableStyle.VerticalAlign = VerticalAlign.Middle;
tableStyle.Width = Unit.Pixel(700);

//aplying design style for table1
foreach (TableRow r in Table1.Rows)
foreach (TableCell c in r.Cells)
c.ApplyStyle(tableStyle);

con.Close();
}
#endregion


protected void Page_Load(object sender, EventArgs e)
{

con_header.Open();//for main menu connection
SqlCommand cmd_header = new SqlCommand("select * from master_mainmenu", con_header);
SqlDataReader dr_header = cmd_header.ExecuteReader();

while (dr_header.Read())
{
//place main menu items in table 1

TableRow tr_header = new TableRow();
TableCell tc_header = new TableCell();

tc_header.BackColor = System.Drawing.Color.Chocolate;
main_menu_name = dr_header["Menu_Names"].ToString();
index_value = dr_header["Menu_Index"].ToString();

//create link for main menu items
tc_header.Text = "" + main_menu_name.ToString() + "";
tr_header.Cells.Add(tc_header);
Table1.Rows.Add(tr_header);
i = i + 1;
names = subOpt.Value.ToString();
}

//calling sub_menu creation Function
sub_menu();

//table design style
TableItemStyle tableStyle = new TableItemStyle();
tableStyle.HorizontalAlign = HorizontalAlign.Center;
tableStyle.VerticalAlign = VerticalAlign.Middle;
tableStyle.Width = Unit.Pixel(200);

//aplying design style for table1
foreach (TableRow r in Table1.Rows)
foreach (TableCell c in r.Cells)
c.ApplyStyle(tableStyle);

//aplying design style for table2
foreach (TableRow rr in Table2.Rows)
foreach (TableCell cc in rr.Cells)
cc.ApplyStyle(tableStyle);
}
}

No comments: