WebForms and lies

time to read 12 min | 2203 words

I made the comment several times that I feel that WebForms lies to me,  Joe Young asks what do I mean by that. Here is a small reproduction that I just created, that demonstrate the problem. Put this in an ASP.Net page, and run, select another value, and see what you get.

<script runat="server">

       protected void Page_Load(object sender, EventArgs e)

       {

              Test.DataSource = new string[]{ "London", "Paris", "Tel Aviv" };

              DataBind();

       }

 

       protected void SelectedIndexChanged(object sender, EventArgs e)

       {

              string msg = string.Format(@"alert('What the contol say: {0}; What the request says {1}');",

                     Test.SelectedValue, Request[Test.ClientID]);

              ClientScript.RegisterStartupScript(GetType(),"blah",msg,true);

       }

</script>

 

<asp:DropDownList ID="Test" EnableViewState="false" AutoPostBack="true" AppendDataBoundItems="true"

       runat="server" OnSelectedIndexChanged="SelectedIndexChanged">

       <asp:ListItem>nothing selected</asp:ListItem>

</asp:DropDownList>

Before you would jump to explain to me why this is happening, assume that I have spent a bit of time on this issue, I understand what the page life cycle is and can figure out what happen when.

Nevertheless, as it stands, here is a control that flat out lies to me about its state. I can go and query the Request and get the correct result back. For more fun and games, remove the AppendDataBoundItems and see what happens then.

I run into this kind of things fairly often, from view state that arrive to the wrong control (and what fun it was to find that out) to control naming rules to 101 little things that if you don't get just right, would silently break the code.

I will try to post about the rest of Joe's comment (and Jeremy Boyd's post) shortly.