473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Programmatic control creation not working correctly

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.

Sep 18 '06 #1
4 1626
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("OutputT ype")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

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

((CheckBox)WizardStep2.FindControl("Printing")).Ch eckedChanged += 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[i].ItemArray[1].ToString()));

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

// Control here
switch (ds.Tables[0].Rows[i].ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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>"));

}
}

Sep 18 '06 #2
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" <ji**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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("OutputT ype")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

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

((CheckBox)WizardStep2.FindControl("Printing")).Ch eckedChanged += 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[i].ItemArray[1].ToString()));

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

// Control here
switch (ds.Tables[0].Rows[i].ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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>"));

}
}

Sep 19 '06 #3
So define a Page_OnInit() method and put the creation in _there_?
bruce barker (sqlwork.com) wrote:
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" <ji**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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("OutputT ype")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

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

((CheckBox)WizardStep2.FindControl("Printing")).Ch eckedChanged += 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[i].ItemArray[1].ToString()));

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

// Control here
switch (ds.Tables[0].Rows[i].ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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>"));

}
}
Sep 19 '06 #4
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();
}

bruce barker (sqlwork.com) wrote:
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" <ji**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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("OutputT ype")).SelectedIndexChanged
+= new EventHandler(OutputType_SelectedIndexChanged);

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

((CheckBox)WizardStep2.FindControl("Printing")).Ch eckedChanged += 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[i].ItemArray[1].ToString()));

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

// Control here
switch (ds.Tables[0].Rows[i].ItemArray[2].ToString())
{
case "Text":
if
(ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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[i].ItemArray[1].ToString().Trim() + ":" +
ds.Tables[0].Rows[i].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>"));

}
}
Sep 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: Bernie Yaeger | last post by:
I'm trying to create a custom control that has 2 listviews, in view smallicon. I'm using these with code that makes them drag/drop controls, so what I am after is a control that does drag/drop...
0
by: Paul | last post by:
Hi! I have been wondering about a design issue for some time now and hope someone can help sort this one out for me. Say you have to create some webcontrols dynamically. Each controls creation...
6
by: Blaine Manyluk | last post by:
I have a very unusual request. I need to be able to generate reports and save them as TIF files, with full programmatic control. The application will provide the filenames. Each page of the...
4
by: Charles Zhang | last post by:
I created a custom server control whose rendering logic relies on session variables and cookies. It works just fine at run time. The problem is at the design time, because session variables and...
11
by: J055 | last post by:
Hi I have a dropdown control which is constructed in another dropdown control SelectedIndexChanged event protected void ddlParamType_SelectedIndexChanged(object sender, EventArgs e) { //...
11
by: Nick Gilbert | last post by:
Hi, How can I create a custom control which will wrap its content in a header and footer? eg: Is it possible to create a .NET user control which can surround other controls? eg:...
0
by: MasterOfTheDark | last post by:
This one's a hairy one to explain. What I've got is a class called "VistaGroupBox" that I put some nice-looking Vista-style gradients in the background and made to look like the ribbon categories...
9
by: Axxe | last post by:
I have searched high and low for cogent, well-explained coding to complete a project on which I have spent six months of work. I stumbled across something on this site that is close to what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.