Sharepoint List

1 posts, 0 answers
  1. Abu
    Abu avatar
    8 posts
    Member since:
    Oct 2011

    Posted 18 Jan 2012 Link to this post

    Hi,
    I have created one usercontrol in Visual studio it contains Employee data here i performed Add,Delete,update,actions and i loaded this usercontrol in sharepoint site but i want to show toolbar for this form which contains Save,Edit like default sharepoint custom list how can i do for this.
    here am sending my code please look at once

            protected void bt_add_Click1(object sender, EventArgs e)

            {

                Label6.Text = string.Empty;

                AddtoList();

                Label6.Visible = true;

            }

        

         

            protected void bt_view_Click(object sender, EventArgs e)

            {

                Gridview();

              

     

            }

            protected void bt_update_Click(object sender, EventArgs e)

            {

                Update();

                clear();

     

            }

            protected void btn_reset_Click(object sender, EventArgs e)

            {

                clearcontrols();

                Label6.Text = string.Empty;

               

            }

            protected void bt_delete_Click(object sender, EventArgs e)

            {

                Delete();

                txt_Item.Text = string.Empty;

            }

            protected void StuList_Grid_PageIndexChanged(object sender, GridPageChangedEventArgs e)

            {

                StuList_Grid.CurrentPageIndex = e.NewPageIndex;

                StuList_Grid.Visible = true;

                SPList taskList = SpListitems();

                DataTable tablelist = taskList.Items.GetDataTable();

                StuList_Grid.DataSource = tablelist;

                StuList_Grid.DataBind();

     

            }

            protected void Page_Load(object sender, EventArgs e)

            {

     

            }

          

            #endregion

            #region Methods

            //This method will display the sharepoint list items to Grid and

            //Show listitems in textboxes by id.

            private void Gridview()

            {

     

                if (txt_Item.Text == string.Empty)

                {

                    try

                    {

                        StuList_Grid.Visible = true;

                        Descending();

                        StuList_Grid.CurrentPageIndex = 0;

                        SPList taskList = SpListitems();

                        DataTable tablelist = taskList.Items.GetDataTable();

                        StuList_Grid.DataSource = tablelist;

                        StuList_Grid.DataBind();

                        Label6.Text = string.Empty;

                    }

                    catch (Exception ex)

                    {

                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),

                        "AlertScript", "(function(){var f = function(){alert('not viewed', 370,210);Sys.Application.remove_load(f) ;};Sys.Application.add_load(f) ;})();", true);

     

                    }

                }

                else

                {

                     StuList_Grid.Visible = false;

                        SPList taskList = SpListitems();

                        int id = Convert.ToInt32(txt_Item.Text);

                        SPListItem item = null;

                        try

                        {

                         

                            item = taskList.GetItemById(id);

                        }

                        catch (Exception ex)

                        {

                            Label6.Text = "Item doesnot Exist";

                        

                        }

                        if (item != null)

                        {

                            if (item["StuName"] != null)

                            {

                                txt_name.Text = item["StuName"].ToString();

                            }

                            else

                                txt_name.Text = string.Empty;

                            if (item["Department"] != null)

                            {

                                txt_dept.Text = item["Department"].ToString();

                            }

                            else

                                txt_dept.Text = string.Empty;

                            if (item["Location"] != null)

                            {

                                txt_location.Text = item["Location"].ToString();

                            }

                            else

                                txt_location.Text = string.Empty;

                            if (item["ContactNumber"] != null)

                            {

                                txt_number.Text = item["ContactNumber"].ToString();

                            }

                            else

                                txt_number.Text = string.Empty;

                            Label6.Text = string.Empty;

                        }

               

                }

               

                }

     

            //This will add textbox values to sharepoint list.

            private void AddtoList()

            {

                try

                {

                    SPList taskList = SpListitems();

                    SPListItem listitem = taskList.Items.Add();

                    listitem["StuName"] = txt_name.Text;

                    listitem["Department"] = txt_dept.Text;

                    listitem["Location"] = txt_location.Text;

                    listitem["ContactNumber"] = txt_number.Text;

                    listitem.Update();

                    Label6.Text  = "Records Saved";

                    clearcontrols();

                    Descending();

                    grid();

     

                }

                catch (Exception ex)

                {

                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),

                    "AlertScript", "(function(){var f = function(){alert('not inserted', 370,210);Sys.Application.remove_load(f) ;};Sys.Application.add_load(f) ;})();", true);

     

     

                }

            }

     

          

            //This will update the list items by id

            private void Update()

            {

                try

                {

                    if (txt_Item.Text == string.Empty)

                    {

                        Label6.Text = "ID value is Required";

                      

                    }

                  

                    SPList taskList = SpListitems();

     

                    SPListItem item = null;

                    int id = Convert.ToInt32(txt_Item.Text);

                    try

                    {

                        item = taskList.GetItemById(id);

                    }

                    catch (Exception ex)

                    {

                        Label6.Text = "Item doesnot Exist";

                       

                    }

                    if (item != null)

                    {

                        item["StuName"] = txt_name.Text;

                        item["Department"] = txt_dept.Text;

                        item["Location"] = txt_location.Text;

                        item["ContactNumber"] = txt_number.Text;

                        item.Update();

                        taskList.Update();

                        Label6.Text = "Updated";

                        grid();

                    }

                  

                }

     

                catch (Exception ex)

                {

     

                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),

                    "AlertScript", "(function(){var f = function(){alert('Not Updated', 370,210);Sys.Application.remove_load(f) ;};Sys.Application.add_load(f) ;})();", true);

     

                }

     

            }

            //This will delete the list items by id

            private void Delete()

            {

                try

                {

                    if (txt_Item.Text == string.Empty)

                    {

                        Label6.Text = "ID value is Required";

                       

                    }

                    else

                    {

                        SPList taskList = SpListitems();

     

                        SPListItem item = null;

                        int id = Convert.ToInt32(txt_Item.Text);

                        try

                        {

                            item = taskList.GetItemById(id);

                        }

                        catch (Exception ex)

                        {

                            Label6.Text = "Item Doesnot Exist";

                          

                        }

                        if (item != null)

                        {

                            item["StuName"] = txt_name.Text;

                            item["Department"] = txt_dept.Text;

                            item["Location"] = txt_location.Text;

                            item["ContactNumber"] = txt_number.Text;

                            item.Delete();

                            Label6.Text = "Deleted";

                            grid();

                        }

                    }

     

                }

     

                catch (Exception ex)

                {

     

                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),

                    "AlertScript", "(function(){var f = function(){alert('Not Deleted', 370,210);Sys.Application.remove_load(f) ;};Sys.Application.add_load(f) ;})();", true);

     

                }

     

            }

          

            //This will return value from method

            protected SPList SpListitems()

            {

                string strDashListRoot = "http://dev02:2000";

                SPSite osite = new SPSite(strDashListRoot);

                SPWeb oWeb = osite.OpenWeb();

                oWeb.AllowUnsafeUpdates = true;

                SPList taskList = oWeb.Lists["Name"];

                return taskList;

            }

           

            //This clear all textboxes and grid

            private void clearcontrols()

            {

                txt_name.Text = string.Empty;

                txt_dept.Text = string.Empty;

                txt_location.Text = string.Empty;

                txt_number.Text = string.Empty;

                txt_Item.Text = string.Empty;

                StuList_Grid.Visible = false;

            }

           

            //This clear all textboxes

            private void clear()

            {

                txt_name.Text = string.Empty;

                txt_dept.Text = string.Empty;

                txt_location.Text = string.Empty;

                txt_number.Text = string.Empty;

                txt_Item.Text = string.Empty;

            }

           

            //This to bind  the grid

            private void grid()

            {

                

                StuList_Grid.Visible = true;

                SPList taskList = SpListitems();

                StuList_Grid.CurrentPageIndex = 0;

                //StuList_Grid.CurrentPageIndex = StuList_Grid.PageCount - 1;

                DataTable tablelist = taskList.Items.GetDataTable();

                StuList_Grid.DataSource = tablelist;

                StuList_Grid.DataBind();

     

            }

           

            //This will display latest item first in grid id in descending order

            private void Descending()

            {

                GridSortExpression expression = new GridSortExpression();

                expression.FieldName = "ID";

                expression.SortOrder = GridSortOrder.Descending;

                StuList_Grid.MasterTableView.SortExpressions.AddSortExpression(expression);

            }

     

            #endregion

        }

    }

     



    Thanks&Regards
    P.Durga
Back to Top