简体   繁体   中英

How do you really add content to a .aspx.designer.cs file?

How do you really add content to a .aspx.designer.cs file?

I have a stepPayment.aspx file, I have a stepPayment.aspx.cs file, and I have a stepPayment.aspx.designer.cs file.

The .aspx.designer.cs file has a warning at the top of the file that suggests that nothing in the file should be edited by hand:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

Additionally, every variable in the file says it is it is an auto-generated field. Of course the designer field suggest I can add a new field in the code behind and this is what I have eventually done. Still, it would be good to know how to add content such that the auto-generated fields generate a new field that I want.

If you need to add info fast and secure in the designer file try this:

  1. Delete the the designer file from the page that you need fix.
  2. Right Click on the aspx page file.
  3. In the popup menu Click "Convert to web aplication".

with that you'll regenerate the designer file again with all controls.

I hope that helps.

Like Servy mentioned in the comments, you should never add anything to the designer explicitly. Basically, just don't open it at all, as it is just a means to give you a way to manipulate the controls added to the page in the aspx (or ascx in case of user controls).

Every time you save the markup file, the designer is updated to contain the controls you added to it that have an explicit id set. For other things that are not controls, you should create the fields/properties in the normal .cs file.

Sometimes it's good to know that you can indeed recreate the designer completely, by following Freak_Droid's advice. I've had to to that a few times because Visual Studio refused to update the designer file for some odd reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM