Creating Daynamic Menu with SubMenu

Add menu.aspx

<body>
    <form id="form1" runat="server">
    <div>
        <fieldset>
            <legend>Add Menu </legend>
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            <table>
                <tr>
                    <td>
                        Manu
                    </td>
                    <td>
                        <asp:TextBox ID="txtmenu" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                            ControlToValidate="txtmenu" ValidationGroup="menu"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        priority
                    </td>
                    <td>
                        <asp:TextBox ID="txtpriority" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                            ControlToValidate="txtpriority" ValidationGroup="menu"></asp:RequiredFieldValidator>
                        <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="txtpriority"
                            ValidChars="0123456789">
                        </asp:FilteredTextBoxExtender>
                    </td>
                </tr>
                <tr>
                    <td>
                        Url
                    </td>
                    <td>
                        <asp:TextBox ID="txturl" runat="server"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        Is active
                    </td>
                    <td>
                        <asp:CheckBox ID="chk" runat="server" />
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnsubmit" runat="server" Text="Sumit" OnClick="btnsubmit_Click" />
                    </td>
                    <td>
                        <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click"
                            Visible="False" />
                    </td>
                    <td>
                        <asp:Label ID="lblmsg" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
            <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" DataKeyNames="id"
                OnSelectedIndexChanging="grid_SelectedIndexChanging">
                <Columns>
                    <asp:CommandField ShowSelectButton="true" HeaderText="Edit" />
                    <asp:BoundField DataField="Manu" HeaderText="Menu" />
                    <asp:BoundField DataField="priority" HeaderText="Priority Menu" />
                    <asp:BoundField DataField="url" HeaderText="Url" />
                    <asp:CheckBoxField DataField="isactive" HeaderText="Is active" />
                </Columns>
            </asp:GridView>
        </fieldset>
    </div>
    </form>
</body>




Add menu aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Addmenu : System.Web.UI.Page
{
    Dal odal = new Dal();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
        }
    }
    public void fill()
    {
        string str = "select * from user_menu_master";
        dt = odal.fetchdt(str);
        grid.DataSource = dt;
        grid.DataBind();

    }
    protected void grid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        grid.SelectedIndex = e.NewSelectedIndex;
        int id = int.Parse(grid.DataKeys[grid.SelectedIndex].Value.ToString());
        ViewState["id"] = id;
        dt = odal.fetchdt("select * from user_menu_master where id =" + id);
        if (dt.Rows.Count > 0)
        {
            txtmenu.Text = dt.Rows[0]["manu"].ToString();
            txtpriority.Text = dt.Rows[0]["priority"].ToString();
            chk.Checked = Convert.ToBoolean(dt.Rows[0]["isactive"].ToString());
            txturl.Text = dt.Rows[0]["url"].ToString();
            btnupdate.Visible = true;
            btnsubmit.Visible = false;
        }
        else
        {
            lblmsg.Text = "No item selected";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        if (txtpriority.Text == "0")
        {
            lblmsg.Text = "Priority Can't be Zero";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            string insert = "insert into user_menu_master(manu,priority,isactive,url) values('" + txtmenu.Text + "','" + txtpriority.Text + "','" + chk.Checked + "','" + txturl.Text + "')";
            odal.fetchdt(insert);
            fill();

            lblmsg.Text = "Record Insert Successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            txtpriority.Text = "";
            txtmenu.Text = "";
            chk.Checked = false;

        }

    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        if (txtpriority.Text == Convert.ToString(0))
        {
            lblmsg.Text = "Priority Can't be Zero";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {

            string update = "update user_menu_master set manu='" + txtmenu.Text + "',priority='" + txtpriority.Text + "',isactive='" + chk.Checked + "',url ='" + txturl.Text + "' where id='" + Convert.ToInt16(ViewState["id"].ToString()) + "'";
            odal.fetchdt(update);
            lblmsg.Text = "Record Update successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            txtpriority.Text = "";
            txtmenu.Text = "";
            btnupdate.Visible = false;
            btnsubmit.Visible = true;
            chk.Checked = false;
            fill();
        }

    }
}






Addsubmenu.aspx


<body>
    <form id="form1" runat="server">
    <div>
        <fieldset>
            <legend>Add Submenu</legend>
            <table>
                <tr>
                    <td>
                        Menu
                    </td>
                    <td>
                        <asp:DropDownList ID="drop" runat="server">
                        </asp:DropDownList>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        Sub Menu Name
                    </td>
                    <td>
                        <asp:TextBox ID="txtsubname" runat="server"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        url
                    </td>
                    <td>
                        <asp:TextBox ID="txturl" runat="server"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        priority
                    </td>
                    <td>
                        <asp:TextBox ID="txtprio" runat="server"></asp:TextBox>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        Isactive
                    </td>
                    <td>
                        <asp:CheckBox ID="chk" runat="server" />
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" />
                    </td>
                    <td>
                        <asp:Button ID="btnupdate" runat="server" Text="Update" Visible="false" OnClick="btnupdate_Click" />
                    </td>
                    <td>
                        <asp:Label ID="lblmsg" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>
            <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" DataKeyNames="id"
                OnSelectedIndexChanging="grid_SelectedIndexChanging" PageSize="50">
                <Columns>
                    <asp:CommandField ShowSelectButton="true" HeaderText="Edit" />
                    <asp:BoundField DataField="manu" HeaderText="Menu" />
                    <asp:BoundField DataField="submenu" HeaderText="Sub Menu" />
                    <asp:BoundField DataField="url" HeaderText="URL" />
                    <asp:BoundField DataField="priority" HeaderText="priority" />
                    <asp:CheckBoxField DataField="isactive" HeaderText="Isactive" />
                </Columns>
            </asp:GridView>
        </fieldset>
    </div>
    </form>
</body>





Addsubmenu.aspx


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class submenu : System.Web.UI.Page
{
    Dal odal = new Dal();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
            DataTable ddt = new DataTable();
            string dro = "select distinct * from user_menu_master";
            ddt = odal.fetchdt(dro);
            drop.DataSource = ddt;
            drop.DataValueField = "id";
            drop.DataTextField = "manu";
            drop.DataBind();
        }

    }
    public void fill()
    {
        string str = "select user_menu_master.manu,user_sub_menu.* from user_sub_menu join user_menu_master on user_menu_master.id=user_sub_menu.mid order by  user_menu_master.id,user_sub_menu.mid ";
        dt = odal.fetchdt(str);
        grid.DataSource = dt;
        grid.DataBind();


    }
    public void clear()
    {
        txtprio.Text = "";
        txtsubname.Text = "";
        txturl.Text = "";
        chk.Checked = false;

    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        if (txtprio.Text == "0")
        {
            lblmsg.Text = "priority can't be Zero";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            string str = "insert into user_sub_menu(submenu,url,priority,isactive,mid) values('" + txtsubname.Text + "','" + txturl.Text + "','" + txtprio.Text + "','" + chk.Checked + "','" + drop.SelectedValue + "')";
            odal.fetchdt(str);
            lblmsg.Text = "Record Insert successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            clear();
            fill();
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        if (txtprio.Text == "0")
        {
            lblmsg.Text = "priority can't be Zero";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            string update = "update user_sub_menu set submenu='" + txtsubname.Text + "',url='" + txturl.Text + "',priority='" + txtprio.Text + "',isactive='" + chk.Checked + "',mid='" + drop.SelectedValue + "' where id='" + Convert.ToInt16(ViewState["id"].ToString()) + "'";
            odal.fetchdt(update);
            lblmsg.Text = "Record Insert successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            clear();
            fill();

        }

    }
    protected void grid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        grid.SelectedIndex = e.NewSelectedIndex;
        int id = int.Parse(grid.DataKeys[grid.SelectedIndex].Value.ToString());
        ViewState["id"] = id;
        dt = odal.fetchdt("select * from user_sub_menu where id =" + id);
        if (dt.Rows.Count > 0)
        {
            drop.SelectedIndex = Convert.ToInt16(dt.Rows[0]["mid"].ToString());
            txtsubname.Text = dt.Rows[0]["submenu"].ToString();
            txtprio.Text = dt.Rows[0]["priority"].ToString();
            txturl.Text = dt.Rows[0]["url"].ToString();
            chk.Checked = Convert.ToBoolean(dt.Rows[0]["isactive"].ToString());
            btnupdate.Visible = true;
            btninsert.Visible = false;
        }
        else
        {
            lblmsg.Text = "No item selected";
        }
    }
}





