[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

dynamically created control added to datagrid cell event will not fire

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
jtwoods4
Asked:
jtwoods4
  • 2
1 Solution
 
KarinLoosCommented:
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
 
jtwoods4Author Commented:
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
 
KarinLoosCommented:
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
 
AhmedMozalyCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now