Alternative to using the OnLoad event in an ASP.Net MVC View?

10,114

The simplest way without creating a codebehind file is to add this anywhere on your .aspx page:

<script runat="server">
  private void Page_Load(object sender, System.EventArgs e)
  {
     //Initialize your control.
  }
</script>
Share:
10,114
shivshankar
Author by

shivshankar

Senior code monkey. Love shiny things. Oh look, squirrel!

Updated on June 14, 2022

Comments

  • shivshankar
    shivshankar almost 2 years

    I'm creating a simple test application so I can see how ASP.Net MVC works for what we deal with on a daily basis. We use 3rd party controls from Developer's Express, and we will continue to use them. If they absolutely do not work in ASP.Net MVC, then we will not use ASP.Net MVC.

    With that said, someone has found a way to make it work. He just had to put some code in the code behind. He had to override the OnLoad event and simply just put grid.DataSource = model and grid.DataBind() and it works as expected.

    In the ASP.Net MVC RC, code behind files were eliminated. I know I could put them back in... However, since people generally say that code behind files are evil, how else could you accomplish this?

  • shivshankar
    shivshankar about 15 years
    I tried this but it doesn't work... I can step through it and see that the model does have the data in it and the grid is set up properly, it's just saying that there is no data to show.
  • shivshankar
    shivshankar about 15 years
    The example that I mentioned in my question was this same grid control. He had it working, but he had to override the OnLoad event in the code behind. I'm trying to find a way to do it without creating a code behind file. I can turn the viewstate and postback functionality off.
  • veggerby
    veggerby about 15 years
    You should just af <form runat="server"></form> and then they should work?!
  • shivshankar
    shivshankar about 15 years
    That did it. I don't know why I didn't just think of this in the first place.
  • shivshankar
    shivshankar about 15 years
    Yes, that's the person I was talking about that got it to work. He had to use code behind files, like I explained.
  • Aaron Palmer
    Aaron Palmer about 15 years
    Thank you. I had to do this exactly to make my Active Report correctly generate and bind to my WebViewer control. re: stackoverflow.com/questions/668328