Daynamicmenu.aspx

 <style type="text/css">
        .menu
        {
            width: 913px;
            font-family: verdana, Segoe UI;
            margin: 0 auto;
            border: 1px solid #B34C00;
            border-radius: 4px;
        }
        .menu ul
        {
            padding: 10px;
            background-color: #FF6600;
            float: left;
            margin: 0px;
            list-style: none;
        }
        .menu ul li        {
            display
inline-block;
            float: left;
            position: relative;
            cursor: pointer;
        }
        .menu ul li a        {
            cursor:
ointer;
            display: block;
            padding: 10px;
            float: left;
            color: #fff;
            text-decoration: none;
        }
        .menu ul li ul
        {
            display: none;
            margin-top: 10px;
        }
        .menu ul li:hover ul
        {
            display: block;
            width: 200px;
            position: absolute;
            left: 0px;
            top: 25px;
            background: #FF6600;
            border: 1px solid #B34C00;
            border-top: none;
            color: #fff;
        }
        .menu ul li:hover ul li
        {
            padding: 5px;
            float: none;
            display: block;
        }
        .menu ul li:hover ul li a
        {
            padding: 5px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="menu">
        <asp:Panel ID="Panel1" runat="server" Width="913px" Style="margin: 0px">
        </asp:Panel>
    </div>
    </form>
    <p>
    </p>
</body>




Daynamicmenu.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class dynamic_menu : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=;Initial Catalog=elaunch_div;User ID=sa;Password=****");
    Dal odal = new Dal();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getmenu();
        }
    }
    public void getmenu()
    {
        string str = "select * from user_menu_master where isactive='1' order by priority";
        SqlDataAdapter da = new SqlDataAdapter(str, con);
        da.Fill(dt);
        HtmlGenericControl main = UList("Menuid", "menu");
        foreach (DataRow row in dt.Rows)
        {
            DataTable ddt = new DataTable();
            string sub = "select * from user_sub_menu where mid='" + row["id"] + "' and isactive='1'order by mid,priority";
            da = new SqlDataAdapter(sub, con);
            da.Fill(ddt);
            if (ddt.Rows.Count > 0)
            {
                HtmlGenericControl sub_menu = LIList(row["manu"].ToString(), row["id"].ToString(), row["url"].ToString());
                HtmlGenericControl ul = new HtmlGenericControl("ul");
                foreach (DataRow r in ddt.Rows)
                {
                    ul.Controls.Add(LIList(r["submenu"].ToString(), r["id"].ToString(), r["url"].ToString()));
                }
                sub_menu.Controls.Add(ul);
                main.Controls.Add(sub_menu);

            }
            else
            {
                main.Controls.Add(LIList(row["manu"].ToString(), row["id"].ToString(), row["url"].ToString()));
            }

        }
        Panel1.Controls.Add(main);
    }

    private HtmlGenericControl UList(string id, string cssClass)
    {
        HtmlGenericControl ul = new HtmlGenericControl("ul");
        ul.ID = id;
        ul.Attributes.Add("class", cssClass);
        return ul;
    }
    private HtmlGenericControl LIList(string innerHtml, string rel, string url)
    {
        HtmlGenericControl li = new HtmlGenericControl("li");
        li.Attributes.Add("rel", rel);
        li.InnerHtml = "<a href=" + string.Format("http://localhost:5068/three%20tier%20demo/ {0}", url) + ">" + innerHtml + "</a>";
        return li;
    }
}



 Snap Shot







Comments

Popular posts from this blog

Periodic Restart Settings for Application Pool Recycling

How to receive JSON object in POST and GET using web api

Generate image ,pdf and Video Thumb using node js