Alternative to using the OnLoad event in an ASP.Net MVC View?
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>
shivshankar
Senior code monkey. Love shiny things. Oh look, squirrel!
Updated on June 14, 2022Comments
-
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 about 15 yearsI 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 about 15 yearsThe 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 about 15 yearsYou should just af <form runat="server"></form> and then they should work?!
-
shivshankar about 15 yearsThat did it. I don't know why I didn't just think of this in the first place.
-
shivshankar about 15 yearsYes, 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 about 15 yearsThank 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