开发者

The name xxx does not exist in the current context

开发者 https://www.devze.com 2023-04-11 21:19 出处:网络
I am using the login control of asp.net and the Fileuploader. When I leave the file uploader control outside the <LoggedInTemplate> it works but when I try to move the Fileuploader inside the &l

I am using the login control of asp.net and the Fileuploader. When I leave the file uploader control outside the <LoggedInTemplate> it works but when I try to move the Fileuploader inside the <LoggedInTemplate> I get "The name xxx does not exist in the current context" and it referes to the C#.

<LoggedInTemplate>
    <b>Login ok ! </b><br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        InsertCommand="INSERT INTO aspnet_PicturesHome(pUrl) VALUES (@pUrl)"
        SelectCommand="SELECT pID, pUrl FROM aspnet_PicturesHome">
            <InsertParameters>
                <asp:ControlParameter Name="pUrl" Type="String" ControlID="FileUpload1" PropertyName="FileName"/>
            </InsertParameters>
        </asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Label">Foto 开发者_JAVA百科upload: </asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
<br></br>
</LoggedInTemplate>


Main Edit: You can access controls nested in a LoginView like this:

FileUpload fu = LoginView1.FindControl("FileUpload1") as FileUpload;

If they are in the <LoggedInTemplate>, you will want to make sure they are in fact logged in (and vice-versa for accessing controls in <AnonymousTemplate>: they should not be logged in), otherwise it won't find the control and .FindControl() will return null.

Reference:
http://www.velocityreviews.com/forums/t109239-accessing-a-control-within-loginview.html

Edit per comment: in your Page_Load(), if initial request (not postback) check if user is admin, and if not, disable the upload controls

protected void Page_Load(object sender, EventArgs e){

if(!this.isPostBack)
{
  if(!Context.User.IsInRole("admin"))
  {
     //disable controls
  }
}
}


You might have created another web page or webform. or maybe you just might have renamed the public class for the webform. in the Global.cs, the control is registered or exists in the first webform you accidently created. delete and created paste your code and it will be fine

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号