Sunday, May 26, 2013

List View Template Control using Asp.net 3.5

Identifies the content for the group layout. It contains a placeholder object, such as a table cell (td), div, or span that will be replaced with the content defined in the other templates, such as the ItemTemplate and EmptyItemTemplate templates.
Identifies the content to render between groups of items.
Identifies the content to render for an empty item when a GroupTemplate template is used. For example, if the GroupItemCount property is set to 5, and the total number of items returned from the data source is 8, the last row of data displayed by the ListView control will contain three items as specified by the ItemTemplate template, and two items as specified by the EmptyItemTemplate template.
Identifies the content to render if the data source returns no data.
Identifies the content to render for the selected data item to differentiate the selected item from the other displayed items.
Identifies the content to render for alternating items to make it easier to distinguish between consecutive items.
Identifies the content to render when an item is being edited. The EditItemTemplate template is rendered in place of the ItemTemplate template for the data item being edited.
Identifies the content to render when an item is being inserted. The InsertItemTemplate template is rendered in place of an ItemTemplate template at either the start of the items displayed by the ListView control, or at the end. You can specify where the InsertItemTemplate template is rendered by using the InsertItemPosition property of the ListView control.
   
 
 
 string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPersonDetails("Name ASC");
        }
    }

    ///
    /// Fires when Edit link is clicked
    ///

    ///
    ///
    protected void EditListViewItem(object sender, ListViewEditEventArgs e)
    {
        ListView1.EditIndex = e.NewEditIndex;
        // Rebind the details
        BindPersonDetails("Name ASC");
    }


    ///
    /// Fires when Update link is clicked
    ///

    ///
    ///
    protected void UpdateListViewItem(object sender, ListViewUpdateEventArgs e)
    {
        ListViewItem item = ListView1.Items[e.ItemIndex];
       
        int autoID = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());

        TextBox tName = (TextBox)item.FindControl("txtName");
        TextBox tAddress = (TextBox)item.FindControl("txtAddress");
        TextBox tPhone = (TextBox)item.FindControl("txtPhone");

        // insert records into database
        using (SqlConnection conn = new SqlConnection(_connStr))
        {
            string Sql = "update Details set Name = @Name, Address = @Address, Phone = @Phone where AutoID = @AutoID";
            conn.Open();
            using (SqlCommand dCmd = new SqlCommand(Sql, conn))
            {
                dCmd.Parameters.AddWithValue("@AutoID", autoID);
                dCmd.Parameters.AddWithValue("@Name", tName.Text.Trim());
                dCmd.Parameters.AddWithValue("@Address", tAddress.Text.Trim());
                dCmd.Parameters.AddWithValue("@Phone", tPhone.Text.Trim());
                dCmd.ExecuteNonQuery();
            }
            conn.Close();
            lblMessage.Text = "Records Updated Successfully.";
        }

        ListView1.EditIndex = -1;
        // Rebind the details
        BindPersonDetails("Name ASC");
    }

    ///
    /// Fires when Cancel link is clicked
    ///

    ///
    ///
    protected void CancelListViewItem(object sender, ListViewCancelEventArgs e)
    {
        ListView1.EditIndex = -1;
        // Rebind the data
        BindPersonDetails("Name ASC");
    }

    ///
    /// Fires when Insert button is clicked
    ///

    ///
    ///
    protected void InsertListViewItem(object sender, ListViewInsertEventArgs e)
    {
        ListViewItem item = e.Item;
        TextBox tName = (TextBox) item.FindControl("txtName");
        TextBox tAddress = (TextBox)item.FindControl("txtAddress");
        TextBox tPhone = (TextBox)item.FindControl("txtPhone");

        // insert records into database
        using (SqlConnection conn = new SqlConnection(_connStr))
        {
            string Sql = "insert into Details (Name, Address, Phone) values " +
                " (@Name, @Address, @Phone)";
            conn.Open();
            using (SqlCommand dCmd = new SqlCommand(Sql, conn))
            {
                dCmd.Parameters.AddWithValue("@Name", tName.Text.Trim());
                dCmd.Parameters.AddWithValue("@Address", tAddress.Text.Trim());
                dCmd.Parameters.AddWithValue("@Phone", tPhone.Text.Trim());
                dCmd.ExecuteNonQuery();
            }
            conn.Close();
        }
        lblMessage.Text = "New Records Inserted Successfully.";
        // Rebind the details
        BindPersonDetails("Name ASC");
    }

    ///
    /// Fires when page links are clicked in the Page control
    ///

    ///
    ///
    protected void PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        // Rebind the data
        BindPersonDetails("Name ASC");
    }

    ///
    /// Fires when delete link is clicked
    ///

    ///
    ///
    protected void DeleteListViewItem(object sender, ListViewDeleteEventArgs e)
    {
        int autoID = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
       
        // insert records into database
        using (SqlConnection conn = new SqlConnection(_connStr))
        {
            string Sql = "delete from Details where AutoID = @AutoID";
            conn.Open();
            using (SqlCommand dCmd = new SqlCommand(Sql, conn))
            {
                dCmd.Parameters.AddWithValue("@AutoID", autoID);
                dCmd.ExecuteNonQuery();
            }
            conn.Close();
            lblMessage.Text = "Records Deleted Successfully.";
        }
        // Rebind the details
        BindPersonDetails("Name ASC");
    }

    ///
    /// Fires when column name is clicked
    ///

    ///
    ///
    protected void SortListViewRecords(object sender, ListViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression + " " + e.SortDirection;
        BindPersonDetails(sortExpression);
    }

    ///
    /// Bind Person Details data
    ///

    private void BindPersonDetails(string sortExpression)
    {
        sortExpression = sortExpression.Replace("Ascending", "ASC");

        using (SqlConnection conn = new SqlConnection(_connStr))
        {
            conn.Open();
            using (SqlDataAdapter dAd = new SqlDataAdapter("select * from Details order by Name", conn))
            {
                DataTable dTable = new DataTable();
                dAd.Fill(dTable);
                // Sort now
                dTable.DefaultView.Sort = sortExpression;
                // Bind data now
                ListView1.DataSource = dTable;
                ListView1.DataBind();
            }
            conn.Close();
        }
    }

No comments:

Post a Comment