Programmatic control creation not working correctly

J

Jimmy M

I've got a page that generates dropdowns and text boxes based on
database data. I have them all set to auto-postback because I'll be
using this with Atlas to make a more seamless user experience. I've got
the Atlas/AJAX turned off for now.

I've got a page with a Wizard control. On step three of the wizard is
an empty panel. In my Page_Init() I'm calling a method which grabs the
panel and creates dropdowns, populates them with data, and adds them to
the Panel.

When I select an option on the dropdown and do a postback, two things
dont happen:

a) The SelectedIndexChanged event does not fire that I have wired to
the objects. I am wiring them up in the method that's called by
Page_Init.

b) The viewstate info does not persist on postback so that the value it
had before the postback is now show after - it's like everything is
reset every time to blank (for textboxes) or 0 (for dropdownlists).

I've also tried calling a method to set the values explicitly from the
viewstate - I'm calling this in Page_Load(). It seems the data isn't
getting set in the viewstate to be loaded.

I'm at my wits end. I've been banging on this for close to 8 hours.
Anyone have any recommendations? I've searched, and searched, and
searched and still no results that seem to fit this situation.
 
J

Jimmy M

Just a little more info with code:

protected void Page_Load(object sender, EventArgs e)
{
if (Security.isLoggedIn())
{
Wizard1.ActiveStepChanged += new
EventHandler(Wizard1_ActiveStepChanged);

((RadioButtonList)WizardStep2.FindControl("OutputType")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

((DropDownList)WizardStep1.FindControl("TemplateList")).SelectedIndexChanged
+= new EventHandler(TemplateList_SelectedIndexChanged);

((CheckBox)WizardStep2.FindControl("Printing")).CheckedChanged += new
EventHandler(Printing_CheckedChanged);

if (Page.IsPostBack)
{
if (Wizard1.ActiveStep.Equals(WizardStep3))
{
//setDetailsViewstateValues();
}
}
}

}
protected void Page_Init()
{
ErrorMessage = (Label)Master.FindControl("ErrorMessage");
if (Session["OrderID"] == null)
{
_order = new Order();
Session["OrderID"] = _order.OrderID + "";
}
else
{
_order = new
Order(Int32.Parse(Session["OrderID"].ToString()));
}

genDetailsContent();
//setDetailsValues();
}

private void genDetailsContent()
{
DataSet ds = SQLData.SELECT(@"...SQL...");

Panel details =
((Panel)WizardStep3.FindControl("DetailsPanel"));

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
details.Controls.Add(new LiteralControl("<tr><td>"));
// Name/prompt here
details.Controls.Add(new
LiteralControl(ds.Tables[0].Rows.ItemArray[1].ToString()));

details.Controls.Add(new LiteralControl("</td><td>"));

// Control here
switch (ds.Tables[0].Rows.ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows.ItemArray[3].ToString().Equals("True"))
{
DropDownList ddlText = new DropDownList();
ddlText.AutoPostBack = true;
//ddlText.EnableViewState = true;
ddlText.SelectedIndexChanged += new
EventHandler(ddlText_SelectedIndexChanged);
ddlText.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet textOptions =
SQLData.SELECT(@"...SQL...");

ddlText.DataSource = textOptions;
ddlText.DataTextField = "TextValue";
ddlText.DataValueField = "ID";
ddlText.DataBind();

details.Controls.Add(ddlText);
}
else
{
TextBox t = new TextBox();
t.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();
t.AutoPostBack = true;
t.TextChanged += new
EventHandler(t_TextChanged);
//t.EnableViewState = true;
details.Controls.Add(t);
}
break;

case "Image":
DropDownList ddlImage = new DropDownList();
ddlImage.AutoPostBack = true;
//ddlImage.EnableViewState = true;
ddlImage.SelectedIndexChanged += new
EventHandler(ddlImage_SelectedIndexChanged);
ddlImage.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet imageOptions =
SQLData.SELECT(@"...SQL...");

ddlImage.DataSource = imageOptions;
ddlImage.DataTextField = "TextValue";
ddlImage.DataValueField = "ID";
ddlImage.DataBind();
details.Controls.Add(ddlImage);
break;
}
details.Controls.Add(new LiteralControl("</td></tr>"));

}
}
 
B

bruce barker \(sqlwork.com\)

you need to recreate the controls on postback during oninit i you want them
to load their postback data, and fire events.

-- bruce (sqwork.com)

Jimmy M said:
Just a little more info with code:

protected void Page_Load(object sender, EventArgs e)
{
if (Security.isLoggedIn())
{
Wizard1.ActiveStepChanged += new
EventHandler(Wizard1_ActiveStepChanged);

((RadioButtonList)WizardStep2.FindControl("OutputType")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

((DropDownList)WizardStep1.FindControl("TemplateList")).SelectedIndexChanged
+= new EventHandler(TemplateList_SelectedIndexChanged);

((CheckBox)WizardStep2.FindControl("Printing")).CheckedChanged += new
EventHandler(Printing_CheckedChanged);

if (Page.IsPostBack)
{
if (Wizard1.ActiveStep.Equals(WizardStep3))
{
//setDetailsViewstateValues();
}
}
}

}
protected void Page_Init()
{
ErrorMessage = (Label)Master.FindControl("ErrorMessage");
if (Session["OrderID"] == null)
{
_order = new Order();
Session["OrderID"] = _order.OrderID + "";
}
else
{
_order = new
Order(Int32.Parse(Session["OrderID"].ToString()));
}

genDetailsContent();
//setDetailsValues();
}

private void genDetailsContent()
{
DataSet ds = SQLData.SELECT(@"...SQL...");

Panel details =
((Panel)WizardStep3.FindControl("DetailsPanel"));

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
details.Controls.Add(new LiteralControl("<tr><td>"));
// Name/prompt here
details.Controls.Add(new
LiteralControl(ds.Tables[0].Rows.ItemArray[1].ToString()));

details.Controls.Add(new LiteralControl("</td><td>"));

// Control here
switch (ds.Tables[0].Rows.ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows.ItemArray[3].ToString().Equals("True"))
{
DropDownList ddlText = new DropDownList();
ddlText.AutoPostBack = true;
//ddlText.EnableViewState = true;
ddlText.SelectedIndexChanged += new
EventHandler(ddlText_SelectedIndexChanged);
ddlText.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet textOptions =
SQLData.SELECT(@"...SQL...");

ddlText.DataSource = textOptions;
ddlText.DataTextField = "TextValue";
ddlText.DataValueField = "ID";
ddlText.DataBind();

details.Controls.Add(ddlText);
}
else
{
TextBox t = new TextBox();
t.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();
t.AutoPostBack = true;
t.TextChanged += new
EventHandler(t_TextChanged);
//t.EnableViewState = true;
details.Controls.Add(t);
}
break;

case "Image":
DropDownList ddlImage = new DropDownList();
ddlImage.AutoPostBack = true;
//ddlImage.EnableViewState = true;
ddlImage.SelectedIndexChanged += new
EventHandler(ddlImage_SelectedIndexChanged);
ddlImage.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet imageOptions =
SQLData.SELECT(@"...SQL...");

ddlImage.DataSource = imageOptions;
ddlImage.DataTextField = "TextValue";
ddlImage.DataValueField = "ID";
ddlImage.DataBind();
details.Controls.Add(ddlImage);
break;
}
details.Controls.Add(new LiteralControl("</td></tr>"));

}
}
 
J

Jimmy M

So define a Page_OnInit() method and put the creation in _there_?

you need to recreate the controls on postback during oninit i you want them
to load their postback data, and fire events.

-- bruce (sqwork.com)

Jimmy M said:
Just a little more info with code:

protected void Page_Load(object sender, EventArgs e)
{
if (Security.isLoggedIn())
{
Wizard1.ActiveStepChanged += new
EventHandler(Wizard1_ActiveStepChanged);

((RadioButtonList)WizardStep2.FindControl("OutputType")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

((DropDownList)WizardStep1.FindControl("TemplateList")).SelectedIndexChanged
+= new EventHandler(TemplateList_SelectedIndexChanged);

((CheckBox)WizardStep2.FindControl("Printing")).CheckedChanged += new
EventHandler(Printing_CheckedChanged);

if (Page.IsPostBack)
{
if (Wizard1.ActiveStep.Equals(WizardStep3))
{
//setDetailsViewstateValues();
}
}
}

}
protected void Page_Init()
{
ErrorMessage = (Label)Master.FindControl("ErrorMessage");
if (Session["OrderID"] == null)
{
_order = new Order();
Session["OrderID"] = _order.OrderID + "";
}
else
{
_order = new
Order(Int32.Parse(Session["OrderID"].ToString()));
}

genDetailsContent();
//setDetailsValues();
}

private void genDetailsContent()
{
DataSet ds = SQLData.SELECT(@"...SQL...");

Panel details =
((Panel)WizardStep3.FindControl("DetailsPanel"));

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
details.Controls.Add(new LiteralControl("<tr><td>"));
// Name/prompt here
details.Controls.Add(new
LiteralControl(ds.Tables[0].Rows.ItemArray[1].ToString()));

details.Controls.Add(new LiteralControl("</td><td>"));

// Control here
switch (ds.Tables[0].Rows.ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows.ItemArray[3].ToString().Equals("True"))
{
DropDownList ddlText = new DropDownList();
ddlText.AutoPostBack = true;
//ddlText.EnableViewState = true;
ddlText.SelectedIndexChanged += new
EventHandler(ddlText_SelectedIndexChanged);
ddlText.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet textOptions =
SQLData.SELECT(@"...SQL...");

ddlText.DataSource = textOptions;
ddlText.DataTextField = "TextValue";
ddlText.DataValueField = "ID";
ddlText.DataBind();

details.Controls.Add(ddlText);
}
else
{
TextBox t = new TextBox();
t.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();
t.AutoPostBack = true;
t.TextChanged += new
EventHandler(t_TextChanged);
//t.EnableViewState = true;
details.Controls.Add(t);
}
break;

case "Image":
DropDownList ddlImage = new DropDownList();
ddlImage.AutoPostBack = true;
//ddlImage.EnableViewState = true;
ddlImage.SelectedIndexChanged += new
EventHandler(ddlImage_SelectedIndexChanged);
ddlImage.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet imageOptions =
SQLData.SELECT(@"...SQL...");

ddlImage.DataSource = imageOptions;
ddlImage.DataTextField = "TextValue";
ddlImage.DataValueField = "ID";
ddlImage.DataBind();
details.Controls.Add(ddlImage);
break;
}
details.Controls.Add(new LiteralControl("</td></tr>"));

}
}
 
