Tuesday, August 27, 2013

Asp.net Treeview from CodeBehind

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView1.aspx.cs" Inherits="DUMMY_TreeView1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TreeView ID="TreeView1" runat="server" >
    
    </asp:TreeView>
    </div>
    </form>
</body>
</html>


 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DUMMY_TreeView1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTreeView();
        }
        
    }
    private void BindTreeView()
    {
        List<string> list1 = new List<string>();
        list1.Add("Business & Office");
        list1.Add("Database");
        list1.Add("Networking");
        list1.Add("Presentation");
        list1.Add("Project Management");
        list1.Add("Reports");
        list1.Add("Spreadsheet");
        list1.Add("Word Processing");
        List<string> list2 = new List<string>();
        list2.Add("Arts");
        list2.Add("Biographies");
        list2.Add("Children's Books");
        list2.Add("Computers & Internet");
        list2.Add("Cooking");
        list2.Add("History");
        list2.Add("Fiction");
        list2.Add("Mystery");
        list2.Add("Nonfiction");
        list2.Add("Romance");
        list2.Add("Science Fiction");
        list2.Add("Travel");
       
        //Node1 - Software
        TreeNode objNode1 = new TreeNode();
        objNode1.Text = "Software";
        TreeView1.Nodes.Add(objNode1);
        foreach (string objlist1 in list1)
        {
            TreeNode objchildNode1 = new TreeNode();
            objchildNode1.Text = objlist1;
            objNode1.ChildNodes.Add(objchildNode1);
        }
        //Node2 - Books
        //Node1 - Software
        TreeNode objNode2 = new TreeNode();
        objNode2.Text = "Books";
        TreeView1.Nodes.Add(objNode2);
        foreach (string objlist1 in list2)
        {
            TreeNode objchildNode1 = new TreeNode();
            objchildNode1.Text = objlist1;
            objNode2.ChildNodes.Add(objchildNode1);
        }
        //TreeView1.Nodes.Add(objchildNode1);
    }
   
}

Treeview

DataList Paging in ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList2.aspx.cs" Inherits="DUMMY_DataList2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <table width="900px" align="center">
    <tr>
    <td>
    <asp:DataList ID="DataList1" runat="server">
    <HeaderTemplate>
    <table width="900px">
    <tr style="background-color:Blue;height:30px;color:White;font-size:14px">
    <td width="150px">Title</td>
    <td width="150px">Message</td>
    </tr> 
    </table>
    </HeaderTemplate>
    <ItemTemplate>
    <table width="900px">
    <tr>
    <td width="150px"><%# Eval("Title")%></td>
    <td width="150px"><%# Eval("Message")%></td>
    
    </tr>
    </table>
    </ItemTemplate>
    </asp:DataList>
    </td>
    </tr>
    <tr>
    <td>
    <table id="tblPaging" runat="server">
    <tr>
    <td style="padding-right: 7px" valign="top">
    <asp:LinkButton ID="lnkbtnPrevious" runat="server" OnClick="lnkbtnPrevious_Click">Previous</asp:LinkButton>
    </td>
    <td valign="top">
    <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" OnItemDataBound="dlPaging_ItemDataBound"
    RepeatDirection="Horizontal">
    <ItemTemplate>
    <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
    CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
    </ItemTemplate>
    </asp:DataList>
    </td>
    <td style="padding-left: 7px" valign="top">
    <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">Next</asp:LinkButton>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </form>
</body>
</html>


 



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.Configuration;
public partial class DUMMY_DataList2 : System.Web.UI.Page
{
    PagedDataSource pds = new PagedDataSource();
    protected void Page_Load(object sender, EventArgs e)
    {
        bindDataList();
    }
    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        bindDataList();
    }
    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        bindDataList();
    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            bindDataList();
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Font.Bold = true;
        }
    }
    public int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        for (int i = 0; i < pds.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        dlPaging.DataSource = dt;
        dlPaging.DataBind();
    }
    void bindDataList()
    {
        string connn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            
        SqlConnection con = new SqlConnection(connn);
        con.Open();
        string str = "select * from Announcement";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataList1.DataSource = ds;
        DataList1.DataBind();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        //to set the paging change the number 3 to your desired value
        pds.PageSize = 3;
        pds.CurrentPageIndex = CurrentPage;
        lnkbtnNext.Enabled = !pds.IsLastPage;
        lnkbtnPrevious.Enabled = !pds.IsFirstPage;
        DataList1.DataSource = pds;
        DataList1.DataBind();
        doPaging();
        con.Close();
    }
}


 



