Wednesday, April 3, 2013

SharePoint List Item Update Using GridView Row Command



 protected void gvRInProgress_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Label lblId = new Label();
            Label lbllistname = new Label();
            TextBox txtComments = new TextBox();

            using (SPSite oCurrentSite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb oCurrentWeb = oCurrentSite.OpenWeb())
                {
                    if (e.CommandName == "SendBack")
                    {
                        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                        lblId = (Label)row.FindControl("lblID");
                        lbllistname = (Label)row.FindControl("lblListName");
                        txtComments = (TextBox)row.FindControl("txtComments");

                        if (lbllistname.Text == "Service")
                        {
                            SPList listResource = oCurrentWeb.Lists["Service Request"];
                            SPListItem itemResource = listResource.GetItemById(Convert.ToInt32(lblId.Text));
                            if (itemResource["Comments"] != null)
                            {
                                if (itemResource["Comments History"] != null)
                                {
                                    itemResource["Comments History"] = itemResource["Comments History"] + "," + itemResource["Comments"];
                                }
                                else
                                {
                                    itemResource["Comments History"] = itemResource["Comments"];
                                }
                            }
                            itemResource["Comments"] = txtComments.Text;
                            itemResource["Status"] = "Pending";
                            itemResource.Update();
                        }                    
                       
                       
                        BindData("Inprogress");
                    }
                   
                    else if (e.CommandName == "ViewItem")
                    {
                        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                        lblId = (Label)row.FindControl("lblID");
                        lbllistname = (Label)row.FindControl("lblListName");
                        txtComments = (TextBox)row.FindControl("txtComments");

                        if (lbllistname.Text == "Service")
                        {                          
                            Response.Redirect(SPContext.Current.Web.Url + "/Lists/Service%20Request/Item/displayifs.aspx?List=cd22a145%2D1a55%2D45fe%2D9d34%2D338fae0b808b&ID=" + lblId.Text + "&Source=http%3A%2F%2Fsyncgdc2068%3A5002%2FLists%2FService%2520Request%2FAllItems%2Easpx&ContentTypeId=0x01004044E3E1408A6B4BA734CD344CB08108");                          
                        }                                            
                       


                    }
                }
            }

        }

No comments:

Post a Comment