0

I've created a user control web part and the code behind file contains the following:

using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

namespace AnnouncementSlider1.ContentSlider 
{
[ToolboxItemAttribute(false)]
public partial class ContentSlider : UserControl
{
    string Qry = " <Where><Geq><FieldRef Name='Expires' /><Value 
    IncludeTimeValue='FALSE' Type='DateTime'>{0}</Value></Geq></Where>"; 
    protected void Page_Load(object sender, EventArgs e) { 
        if (!IsPostBack) { DisplayAnnouncements(); 
        } 
    }       
    private void DisplayAnnouncements() { 
        SPSecurity.RunWithElevatedPrivileges(delegate() 
        { 
            using (SPSite site = new SPSite(SPContext.Current.Site.ID)) 
            { 
                using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) 
                { 
                    SPList list = web.Lists["Announcements"]; 
                    SPQuery query = new SPQuery(); 
                    query.RowLimit = 10; 
                    query.Query = string.Format(Qry, DateTime.Now.ToString("yyyy-MM-dd")); 
                    rep1.DataSource = list.GetItems(query).GetDataTable(); 
                    rep1.DataBind(); 
                } 
            } 
        }
        ); 
    }
}
}

The control has the following item:

    <asp:Repeater ID="rep1" runat="server">             

When I build the solution, I receive an error stating:

Error The name 'rep1' does not exist in the current context

How should I remedy this error?

  • Check to see if your .ascx file is valid ie all tags are closed properly. Otherwise are you getting any other errors on build? This is usually the result of something else wrong. – Ryan Erickson Mar 15 '13 at 21:52
  • All tags are closed in the ascx and no other build errors. – SidC Mar 15 '13 at 21:59
1

Stop using RWEP. It is a security risk that you do not need to take. Using the System Account token is sufficient for reading a list.

http://www.schaeflein.net/Pages/Impersonation-and-Elevation-of-Privilege.aspx

0

So, I noticed that the ascx was actually under the Control Template folder in the solution for some reason. I re-created the user control and ensured it was saved under the web part folder. This rectified the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.