views:

175

answers:

4

Hi, I have a simple .ascx file

<%@ Control Language="C#"
    AutoEventWireup="true"
    CodeFile="Profile.ascx.cs"
    Inherits="UserControls_Profile" %>
<%@ Register Assembly="RadChart.Net2"
    Namespace="Telerik.WebControls"
    TagPrefix="radC" %>
<div id="testdiv" runat="server"></div>

in code behind:

protected void Page_Load(object sender, EventArgs e) {
  Chart();
}

private void Chart() {
  testdiv.Visible=false;
}

When I try to access the page I get the exception:

error CS0103: The name 'testdiv' does not exist in the current context

I have tried onload as well but same result!

+2  A: 

If you have an HTML div, you need to add a runat attribute like

  <div id="testDiv" runat="server">Hello world</div>

HTML elements like div's are not ASP.NET Controls though and mixing HTML "controls" with ASP.NET might be problematic. You may prefer to do something like

  <asp:panel id="myPanel" runat="server">Hello world</asp:panel>

Update: Also check Profile.ascx.designer.cs which should be auto-generated by Visual Studio to contain "protected HtmlControl testdiv". I assume that is missing from the partial class. I note you derive from UserControls_Profile which may be cause for confusion.

Dead account
A: 

One thing, testdiv is a asp.net control??? This control has to be declared as protected in your .cs file or in another file with partial class of this user control. In addition, testdiv must have runat="server" in ascx file. If you have those things you can access to your control.

jaloplo
A: 

This should work, as long as the <div> is not in a dynamic template, like a Repeater for example.

Check your page template. It should contain something like this:

<div id="testdiv" runat="server">
   ...
<div>

If your div is part of a template, you will need to use container.FindControl("testdiv").

Edit: Sometimes, Visual Studio isn't properly updating the Profile.ascx.designer.cs. Just resaving the ASCX file doesn't seem to work in that case either. Try adding a ASP.NET control with another name. That usually triggers regeneration of the code.

Thorarin
A: 

In the .cs, right-click above testdiv and select "Go to Definition". If no result is found, open the .ascx in design mode and save the file. This should generate the declaration of the testdiv control inside the ascx.

devio