473,398 Members | 2,343 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,398 software developers and data experts.

Dynamically adding DropDownList - if I set ID also, I lose PostBack EventHandler

I have built a control that runs an on-line help system. Depending on the
category you selected via dropdownlist, it goes out and gets the child
subcategories, and if there are any, adds a new dropdownlist to the screen
for selection. This continues until there are no children, and then it
checks for a help article list based on that last selection and displays
actual articles for display.

Adding the controls and getting everything displaying has worked fine so
far. the problem is when the user goes back and changes a past dropdown, i
need to account for what happens to the successive dropdowns related to that
parent.

What i decide was just to disable previously selected dropdowns, each time a
new one is added, and then add a link to "start-over" basically.

Unfortunately, i cant even loop through the dynamically added dropdownlists
on the page because if i set an ID property when they are added, i lose my
postback event!

Can anyone explain to me what is going on with that or any alternatives?

I even tried adding an "id" attribute to the dropdownlists, and then my
postback handler works again but i cannot do a page.findcontrol() on that
attribute value, because it really isn't the "ID" of it, it doesn't find it.

Thanks in advance.

- DotNetJunky


Nov 16 '05 #1
4 5443
Look at the javascript being run when your drop down initiates a
postback. One of the arguments in the unique id of the control. The
last part of the unique id must be the same as the id of the control.

Can you post the code you are using to create the control, add the
control to the page, set the id of the control and subscribe to the
event of the control?

With dynamically added controls, there are some issues that may arise
if the control isn't in the same index of the Controls collection. on
the postback. Add dynamic controls on the Init phase of the page
lifecycle. It's also a good practice to add the control to the page's
Controls collection before setting additional properties.

If you can post a stripped-down code sample that exibits the undesired
behavior, I'd be glad to assist you further.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #2
I am storing the categories posted back from the dropdowns into an arrayList
and then ViewStating the arrayList. On PostBack i check that arrayList and
re-run the routine which builds the dropDowns and adds them to the page with
their event handlers.

I am adding them manually to the control hierarchy, by simply adding a new
asp:table cell and then adding the control to it.

attached is sample test code (thrown together in an hour just to see if my
idea for this thing would even work). let me know what you think i may be
able to do to fix this. SOme of the code is remmed out just so it works for
you if you set it up to test. thanks,

DNJ

================================================== ======================

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Testing
{
/// <summary>
/// Summary description for TestDD.
/// </summary>
public class TestDD : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table tblCategory;
protected System.Web.UI.WebControls.Label lblArticleDate;
protected System.Web.UI.WebControls.Label lblArticleTitle;
protected System.Web.UI.WebControls.Label lblArticleContent;
protected System.Web.UI.WebControls.DropDownList ddList1;
protected System.Web.UI.WebControls.LinkButton lbRestart;
protected System.Web.UI.HtmlControls.HtmlForm Form1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
ddList1.DataSource = GetCategories(0);
ddList1.DataBind();

//add default item
ListItem _liDefault = new ListItem("Please select Category ->","");
ddList1.Items.Insert(0,_liDefault);
ddList1.Attributes.Add("id","ddList1");

//GenerateNewDropDown(Convert.ToInt64(ddList1.Select edValue));

System.Collections.ArrayList _CatList = new ArrayList();
System.Collections.ArrayList _PrevDDList = new ArrayList();

ViewState["CatList"] = _CatList;
ViewState["PrevDDList"] = _PrevDDList;
ViewState["ddListCount"] = 0;
}
else
{
System.Collections.ArrayList _CatList = (ArrayList)ViewState["CatList"];

for (int i = 0; i < _CatList.Count; i++)
{
GenerateNewDropDown(Convert.ToInt64(_CatList[i]));
}

//DisablePrevious(this.Page);
}
}

public DataTable GetCategories(long catid)
{
//Create Instance of Connection and Command Object
SqlConnection oConnection = new
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"));
SqlDataAdapter daItems = new
SqlDataAdapter("tPortal_Help_Category_sel_List", oConnection);
//Mark the Command as a SPROC
daItems.SelectCommand.CommandType = CommandType.StoredProcedure;
//Add paramaters to SPROC
SqlParameter paramParentID = new SqlParameter("@parent_id",
SqlDbType.BigInt);
paramParentID.Value = catid;
daItems.SelectCommand.Parameters.Add(paramParentID );
//Create the DataSet
DataSet dsItems = new DataSet();
//Fill DataSet
daItems.Fill(dsItems);
//Close Connection
daItems.SelectCommand.Connection.Close();

return dsItems.Tables[0];
}

