1

This is driving me crazy, because I thought I had the runat=server stuff totally figured out.

I have an asp:Literal control in my markup (it's in a masterpage, but that doesn't matter, right?)

In MyMaster.Master:

<asp:Literal ID="myLiteral" runat="server" Text="Some Text"></asp:Literal>

Then, in the codebehind (MyMaster.Master.cs), I have:

myLiteral.Text = "Some different text";

No matter what I try, I get

The name 'myLiteral' does not exist in the current context

I've tried cleaning and rebuilding my whole solution, but it doesn't make any difference.

What am I doing wrong?

Edit: To clarify, I'm trying to change the text of the Literal control in the codebehind of the master page, not a content page. Specifically, I'm doing this in the Page_Load method of the master file.

Isaac Dontje Lindell
  • 3,246
  • 6
  • 24
  • 35
  • Where do you have that control in your Master page ASPX, is it inside some other control ? or do you have it as a *direct* child of your `form` in master control – Habib Feb 11 '15 at 16:20
  • It's inside a couple of normal HTML`div`s (not ASP controls), which are themselves within the `form`. Is that the problem? – Isaac Dontje Lindell Feb 11 '15 at 16:21
  • No that shouldn't be a problem. Something else is wrong. – Habib Feb 11 '15 at 16:22
  • Just try one thing, comment out `myLiteral` stuff in your project and try building it. See if it builds successfully, then try putting that back and build again. – Habib Feb 11 '15 at 16:23
  • It builds fine if I leave out the line that accesses `myLiteral`. As soon as I put it back, it fails to compile again. – Isaac Dontje Lindell Feb 11 '15 at 16:25
  • sounds like your designer is not updating or you have a namespacing issue. Can you verify the literal is created in your designer.cs file? – moarboilerplate Feb 11 '15 at 16:37
  • Huh. I don't seem to have a designer.cs file for that master page. Can I force Visual Studio to create one? – Isaac Dontje Lindell Feb 11 '15 at 16:52

2 Answers2

1

Figured it out! I wish I knew why, but the Masterfile was missing it's corresponding .designer.cs file. I tried several things to get VS to regenerate the designer file, but I finally found the trick in one of the answers to this question.

The trick is to create an empty code file with the correct name (in my case MyMaster.Master.designer.cs. Then open the masterfile (MyMaster.Master) and make some small change to cause VS to resave the file. When that happens, it magically fills in the empty designer file!

So, this problem is solved. However, if anyone knows why a) my designer file disappeared in the first place, and b) if there's any better way of forcing VS to regenerate the designer files if they happen to disappear, that would fantastic.

Community
  • 1
  • 1
Isaac Dontje Lindell
  • 3,246
  • 6
  • 24
  • 35
  • 1
    The "safest" way to do this would be to copy all of the code out of the master and codebehind file, delete and recreate the master page, and then copy the code back in. That way you know you're working with something VS created and is maintaining and there's less of a probability you have any extraneous issues with the master page still out there. – moarboilerplate Feb 11 '15 at 17:30
0

that is some problem of VS, i have witness such issues in VS 2010 when the controls were not forming. Whenever such things happens i try the following things - 1. Cleaning and rebuilding solution. 2. Dragging and dropping the contacts from toolbox which is not creating. 3. Restarting computer.

This thing happens only sometimes. I would recommend you to use VS 2013 as it is mostly bug free(if not 100%).

Thanks

yogihosting
  • 5,494
  • 8
  • 47
  • 80