DataList2

How to Bind DataList using SqlDataAdapter and DataTable in asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList1.aspx.cs" Inherits="DUMMY_DataList1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <fieldset style="width:250px;">
            <legend>Bind DataList example in asp.net</legend>
            <asp:DataList ID="dlEmployee" runat="server" CellPadding="4" ForeColor="#333333" RepeatColumns="2">
                <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <ItemTemplate>
                <table style="width:220px;" cellspacing="2" cellpadding="2">
                      <tr>
                        <td><b>Title:</b> <%#DataBinder.Eval(Container.DataItem, "Title")%></td>
                      </tr>
                      <tr>
                        <td><b>Message:</b> <%#DataBinder.Eval(Container.DataItem, "Message")%></td>
                      </tr>
                      <tr>
                        <%--<td><b>Salary:</b> <%#DataBinder.Eval(Container.DataItem, "SALARY")%></td>--%>
                      </tr>
                </table>
            </ItemTemplate>
                <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        </asp:DataList>
        </fieldset>
    </div>
    </form>
</body>
</html>


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.Configuration;
public partial class DUMMY_DataList1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDataList();
        }
    }
    protected void BindDataList()
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from Announcement", con);
            adp.Fill(dt);
            dlEmployee.DataSource = dt;
            dlEmployee.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write("Error occured: " + ex.Message.ToString());
        }
        finally
        {
            dt.Clear();
            dt.Dispose();
            con.Close();
        }
    }
}


 



DataList

Repeater Paging in C# without post back ("asynchronous paging")

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater3.aspx.cs" Inherits="DUMMY_Repeater3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
     <form id="form1" runat="server">   
    <asp:ScriptManager ID="MainScriptManager" runat="server" />
        <asp:UpdatePanel ID="pnlHelloWorld" runat="server">        
            <ContentTemplate>
                <div>
                    <asp:Repeater ID="rptItems" runat="server">        
                        <ItemTemplate>            
                            <div>
                                <div>
                                    <%# DataBinder.Eval(Container.DataItem,"Title") %>
                                </div>
                                <div>
                                    <%# DataBinder.Eval(Container.DataItem, "Message")%>
                                </div>
                                <div>
                                   <%-- <%# DataBinder.Eval(Container.DataItem, "BrandName")%>--%>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
                <div>
                <table>
                <tr>
                <td>
                <asp:LinkButton ID="lbPrev" runat="server" OnCommand="lbPrev_Command">Prev </asp:LinkButton> 
                </td>
                <td>
                <asp:Repeater ID="rptPages" runat="server">       
                        <ItemTemplate>
                            <div style="float:left;padding-left:10px;">
                                <asp:LinkButton ID="btnPage" style="padding:8px; margin:2px; background:#ffa100; border:solid 1px #666; font:8pt tahoma;" CommandName="Page" CommandArgument="<%#Container.DataItem %>" CssClass="text" runat="server"  ForeColor="White" Font-Bold="True"><%# Container.DataItem %>
                                </asp:LinkButton>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </td>
                <td>
                 <asp:LinkButton ID="lbNext" runat="server" OnCommand="lbNext_Command">Next </asp:LinkButton> 
                </td>
                </tr>
                </table>
                   
                    
                  
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>     
    </form>
</body>
</html>


 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Configuration;
public partial class DUMMY_Repeater3 : System.Web.UI.Page
{
    public static int totalPages = 0;
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    private void LoadData()
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString.ToString());
        SqlCommand cmd = new SqlCommand("Select * from Announcement", cn);
        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        cn.Close();
        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 10;
        pgitems.CurrentPageIndex = PageNumber;
        totalPages = pgitems.PageCount - 1;
        if (pgitems.PageCount > 1)
        {
            rptPages.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPages.DataSource = pages;
            rptPages.DataBind();
        }
        else
            rptPages.Visible = false;
        rptItems.DataSource = pgitems;
        rptItems.DataBind();
    }
    void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        LoadData();
    }
    protected void lbPrev_Command(Object sender, CommandEventArgs e)
    {
        if (PageNumber == 0)
        {
            PageNumber = 0;
        }
        else
        {
            PageNumber = PageNumber - 1;
        }
        LoadData();
    }
    protected void lbNext_Command(Object sender, CommandEventArgs e)
    {
        if (PageNumber == totalPages)
        {
            PageNumber = totalPages;
        }
        else
        {
            PageNumber = PageNumber + 1;
        }
        LoadData();
    }
}