Click here to Skip to main content
15,074,394 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can we Display menu items dynamically in master page from database in asp.net?

please help me to do this


Im using repeater control for displaying menu but it doesn't show anything in master page

<asp:Repeater ID="rptrmenu" runat="server">
               <ItemTemplate>

                   <li><a style="color:White " id='<%#Eval("menu_id ")%>' class="aTab" >

                      <%#Eval("menu_name ")%></a></li>
               </ItemTemplate>
           </asp:Repeater>





in code behind(test.master.cs)
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      BindRepeaterData();
   }
}

public void BindRepeaterData()
{
   Con1.SetConnection();
   StrQry = new StringBuilder();
   StrQry = new StringBuilder();
   StrQry.AppendFormat("select a.Menu_Id,a.Menu_Name from EGP_MstMenu a inner join EGP_UserGroupMenu b on a.menu_id=b.menu_id  ");
   da = new OleDbDataAdapter(StrQry.ToString(), Con1.Con);
   DataSet ds = new DataSet();
   Repeater rptrmenu = new Repeater();
   da.Fill(ds);
   if (ds.Tables[0].Rows.Count > 0)
   {
      rptrmenu.DataSource = ds;
      rptrmenu.DataBind();
   }
}
Posted
Updated 4-Apr-14 23:56pm
v5
Comments
Nelek 5-Apr-14 5:29am
   
121ss 5-Apr-14 5:34am
   
Im using repeater control for displaying menu but it doesn't show anything in master page

<asp:Repeater ID="rptrmenu" runat="server">
<itemtemplate>

<li>

<%#Eval("menu_name ")%>
</li>





in code behind(test.master.cs)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterData();
}
}


public void BindRepeaterData()
{
Con1.SetConnection();
StrQry = new StringBuilder();
StrQry = new StringBuilder();
StrQry.AppendFormat("select a.Menu_Id,a.Menu_Name from EGP_MstMenu a inner join EGP_UserGroupMenu b on a.menu_id=b.menu_id ");
da = new OleDbDataAdapter(StrQry.ToString(), Con1.Con);
DataSet ds = new DataSet();
Repeater rptrmenu = new Repeater();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
rptrmenu.DataSource = ds;
rptrmenu.DataBind();
}

}
121ss 5-Apr-14 5:38am
   
I want to display menu ,but im not getting .....
Er. Puneet Goel 5-Apr-14 5:51am
   
I think this should be fine !! what error you getting ?
121ss 5-Apr-14 5:52am
   
Im not getting menu in master page....getting blank,but i have data in database.

1 solution

As per code you posted in your question, there seems no issue in code, But check if your table returning any value or not. But in the code mention in comment you have error in position as mentioned:

XML
<asp:Repeater ID="rptrmenu" runat="server">


                    <li>

                       <%#Eval("menu_name ")%></li>




You have not used the
<itemtemplate>
tag. Let me know if more help is required?
   
v2
Comments
121ss 5-Apr-14 5:59am
   
i have used the itemtemplate
121ss 5-Apr-14 6:02am
   
datatable returning the value also..but the same code work in aspx page.is there is any other way i can do it in master page?
Er. Puneet Goel 5-Apr-14 6:05am
   
Code is perfect. Just check that the container visibility in which you have written this code block on master page. If you still don't find the issue, can you please post the HTML code block ?

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