Created
May 15, 2012 21:28
-
-
Save russau/2705292 to your computer and use it in GitHub Desktop.
Evil viewstate crash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DynamicControls.WebForm1" %> | |
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %> | |
<!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> | |
<form id="form1" runat="server"> | |
<div> | |
<asp:DropDownList ID="ddl" runat="server" AutoPostBack=true> | |
<asp:ListItem>qwerty</asp:ListItem> | |
<asp:ListItem>qabcwerty</asp:ListItem> | |
</asp:DropDownList> | |
<asp:Label ID="lbl" runat="server" /> | |
<asp:Button Text="go" runat="server"/> | |
<hr /> | |
<table id="tbl" runat="server"> | |
</table> | |
</div> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Web.UI.HtmlControls; | |
namespace DynamicControls | |
{ | |
public partial class WebForm1 : System.Web.UI.Page | |
{ | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (!IsPostBack) | |
{ | |
lbl.Text = "kkk"; | |
ViewState["def"] = "ghi"; | |
BuildDataEntry(); | |
} | |
} | |
protected override void LoadViewState(object savedState) | |
{ | |
base.LoadViewState(savedState); | |
BuildDataEntry(); | |
} | |
protected override object SaveViewState() | |
{ | |
object o = base.SaveViewState(); | |
return o; | |
} | |
void BuildDataEntry() | |
{ | |
string selected = Request.Form["ddl"] ?? ddl.SelectedValue as string; | |
foreach (var c in selected.ToCharArray()) | |
{ | |
var row = new HtmlTableRow(); | |
var cell = new HtmlTableCell(); | |
cell.InnerHtml = c.ToString(); | |
if (c != 't') | |
{ | |
TextBox textbox = new TextBox(); | |
textbox.ID = c + "txt"; | |
textbox.Text = c + "val"; | |
cell.Controls.Add(textbox); | |
} | |
else | |
{ | |
DropDownList dll2 = new DropDownList(); | |
dll2.ID = c + "ddl"; | |
dll2.DataSource = new string[] { "aaa", "bbb", "ccc" }; | |
dll2.DataBind(); | |
cell.Controls.Add(dll2); | |
// disabling a control must store something in the viewstate control tree | |
// that causes a crash when other controls are inserted above this one | |
// EnableViewState = false fixes the problem | |
//dll2.EnableViewState = false; | |
dll2.Enabled = false; | |
} | |
row.Cells.Add(cell); | |
tbl.Rows.Add(row); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment