473,778 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding EventHandler to DropDownList programically

Hello.
Im trying to add new EventHandler to DropDownList programically. Im
doing something like this:

DropDownList ddlCate = new DropDownList();
ddlCate.ID = String.Format(" ddlCategory{0}" , i++);
ddlCate.DataSou rce = tblSub1;
ddlCate.DataTex tField =
tblSub1.Columns["categories_nam e"].ColumnName;
ddlCate.DataVal ueField =
tblSub1.Columns["categories _id"].ColumnName;
Panel1.Controls .Add(ddlCate);
ddlCate.DataBin d();
ddlCate.AutoPos tBack = true;
ddlCate.Selecte dIndexChanged += new
System.EventHan dler(BindCatego ries);

This is BindCategories function:

void BindCategories( object sender, EventArgs e)
{
Response.Write( "Event raised!");

}

but when Im changing value in ddlCate BindCategories are not raised.
Any help?

thanks in advandce
Darek

Sep 7 '06 #1
2 1818
My guess s that you have your code in a if (!Page.IsPostBa ck){}

since events happen on postback, they need to be setup during postback.
Normally you'd just place the hook during init...

onInit(object source, EventArgs e)
{
ddlCate.Selecte dIndexChanged +=....
}

since it's dynamic, simply put it out side the !Page.IsPostbac k

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"dtarczynsk i" <dt*********@gm ail.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
Hello.
Im trying to add new EventHandler to DropDownList programically. Im
doing something like this:

DropDownList ddlCate = new DropDownList();
ddlCate.ID = String.Format(" ddlCategory{0}" , i++);
ddlCate.DataSou rce = tblSub1;
ddlCate.DataTex tField =
tblSub1.Columns["categories_nam e"].ColumnName;
ddlCate.DataVal ueField =
tblSub1.Columns["categories _id"].ColumnName;
Panel1.Controls .Add(ddlCate);
ddlCate.DataBin d();
ddlCate.AutoPos tBack = true;
ddlCate.Selecte dIndexChanged += new
System.EventHan dler(BindCatego ries);

This is BindCategories function:

void BindCategories( object sender, EventArgs e)
{
Response.Write( "Event raised!");

}

but when Im changing value in ddlCate BindCategories are not raised.
Any help?

thanks in advandce
Darek

Sep 7 '06 #2
Thanks for respose, but mayby you can answer me in more detail. Look
Karl. I have one DropDownList ddlCategories (I create it with
designer), then I have added code for SelectedItemInd exChanged. I have
to create another DropDownList when ddlCategories
SelectedItemInd exChanged is raised. All my code looks like this:

protected void Page_Load(objec t sender, EventArgs e)
{
}

protected void ddlCategories_S electedIndexCha nged(object sender,
EventArgs e)
{

if (...)
{
int i = 0;
DropDownList ddlCate = new DropDownList();
ddlCate.ID = String.Format(" ddlCategory{0}" , i++);
ddlCate.DataSou rce = tblSub1;
ddlCate.DataTex tField =
tblSub1.Columns["categories_nam e"].ColumnName;
ddlCate.DataVal ueField =
tblSub1.Columns["categories _id"].ColumnName;
pnlCat.Controls .Add(ddlCate);
ddlCate.DataBin d();
ddlCate.AutoPos tBack = true;
ddlCate.Selecte dIndexChanged += new
System.EventHan dler(BindCatego ries);
}
}

void BindCategories( object sender, EventArgs e)
{
Response.Write( "Event raised!");

}

Can you answer me, how to properly add this event handler please?

Karl Seguin [MVP] napisal(a):
My guess s that you have your code in a if (!Page.IsPostBa ck){}

since events happen on postback, they need to be setup during postback.
Normally you'd just place the hook during init...

onInit(object source, EventArgs e)
{
ddlCate.Selecte dIndexChanged +=....
}

since it's dynamic, simply put it out side the !Page.IsPostbac k

Karl
Sep 7 '06 #3

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

Similar topics

4
5482
by: DotNetJunky | last post by:
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...
0
353
by: mrwoopey | last post by:
I did not find code for adding an event to dropdownlist in a repeater control posted anywhere. So, I am posting it for the next person (sorry if this seems obvious to you): '''''''''''''''''''' Private Sub MyRepeater_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles MyRepeater.ItemCreated Dim strValue As String
6
16619
by: Oscar | last post by:
I want to add items to a dropdownlist control within a Javascript eventhandler. This is what I code : var dd = document.getElementById("DropDownList1"); dd.Items.Add("1990"); dd.Items.Add("1991"); dd.Items.Add("1992");
3
2343
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users page. So whenever the Admin person comes to know about the new category in the market he will be adding as different Sub-Categories for example ABAP, BDC etc..etc.. on every click event as Checkboxes. And these controls(checkboxes) should remain...
3
8781
by: Bruce Wood | last post by:
Maybe I'm going nuts, but I was so sure that adding the same method more than once to a delegate would result in only one entry on the delegate's call list: this.UpdateEnd += new EventHandler(UpdateStockCode); this.UpdateEnd += new EventHandler(UpdateStockCode); if (this.UpdateEnd != null) { this.UpdateEnd(this, System.EventArgs.Empty);
15
3132
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
1
3184
by: RSH | last post by:
Hi, I have a situation where I need to add several "Hidden" properties to list items in a dropdownlist. By default the DropDownList item has two properties with regards to the listitems collection, Text and Value. I need to add a DivisionID, and DepartmentID. I assumed i could simply override the ListItem class and add the additional properties. is this in fact the case? If so what would the code look like?
2
3323
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 the the cells of the table respectively which I have done easily but the problem is when I add the dynamically generated dropdownlist to the cell I cannot read the value from the dropdownlist. For adding the dropdownlist in the cell I have done the...
0
1286
by: mesut | last post by:
Hi colleagues, I know this group is not related to ASPX directly, but I'm struggling with this already a week and can't get/find any answer from other groups or internet site... I'm using a listview and the problem is I won't get the values from the server controls in 1-ListView_Itemcommand and/or 2- listview_ItemUpdating events for all Server controls Except for TEXTBOX controls.
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8954
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.