ASP.NET issue when removing Control on Page Load

smaxildibia

New Member
I have following Index.aspx web form:\[code\]<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="ProblematicWebApplication.Index" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <asp:Label ID="dummyLabel" runat="server" Text="This is dummy label." /> <form id="form1" runat="server"> <div> <asp:DropDownList ID="intDropDownList" runat="server"/> <br /> <asp:Button Text="Submit" OnClick="OnSubmitClicked" runat="server"/> <br /> Selected value: <asp:Label ID="selectedValueLabel" runat="server" /> </div> </form></body></html>\[/code\]And following code behind Index.aspx.cs file:\[code\]using System;using System.Linq;namespace ProblematicWebApplication{ public partial class Index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.intDropDownList.DataSource = Enumerable.Range(0, 11).ToArray(); this.intDropDownList.DataBind(); } if (this.Request["remove-dummy"] != null) this.Controls.Remove(this.dummyLabel); } protected void OnSubmitClicked(object sender, EventArgs e) { this.selectedValueLabel.Text = this.intDropDownList.SelectedValue; } }}\[/code\]When I run my application with no remove-dummy parameter in query string and select some value from intDropDownList and click Submit button, selected value is accordingly presented in selectedValueLabel.But if I run my application with remove-dummy=1 parameter in query string, dummyLabel gets removed. Now when I select some value from intDropDownList and click Submit button, selected value is not correctly written to selectedValueLabel and all items from intDropDownList are removed.Can someone explain it to me why this is happening?Why removing unrelated dummyLabel control has influence on intDropDownList control?
 
Back
Top