public DataTable GetArticles(long catid)
{
//Create Instance of Connection and Command Object
SqlConnection oConnection = new
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"));
SqlDataAdapter daItems = new SqlDataAdapter("tPortal_Help_sel_List",
oConnection);
//Mark the Command as a SPROC
daItems.SelectCommand.CommandType = CommandType.StoredProcedure;
//Add paramaters to SPROC
SqlParameter paramParentID = new SqlParameter("@category_id",
SqlDbType.BigInt);
paramParentID.Value = catid;
daItems.SelectCommand.Parameters.Add(paramParentID );
//Create the DataSet
DataSet dsItems = new DataSet();
//Fill DataSet
daItems.Fill(dsItems);
//Close Connection
daItems.SelectCommand.Connection.Close();

return dsItems.Tables[0];
}

public DataTable GetArticle(long helpid)
{
//Create Instance of Connection and Command Object
SqlConnection oConnection = new
SqlConnection(ConfigurationSettings.AppSettings.Ge t("ConnectionString"));
SqlDataAdapter daItems = new SqlDataAdapter("tPortal_Help_sel_GetHelp",
oConnection);
//Mark the Command as a SPROC
daItems.SelectCommand.CommandType = CommandType.StoredProcedure;
//Add paramaters to SPROC
SqlParameter paramParentID = new SqlParameter("@help_id",
SqlDbType.BigInt);
paramParentID.Value = helpid;
daItems.SelectCommand.Parameters.Add(paramParentID );
//Create the DataSet
DataSet dsItems = new DataSet();
//Fill DataSet
daItems.Fill(dsItems);
//Close Connection
daItems.SelectCommand.Connection.Close();

return dsItems.Tables[0];
}
private void ddLists_SelectedIndexChanged(object sender, System.EventArgs
e)
{
DropDownList _ddList = (DropDownList)sender;

GenerateNewDropDown(Convert.ToInt64(_ddList.Select edValue));
}

private void ddArticles_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DropDownList _ddList = (DropDownList)sender;
if ( _ddList.SelectedIndex > 0 )
{
DataTable _dtArticle =
GetArticle(Convert.ToInt64(_ddList.SelectedValue)) ;
if (_dtArticle.Rows.Count > 0)
{
lblArticleDate.Text =
_dtArticle.Rows[0]["date_lastupdated"].ToString();
lblArticleTitle.Text = _dtArticle.Rows[0]["help_title"].ToString();
lblArticleContent.Text =
_dtArticle.Rows[0]["help_content"].ToString();
}
}
}

private void GenerateNewDropDown(long parentid)
{

DataTable _dtCats = GetCategories(parentid);
DataTable _dtArticleList = new DataTable();
DropDownList _NewDDList = new DropDownList();
_NewDDList.AutoPostBack = true;
_NewDDList.EnableViewState = true;
_NewDDList.SelectedIndexChanged += new
System.EventHandler(this.ddLists_SelectedIndexChan ged);
//_NewDDList.ID = "ddList" + Convert.ToString(CountDDLists(this.Page) +
1);
_NewDDList.Attributes.Add("id", "ddList" +
Convert.ToString(CountDDLists(this.Page) + 1));

if (_dtCats.Rows.Count > 0)
{

for (int i = 0; i < _dtCats.Rows.Count; i++)
{
ListItem _liNew = new
ListItem(_dtCats.Rows[i]["category_name"].ToString(),_dtCats.Rows[i]["id"].ToString());
_NewDDList.Items.Add(_liNew);
}

//add default item
ListItem _liDefault = new ListItem("Please select SubCategory ->","");
_NewDDList.Items.Insert(0, _liDefault);

TableCell _newCell = new TableCell();
int _newCellIndex = tblCategory.Rows[0].Cells.Add(_newCell);
tblCategory.Rows[0].Cells[_newCellIndex].Controls.Add(_NewDDList);
//GenerateNewDropDown(Convert.ToInt64(_NewDDList.Sel ectedValue));

System.Collections.ArrayList _CatList = (ArrayList)ViewState["CatList"];

if (!_CatList.Contains(parentid))
{
_CatList.Add(parentid);
}

}
else
{
//try and display the article list if no children
_dtArticleList = GetArticles(parentid);

if (_dtArticleList.Rows.Count > 0)
{
DropDownList _NewDDList2 = new DropDownList();
_NewDDList2.AutoPostBack = true;
_NewDDList2.EnableViewState = true;
_NewDDList2.SelectedIndexChanged += new
System.EventHandler(this.ddArticles_SelectedIndexC hanged);
_NewDDList2.Attributes.Add("id", "ddList" +
Convert.ToString(CountDDLists(this.Page) + 1));

for (int i = 0; i < _dtArticleList.Rows.Count; i++)
{
ListItem _liNew = new
ListItem(_dtArticleList.Rows[i]["help_title"].ToString(),_dtArticleList.Rows[i]["id"].ToString());
_NewDDList2.Items.Add(_liNew);
}

//add default item
ListItem _liDefault = new ListItem("Please select an Article ->","");
_NewDDList2.Items.Insert(0, _liDefault);

TableCell _newCell = new TableCell();
int _newCellIndex = tblCategory.Rows[0].Cells.Add(_newCell);
tblCategory.Rows[0].Cells[_newCellIndex].Controls.Add(_NewDDList2);
System.Collections.ArrayList _CatList =
(ArrayList)ViewState["CatList"];

if (!_CatList.Contains(parentid))
{
_CatList.Add(parentid);
}
}
}
}

private void DisablePrevious(Control parent)
{
StoreDDLists(parent);

System.Collections.ArrayList _PrevDDList =
(ArrayList)ViewState["PrevDDList"];

for (int i = 0; i <= _PrevDDList.Count - 1; i++)
{
DropDownList _ddList =
(DropDownList)Page.FindControl(_PrevDDList[i].ToString());
_ddList.Enabled = false;
}
}

private void StoreDDLists(Control parent)
{
System.Collections.ArrayList _PrevDDList =
(ArrayList)ViewState["PrevDDList"];

foreach (Control c in parent.Controls)
{
if
(c.GetType().FullName.Equals("System.Web.UI.WebCon trols.DropDownList"))
{
DropDownList _thisDD = (DropDownList)c;

if (!_PrevDDList.Contains(_thisDD.Attributes["id"].ToString()))
{
_PrevDDList.Add(_thisDD.Attributes["id"].ToString());
}
}
if (c.Controls.Count > 0)
{
StoreDDLists(c);
}
}
}
private int CountDDLists(Control parent)
{
foreach (Control c in parent.Controls)
{
if
(c.GetType().FullName.Equals("System.Web.UI.WebCon trols.DropDownList"))
{
ViewState["ddListCount"] = (int)ViewState["ddListCount"] + 1;
}
if (c.Controls.Count > 0)
{
CountDDLists(c);
}
}

return (int)ViewState["ddListCount"];
}

private void lbRestart_Click(object sender, System.EventArgs e)
{
ViewState["CatList"] = null;
Response.Redirect("TestDD.aspx");
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.lbRestart.Click += new System.EventHandler(this.lbRestart_Click);
this.ddList1.SelectedIndexChanged += new
System.EventHandler(this.ddLists_SelectedIndexChan ged);
}
#endregion
}
}

"Jeffrey Palermo, MCAD.Net" <je************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Look at the javascript being run when your drop down initiates a
postback. One of the arguments in the unique id of the control. The
last part of the unique id must be the same as the id of the control.

Can you post the code you are using to create the control, add the
control to the page, set the id of the control and subscribe to the
event of the control?

With dynamically added controls, there are some issues that may arise
if the control isn't in the same index of the Controls collection. on
the postback. Add dynamic controls on the Init phase of the page
lifecycle. It's also a good practice to add the control to the page's
Controls collection before setting additional properties.

If you can post a stripped-down code sample that exibits the undesired
behavior, I'd be glad to assist you further.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #3
DotNetJunky,
It looks like you are subscribing to the control's event before you are
setting the ID.

try reversing this.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #4
Just in that particular example of code it appeared that way, but i did in
fact try it both ways and still no luck.
"Jeffrey Palermo, MCAD.Net" <je************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
DotNetJunky,
It looks like you are subscribing to the control's event before you are
setting the ID.

try reversing this.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com

Nov 16 '05 #5

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

Similar topics

11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
2
by: Dimitris Pantazopoulos | last post by:
Ok, let's say I recreate the dynamically-added Dropdownlist controls of my web form even at postbacks. That means they are repopulated with values as well. (If I don't I won't be getting anything...
5
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
3
by: Ben Dewey | last post by:
Okay, so I have a base Page class called ArticlesPageBase. This base class has reads in an overridable ArticleId and Loads the data into a Panel object in the ContentPlaceHolder of the Master. ...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
5
by: Liquidtouch | last post by:
I'm not much of a HTML or Javascript programmer but have a little experience just hacking away at it. I am creating a HTML application and would like to be able to add or remove rows of a table....
2
by: mylog | last post by:
Hi I am having a problem of getting the value from the dynamically generated table and dropdownlist. What I am facing is, I have created a table in the aspx page and now I need to add values to...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
2
ndhamecha
by: ndhamecha | last post by:
hello frds.. pls can you give me urgent reply how to add values dynamically in dropdownlist as im new in using asp.net with c# please ...thank u in advance.
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.