So I haven’t used ASP.NET for three years or so and I’m really rusty on it. My old code isn’t available to me for review (at an old company). This question should be pretty basic, but I can’t find any good or reliable or not-super-old resources on the issue, so I’m asking here.
Can I get a general overview of databinding again? I remember it being really useful for select boxes, etc., but I don’t really remember how it works. Maybe a good ASP.NET tutorial in general, because I don’t remember how it handles POST requests or anything like that really either. Should I just try ASP.NET MVC?
Relatedly, suppose I have a public variable in my codebehind page. Right now I am accessing it by saying Page.DataBind() at the end of the page load function and then running <%# variable %> in the ASPX, but that’s not how I remember doing it before and I reckon that it’s not very good practice. What’s the best way to display variables from codebehind?
Databinding in general (at least in the WebForms model) is mostly a case of assigning fields to be displayed, setting the DataSource property to a suitable object that contains those fields e.g. a DataReader, DataTable, a Collection, and calling the DataBind method. So for your
selectcase, you’d put an<asp:dropdownlist runat="server" id="MyDropDownList">in the markup for the page, and then in the codeOr you can avoid writing that kind of code and do it in the markup if you use a DataSource control e.g.
<asp:SqlDataSource>,<asp:ObjectDataSource>For putting your variable on a page, the way you might have done it before is to have a label or textbox on the page, that in your code-behind you assign your variable to the Text property e.g.
Postbacks: you can test the
IsPostbackproperty of a page in code-behind to determine if it’s a postback or not. After the Page_Load method, other methods will fire if you’ve defined them e.g. SelectedIndexChanged for a DropDownList.