J

Jimmy M

This didn't accomplish anything - now, the controls don't even show up:

protected void Page_Init()
{
if (Session["OrderID"] == null)
{
_order = new Order();
Session["OrderID"] = _order.OrderID + "";
}
else
{
_order = new
Order(Int32.Parse(Session["OrderID"].ToString()));
}
}

protected void Page_OnInit(EventArgs e)
{
genDetailsContent();
}
you need to recreate the controls on postback during oninit i you want them
to load their postback data, and fire events.

-- bruce (sqwork.com)

Jimmy M said:
Just a little more info with code:

protected void Page_Load(object sender, EventArgs e)
{
if (Security.isLoggedIn())
{
Wizard1.ActiveStepChanged += new
EventHandler(Wizard1_ActiveStepChanged);

((RadioButtonList)WizardStep2.FindControl("OutputType")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

((DropDownList)WizardStep1.FindControl("TemplateList")).SelectedIndexChanged
+= new EventHandler(TemplateList_SelectedIndexChanged);

((CheckBox)WizardStep2.FindControl("Printing")).CheckedChanged += new
EventHandler(Printing_CheckedChanged);

if (Page.IsPostBack)
{
if (Wizard1.ActiveStep.Equals(WizardStep3))
{
//setDetailsViewstateValues();
}
}
}

}
protected void Page_Init()
{
ErrorMessage = (Label)Master.FindControl("ErrorMessage");
if (Session["OrderID"] == null)
{
_order = new Order();
Session["OrderID"] = _order.OrderID + "";
}
else
{
_order = new
Order(Int32.Parse(Session["OrderID"].ToString()));
}

genDetailsContent();
//setDetailsValues();
}

private void genDetailsContent()
{
DataSet ds = SQLData.SELECT(@"...SQL...");

Panel details =
((Panel)WizardStep3.FindControl("DetailsPanel"));

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
details.Controls.Add(new LiteralControl("<tr><td>"));
// Name/prompt here
details.Controls.Add(new
LiteralControl(ds.Tables[0].Rows.ItemArray[1].ToString()));

details.Controls.Add(new LiteralControl("</td><td>"));

// Control here
switch (ds.Tables[0].Rows.ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows.ItemArray[3].ToString().Equals("True"))
{
DropDownList ddlText = new DropDownList();
ddlText.AutoPostBack = true;
//ddlText.EnableViewState = true;
ddlText.SelectedIndexChanged += new
EventHandler(ddlText_SelectedIndexChanged);
ddlText.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet textOptions =
SQLData.SELECT(@"...SQL...");

ddlText.DataSource = textOptions;
ddlText.DataTextField = "TextValue";
ddlText.DataValueField = "ID";
ddlText.DataBind();

details.Controls.Add(ddlText);
}
else
{
TextBox t = new TextBox();
t.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();
t.AutoPostBack = true;
t.TextChanged += new
EventHandler(t_TextChanged);
//t.EnableViewState = true;
details.Controls.Add(t);
}
break;

case "Image":
DropDownList ddlImage = new DropDownList();
ddlImage.AutoPostBack = true;
//ddlImage.EnableViewState = true;
ddlImage.SelectedIndexChanged += new
EventHandler(ddlImage_SelectedIndexChanged);
ddlImage.ID =
ds.Tables[0].Rows.ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows.ItemArray[0].ToString();

DataSet imageOptions =
SQLData.SELECT(@"...SQL...");

ddlImage.DataSource = imageOptions;
ddlImage.DataTextField = "TextValue";
ddlImage.DataValueField = "ID";
ddlImage.DataBind();
details.Controls.Add(ddlImage);
break;
}
details.Controls.Add(new LiteralControl("</td></tr>"));

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top