Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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