Pregunta

I am trying to add controls to a page where previously it was read-only. The environment is:

  • SharePoint 2010 solution
  • Developed in Visual Studios 2010
  • This feature is on a webpart (AceAllocation30.cs)
  • The code is C#

Here is the code I added:

sb.Append("      <td width=\"75%\">"); lc3 = new LiteralControl(sb.ToString()); Controls.Add(lc3); TextBox JDHFTxt = new TextBox(); JDHFTxt.ID = "txtJDHF"; JDHFTxt.Enabled = true; JDHFTxt.Value = ConvertToAllocation(DrACE["SPRAmount"].ToString()); Controls.Add(JDHFTxt); sb = new StringBuilder();
sb.Append("</td>");

The above added code replaced this code:

sb.Append("             <td class=\"rowText\" width=\"75%\">" + ConvertToAllocation(DrACE["SPRAmount"].ToString()) + "</td>");

For a more complete reference, the full original segement of the above is this:

sb.Append("         <tr class=\"row2\">");
sb.Append("         <td class=\"rowTextLeft\" width=\"25%\">SPR/JDHF Allotment:</td>");
sb.Append("             <td class=\"rowText\" width=\"75%\">" + ConvertToAllocation(DrACE["SPRAmount"].ToString()) + "</td>");
sb.Append("         </tr>");

But when I deploy I get the following errors:

  • Error 35; 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?)

  • Error 32; The name 'lc3' does not exist in the current context

  • Error 33; The name 'lc3' does not exist in the current context

Here is the full page code:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
//Data Controls
using System.Runtime.InteropServices;
using System.Data;
using System.Collections.Specialized;
using System.Data.SqlClient;
using IDE_Utility.DBConnection;
using IDE_Utility.ErrorLogging;
using System.Text;


