Solved

dynamically created control added to datagrid cell event will not fire

Posted on 2007-03-18
4
441 Views
Last Modified: 2013-11-07
I have a datagrid that shows a dropdownlist in the row cell when I click the datagrid edit link. When the selected index change event fired for the dropdownlist I am dynamically creating controls (linkButton) at runtime and adding it to the cell and hiding the dropdownlist.
PROBLEM:
When the linkButton is clicked I cannot get and event to fire. The DataGrid ItemCommand event does not fire and the event I created when I dynamically added the control to the cell will not fire. Does anyone know how to fire an event for a control dynamically created and added to a datagrid cell?
0
Comment
Question by:jtwoods4
  • 2
4 Comments
 
LVL 13

Expert Comment

by:KarinLoos
ID: 18742880
Dynamic controls need to be created in the rowcreated event of the grid. Maybe it would be easier to have the linkbutton in your edititemtemplate but with visible=false, and merely show/hide appropriately.
0
 

Author Comment

by:jtwoods4
ID: 18743442
Excellent idea. I did not think of looking at the problem from this angle. I created the link button in the EditItemTemplate and set the visibility to false. How do I access the LinkButton in my code behind so I can set the visibility to true when the user selects an item from the DropDowList? I think this is why I avoided this mehtod before. Here is my code when the user selects an item from the drop down list.

public void SelectedIndexChanged(object sender, System.EventArgs e)
{
           DropDownList DropDownList1 = (DropDownList)sender;
           if (DropDownList1.SelectedValue == "-1")
           {
      DropDownList1.Visible = false;
                     //Need to show the link button here.....
           }
}
0
 
LVL 13

Accepted Solution

by:
KarinLoos earned 500 total points
ID: 18743922
Well thats quite simple really:

protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList list = (DropDownList)sender;
           if (list.SelectedValue == "-1")
           {
                // pick up the parent of the dropdownlist which is a datagrid item
                 TableCell cell = list.Parent as TableCell;
                 DataGridItem item = cell.Parent as DataGridItem;
                // now get the itemindex of the datagrid
                 int index = item.ItemIndex;
               // once you have this you can use this to find the linkbutton in the datagrid
               //  and then set its visibility of the linkbutton and the dropdownlist itself.
                  }
       )
   
}

0
 
LVL 1

Expert Comment

by:AhmedMozaly
ID: 18746547
if you are using asp.net
replace your datagrid with gridview
then do the same things you want in it , it will raise the SelectedIndexChanged
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now