Internet Marketing Strategy & Website Development

Icon

This blog is contains information related to Internet Marketing Strategies, Website Development, and Website Design (ASP.net)

Bind Repeater to a DataTable

<asp:Repeater ID="RepeaterMenu" runat="server" OnItemDataBound="RepeaterMenu_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="lblLi" runat="server" Text="Label"></asp:Label>
        </ItemTemplate>
    </asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string pageName = Path.GetFileNameWithoutExtension(Page.Request.PhysicalPath);

            //Declare Objects
            SqlConnection conn;
            SqlDataAdapter sqlDA;

            StringBuilder SQL = new StringBuilder();
            SQL.Append(" SELECT displayName, pageName");
            SQL.Append(" FROM menu");

            string connectionString = ConfigurationManager.ConnectionStrings[
             "DefaultDatabase"].ConnectionString;

            //Initialize connection
            conn = new SqlConnection(connectionString);



            try
            {
                conn.Open();

                sqlDA = new SqlDataAdapter(SQL.ToString(), conn);

                DataTable dt = new DataTable();
                sqlDA.Fill(dt);
                conn.Close();

                RepeaterMenu.DataSource = dt;
                RepeaterMenu.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }

        }
    }

    protected void RepeaterMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string loadedPageName = Path.GetFileNameWithoutExtension(Page.Request.PhysicalPath).ToUpper();


        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            DataRowView row = e.Item.DataItem as DataRowView;

            string pageName = row["pageName"].ToString();
            string displayName = row["displayName"].ToString().ToUpper();

            if (displayName == loadedPageName)
            {
                ((Label)e.Item.FindControl("lblLi")).Text = "<li class='activeMenu'><a href='" + pageName + "'>" + displayName + "</a></li>";
            }
            else
            {
                ((Label)e.Item.FindControl("lblLi")).Text = "<li><a href='" + pageName + "'>" + displayName + "</a></li>";
            }
        }
    }

About these ads

Filed under: ASP.NET, C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: