Thursday, 17 October 2013

All grid action server side events

Here i am going to show you simple asp grid all inline editing with sorting. Like CRUD operation in MVP structure.


.cs Page code


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex; BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = GridView1.Rows[e.RowIndex];
            CurrentUserData = new Entity.UserData();
            CurrentUserData.UserId = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Value);
            _presenterData.GetCurrentData();

            CurrentUserData.FirstName = ((TextBox)(row.Cells[1].FindControl("TextBox1"))).Text;
            CurrentUserData.LastName = ((TextBox)(row.Cells[2].FindControl("TextBox2"))).Text;
            CurrentUserData.CompanyName = ((TextBox)(row.Cells[3].FindControl("TextBox3"))).Text;
            CurrentUserData.ContactNo = Convert.ToDecimal(((TextBox)(row.Cells[4].FindControl("TextBox4"))).Text);
            CurrentUserData.Address = ((TextBox)(row.Cells[5].FindControl("TextBox5"))).Text;
            CurrentUserData.Country = ((TextBox)(row.Cells[6].FindControl("TextBox6"))).Text;
            CurrentUserData.DOB = Convert.ToDateTime(((TextBox)(row.Cells[7].FindControl("TextBox7"))).Text);

            FileUpload fp = ((FileUpload)row.Cells[7].FindControl("fup"));
            if (fp.HasFile)
            {
                fp.SaveAs(Server.MapPath("Temp/" + fp.FileName)); CurrentUserData.ImgUrl = fp.FileName;
            }

            _presenterData.Update();
            GridView1.EditIndex = -1;
            BindData();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1; BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CurrentUserData = new Entity.UserData();
            CurrentUserData.UserId = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Value);
            _presenterData.Delete(); BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                #region Old code

                _presenterData.GetAllData();

                if (lastSortExpression.Value == e.SortExpression.ToString())
                {
                    if (lastSortDirection.Value == SortDirection.Ascending.ToString())
                    {
                        e.SortDirection = SortDirection.Descending;
                    }
                    else
                    {
                        e.SortDirection = SortDirection.Ascending;
                    }
                    lastSortDirection.Value = e.SortDirection.ToString();
                    lastSortExpression.Value = e.SortExpression;
                }
                else
                {
                    lastSortExpression.Value = e.SortExpression;
                    e.SortDirection = SortDirection.Ascending;
                    lastSortDirection.Value = e.SortDirection.ToString();
                }


                switch (e.SortExpression)
                {
                    case "FirstName":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.FirstName).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.FirstName).ToList();
                            GridView1.DataBind();
                        }
                        break;

                    case "LastName":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.LastName).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.LastName).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "CompanyName":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.CompanyName).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.CompanyName).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "ContactNo":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.ContactNo).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.ContactNo).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "Address":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.Address).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.Address).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "Country":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.Country).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.Country).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    case "DOB":
                        if (e.SortDirection == SortDirection.Ascending)
                        {
                            GridView1.DataSource = UserDatas.OrderBy(x => x.DOB).ToList();
                            GridView1.DataBind();
                        }
                        else
                        {
                            GridView1.DataSource = UserDatas.OrderByDescending(x => x.DOB).ToList();
                            GridView1.DataBind();
                        }
                        break;
                    default:
                        break;
                }

                #endregion

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

       

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentUserData = new Entity.UserData();
                CurrentUserData.FirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
                CurrentUserData.LastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
                CurrentUserData.CompanyName = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;

                CurrentUserData.ContactNo = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtContact")).Text);
                CurrentUserData.Address = ((TextBox)GridView1.FooterRow.FindControl("txtAddress")).Text;
                CurrentUserData.Country = ((TextBox)GridView1.FooterRow.FindControl("txtCountry")).Text;
                CurrentUserData.DOB = Convert.ToDateTime(((TextBox)GridView1.FooterRow.FindControl("txtDOB")).Text);

                FileUpload fup = ((FileUpload)GridView1.FooterRow.FindControl("fupImg"));
                if (fup.HasFile)
                {
                    fup.SaveAs(Server.MapPath("Temp/" + fup.FileName)); CurrentUserData.ImgUrl = fup.FileName;
                }
                _presenterData.Add();

                BindData();

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

.aspx page
    Select
Name : <%# Eval("FirstName") %>   <%# Eval("LastName") %>
Company : <%# Eval("CompanyName") %>
 

Style
  .tooltip
        {
            position: relative; /* color: Black;*/
        }
        
        .tooltip span td
        {
            text-align: left;
        }
        .tooltip span
        {
            display: none;
        }
        .tooltip:hover span
        {
            display: block;
            position: absolute;
            color: Black;
            width: 200px;
            background-color: rgba(251,251,251,0.9);
            border: 1px solid #a2a2a2;
            z-index: 100;
            border-radius: 7px;
            box-shadow: 2px 1px 3px 2px #a2a2a2;
        }
        input[type="text"]
        {
            width: 100px;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 3px;
        }
        
        td
        {
            text-align: center;
        }


For custom paging in grid view
http://www.codeproject.com/Articles/16238/GridView-Custom-Paging
http://www.dotnetcurry.com/ShowArticle.aspx?ID=345 

No comments:

Post a Comment