namespace GIK30.ACEAllocation30
{
    [ToolboxItemAttribute(false)]
    public class ACEAllocation30 : WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        private Label m_lblMsg;
        private Button m_NewAllocation_Button;
        private HttpResponse Response = HttpContext.Current.Response;

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "";
                }
                return _myProperty;
            }
            set
            {                                                                                                              
                _myProperty = value;
            }
        }

        public ACEAllocation30()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                SPSite site = SPContext.Current.Site;
                DataSet DsAllocation = new DataSet();
                DataView DvAllocation = null;
                DataSet DsACE = new DataSet();
                DataRow DrACE;
                int ACEID = 0;

                if (site != null)
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        try
                        {
                            int.TryParse(Page.Request["ACEID"], out ACEID);
                            int UserID = GIK20_CodeLibrary.GetUserID(SPContext.Current.Web);
                            StringCollection Access = GIK20_CodeLibrary.GetUserAccessForACE(UserID, ACEID);
                            bool IsACEAdmin = GIK20_CodeLibrary.IsUserACEAdmin(Access);
                            bool IsContracts = GIK20_CodeLibrary.IsUserAllocationSpecialist(Access);

                            // Make sure the ACE exists first
                            Boolean IsACE = false;
                            SqlParameter sqlParamACEID = new SqlParameter();
                            sqlParamACEID.ParameterName = "@aceid";
                            sqlParamACEID.DbType = DbType.Int32;
                            sqlParamACEID.Value = ACEID;

                            SqlParameter[] sqlParams = new SqlParameter[]
                            {
                                sqlParamACEID
                            };
                            try
                            {
                                DsACE = DBConnection.GetDataSet("getAceInfo", CommandType.StoredProcedure, sqlParams);
                            }
                            catch (Exception ex)
                            {
                                HandleException(ex);
                                IDEErrLogging.LogError(new IDEException("ACE Allocation: Validate ACE: Error on Stored Procedure: getAceInfo", ex), true, false, SPContext.Current.Web);
                            }
                            if (DsACE != null)
                            {
                                if (DsACE.Tables[0].Rows.Count == 1)
                                    IsACE = true;
                            }

                            if (IsACE)
                            {
                                base.CreateChildControls();
                                DrACE = DsACE.Tables[0].Rows[0];

                                //strSQL = "select * from ACE_Allocation where ACEID = " + ACEID.ToString();
                                try
                                {
                                    sqlParamACEID = new SqlParameter();
                                    sqlParamACEID.ParameterName = "@aceid";
                                    sqlParamACEID.DbType = DbType.Int32;
                                    sqlParamACEID.Value = ACEID;

                                    sqlParams = new SqlParameter[]
                                    {
                                        sqlParamACEID
                                    };
                                    DsAllocation = DBConnection.GetDataSet("getACeAllocation", CommandType.StoredProcedure, sqlParams);
                                    DvAllocation = DsAllocation.Tables[0].DefaultView;
                                    DvAllocation.Sort = "FY, AllocationType";
                                }
                                catch (Exception ex)
                                {
                                    HandleException(ex);
                                    IDEErrLogging.LogError(new IDEException("ACE Allocation: Error on ACE Allocation Stored Procedure: getACeAllocation", ex), true, false, SPContext.Current.Web);
                                }

                                // Check to see if edit controls are enabled
                                Boolean enableStatus = false;
                                if (IsACEAdmin || IsContracts)
                                    enableStatus = true;

                                //enableStatus = GIK20_CodeLibrary.CheckUserInGroup(myWeb, statusArray);

                                string AllocationAvail = "No";
                                if (DrACE["AllocationAVailable"].Equals(true))
                                {
                                    AllocationAvail = "Yes";
                                }


                                StringBuilder sb = new StringBuilder();
                                sb.Append("<div class=\"border\" style=\"width:700px\">");
                                sb.Append("  <div class=\"partTitle\" >");
                                sb.Append("     <table cellspacing=\"0\" cellpadding=\"0\" height=\"25px\" width=\"100%\">");
                                sb.Append("         <tr>");
                                sb.Append("             <td class=\"partTitleText\">ACE ALLOCATION</td>");
                                sb.Append("         </tr>");
                                sb.Append("     </table>");
                                sb.Append("  </div>");
                                sb.Append("  <div class=\"columnHeader\">");
                                sb.Append("     <table cellspacing=\"0\" cellpadding=\"0\" height=\"25px\" width=\"100%\">");
                                sb.Append("         <tr>");
                                sb.Append("             <td class=\"columnHeaderText\" style=\"margin-top:5px;\">Allocation Information</td>");
                                sb.Append("         </tr>");
                                sb.Append("     </table>");
                                sb.Append("  </div>");
                                sb.Append("  <div class=\"rows\">");
                                sb.Append("     <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
                                sb.Append("         <tr class=\"row1\">");
                                sb.Append("             <td class=\"rowTextLeft\" width=\"25%\">Allocation Available:</td>");
                                sb.Append("             <td class=\"rowText\" width=\"75%\">" + AllocationAvail + "</td>");
                                sb.Append("         </tr>");
                                sb.Append("         <tr class=\"row2\">");
                                sb.Append("             <td class=\"rowTextLeft\" width=\"25%\">SPR/JDHF Allotment:</td>");
                                sb.Append("             <td class=\"rowText\" width=\"75%\">" + ConvertToAllocation(DrACE["SPRAmount"].ToString()) + "</td>");
                                sb.Append("         </tr>");
                                sb.Append("         <tr class=\"row1\">");
                                sb.Append("             <td class=\"rowTextLeft\" width=\"25%\">SPR Source:</td>");
                                sb.Append("             <td class=\"rowText\" width=\"75%\">" + DrACE["SPRSource"] + "</td>");
                                sb.Append("         </tr>");
                                //sb.Append("         <tr class=\"row2\">");
                                //sb.Append("             <td class=\"rowTextLeft\" width=\"25%\">TEV:</td>");
                                //sb.Append("             <td class=\"rowText\" width=\"75%\">" + ConvertToAllocation(DrACE["EstVal"].ToString()) + "</td>");  //); + DrACE["SPRSource"] + "</td>");
                                //sb.Append("         </tr>");
                                sb.Append("         <tr class=\"row2\">");
                                sb.Append("             <td class=\"rowTextLeft\" width=\"25%\">Allocation Comments:</td>");
                                sb.Append("             <td class=\"rowText\" width=\"75%\">" + DrACE["AllocationComments"] + "</td>");
                                sb.Append("         </tr>");
                                sb.Append("     </table>");
                                sb.Append("  </div>");
                                sb.Append("  <div class=\"columnHeader\">");
                                sb.Append("     <table cellspacing=\"0\" cellpadding=\"0\" height=\"25px\" width=\"100%\">");
                                sb.Append("         <tr>");
                                sb.Append("             <td class=\"rowTextTitle\" width=\"21%\">Type</td>");
                                sb.Append("             <td class=\"rowTextTitle\" width=\"20%\">Source</td>");
                                sb.Append("             <td class=\"rowTextTitle\" width=\"20%\">FY</td>");
                                sb.Append("             <td class=\"rowTextTitle\" width=\"20%\">$ Amount</td>");
                                sb.Append("             <td width=\"19%\"></td>");
                                sb.Append("         </tr>");
                                sb.Append("     </table>");
                                sb.Append("  </div>");
                                sb.Append("  <div class=\"rows\">");
                                sb.Append("     <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");

                                if (DvAllocation != null)
                                {
                                    Boolean row1 = true;
                                    int i = 1;
                                    LiteralControl lc2 = new LiteralControl();

                                    foreach (DataRowView DvAllocationRow in DvAllocation)
                                    {
                                        if (row1)
                                        {
                                            sb.Append("         <tr class=\"row1\">");
                                            row1 = false;
                                        }
                                        else
                                        {
                                            sb.Append("         <tr class=\"row2\">");
                                            row1 = true;
                                        }


                                        sb.Append("             <td class=\"rowText\" width=\"21%\">" + DvAllocationRow["AllocationType"].ToString() + "</td>");
                                        sb.Append("             <td class=\"rowText\" width=\"20%\">" + DvAllocationRow["AllocationSourceName"].ToString() + "</td>");
                                        sb.Append("             <td class=\"rowText\" width=\"20%\">" + DvAllocationRow["FYTxt"].ToString() + "</td>");
                                        sb.Append("             <td class=\"rowText\" width=\"20%\">" + ConvertToAllocation(DvAllocationRow["Amount"].ToString()) + "</td>");
                                        string querystring = "?ACEID=" + DvAllocationRow["ACEID"].ToString() + "&ACEAllocationID=" + DvAllocationRow["ACE_AllocationID"].ToString();
                                        if (enableStatus)
                                        {
                                            m_NewAllocation_Button = new Button();
                                            m_NewAllocation_Button.Text = "Edit Allocation";
                                            //m_NewAllocation_Button.Style = "font-size:8pt;font-family:Verdana,sans-serif";
                                            //m_NewAllocation_Button.Width = 80;
                                            //m_NewAllocation_Button.Height = 21;
                                            m_NewAllocation_Button.CssClass = "plainButton";
                                            m_NewAllocation_Button.ID = "NewAllocationButton" + i;
                                            m_NewAllocation_Button.Attributes.Add("OnClick", "window.open('../_layouts/GIK30/EditAllocation.aspx" + querystring + "');");
                                            //string[] statusArray = new string[] { "Owner", "SiteAdmin" };
                                            m_NewAllocation_Button.Enabled = enableStatus;
                                            //m_NewAllocation_Button.Visible = enableStatus;

                                            sb.Append("             <td width=\"19%\" align=\"right\" style=\"padding-right:20px\">");
                                            lc2 = new LiteralControl(sb.ToString());
                                            Controls.Add(lc2);
                                            Controls.Add(m_NewAllocation_Button);
                                            sb = new StringBuilder();
                                            sb.Append("</td>");
                                        }
                                        else
                                            sb.Append("             <td width=\"19%\"></td>");
                                        //sb.Append("             <td width=\"19%\"><input TYPE=\"button\" VALUE=\"Edit Allocation\" style=\"font-size:8pt;font-family:Verdana,sans-serif\" />");
                                        sb.Append("             </td>");
                                        sb.Append("         </tr>");
                                        i++;
                                    }
                                }
                                else
                                {
                                    sb.Append("     <tr class=\"row1\"><td class=\"rowText\" width=\"100%\">Error Reading from Database</td></tr>");
                                }
                                sb.Append("     </table>");
                                sb.Append("  </div>");
                                sb.Append("  <table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:9px; margin-top:1px; margin-right:9px\">");
                                sb.Append("     <tr class=\"total\">");
                                sb.Append("         <td width=\"555px\" style=\"vertical-align:middle\">EV on Allotment: " + ConvertToAllocation(DrACE["EstVa"].ToString()) + "</td>");
                                sb.Append("         <td align=\"right\" style=\"padding-right:5px\">");
                                LiteralControl lc = new LiteralControl(sb.ToString());
                                Controls.Add(lc);

                                m_NewAllocation_Button = new Button();
                                m_NewAllocation_Button.Text = "Add New Allocation";
                                //m_NewAllocation_Button.Style = "font-size:8pt;font-family:Verdana,sans-serif";
                                //m_NewAllocation_Button.Width = 80;
                                //m_NewAllocation_Button.Height = 21;
                                m_NewAllocation_Button.CssClass = "plainButton";
                                m_NewAllocation_Button.ID = "NewAllocationButton";
                                m_NewAllocation_Button.Attributes.Add("OnClick", "window.open('../_layouts/GIK30/EditAllocation.aspx?ACEID=" + ACEID + "');");
                                //string[] statusArray = new string[] { "Owner", "SiteAdmin" };
                                m_NewAllocation_Button.Enabled = enableStatus;
                                //m_NewAllocation_Button.Visible = enableStatus;
                                Controls.Add(m_NewAllocation_Button);

                                sb = new StringBuilder();
                                sb.Append("</td>");
                                sb.Append("     </tr>");
                                sb.Append("  </table>");
                                sb.Append("</div>");
                                lc = new LiteralControl(sb.ToString());
                                Controls.Add(lc);
                            }
                        }
                        catch (Exception ex)
                        {
                            HandleException(ex);
                            IDEErrLogging.LogError(new IDEException("ACE Allocation: Error on ACE: " + ACEID.ToString(), ex), true, false, SPContext.Current.Web);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();
                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }

        protected void m_NewAllocation_Button_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("EditAllocation.aspx?ACEID=" + Page.Request["ACEID"]);
        }

        private string ConvertToAllocation(string stramount)
        {
            // Convert the real number to a $ format
            double amount = 0.0;
            if (!String.IsNullOrEmpty(stramount))
                amount = Convert.ToDouble(stramount);
            return string.Format("{0:C}", amount);
        }
    }
}

And here is a mockup of what the page looks like: enter image description here

¿Fue útil?

Solución

The issue was that I had not added my literal control before utilizing it. I added it just prior to building the table with the fields and controls:

LiteralControl lc3 = new LiteralControl();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top