0

I do not manage to access a div element from code behind in ASP.NET:

Inside my aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContactForm.aspx.cs" Inherits="Form_Mailer_ContactForm" %>
...
<div  runat="server" id="SentFormDiv">
    <s>my content</s>
</div>

Inside the corresponding .cs file (ContactForm.aspx):

protected void Page_Load(object sender, EventArgs e) 
{
    this.SentFormDiv.Visible=false; 
}

I get the usual compilation error message:

does not contain a definition for 'SentFormDiv' and no extension method 'SentFormDiv' accepting a first argument of type 'xxx.SentFormDiv' could be found (are you missing a using directive or an assembly reference?)

The odd part is that is I start typing SentFor into the code behind, the Intellisense does suggest me SentFormDiv!!

What did I miss?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
A.D.
  • 1,062
  • 1
  • 13
  • 37
  • Try give it a name attribute. – Paulo Junior Nov 08 '16 at 14:51
  • @PauloJunior That has nothing to do with it. – mason Nov 08 '16 at 14:53
  • I guess your `Div` is inside another control and if so you need to use the `FindControl`. Like this http://stackoverflow.com/a/40025036/2946329 – Salah Akbari Nov 08 '16 at 14:55
  • @ S.Akbari : was a good idea but it is not my problem. My div was not inside an asp control. Plus, if I make the same test with another dif that I place right after the tag, the problem persists. – A.D. Nov 08 '16 at 15:26

1 Answers1

1

Please reference this SO question for help. Without seeing your full code, there is no way to give you a definitive answer. Also, as an aside, this.SentFormDiv can be simplified to SentFormDiv.

Community
  • 1
  • 1
Ethilium
  • 1,224
  • 1
  • 14
  • 20
  • Yeaaah! :) One of the suggestions from this post made the trick. That was not the main answer but this one: Problem is you might have multiple aspx files with codefile in page directive points to same codebehind file. It expects the same control to exists in all the aspx file linked to same code behind and thus throwing compilation error. Thx a ton Ethilium! – A.D. Nov 08 '16 at 15:32
  • Glad to help A.D. I've run into this issue myself (as I'm sure many have as well). – Ethilium Nov 08 '16 at 15:38