Add javascript confirm to linkbutton in nested Datalist

I have a "Delete" linkbutton in edititem of a  nested datalist that I'm trying to add a javascript confirm to on the itemcreated event handler I keep getting "Object reference not set to an instance of an object." I can't seem to find the control.
I'm sure this is simple but its been driving me nuts!!

Thanks in advance
Glorfindal


// "OrderList" Datatlist  is nested in the selected item of the "CustomerList" Datalist

OrderList.ItemCreated += new DataListItemEventHandler(this.Item_Created);

private void Item_Created(Object Sender,  System.Web.UI.WebControls.DataListItemEventArgs e)
{
      if (e.Item.ItemType == ListItemType.EditItem)
      {
                  LinkButton _ApproveBtn = (LinkButton)e.Item.FindControl("btnDelete");
                 _ApproveBtn.Attributes.Add("onClick", "return confirm('Do you wish to delete this order?')");
                        
      }
}
glorfindalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
try this:

void ItemDataBound(Object Sender,  System.Web.UI.WebControls.DataListItemEventArgs e)
{
     if (e.Item.ItemType == ListItemType.EditItem)
     {
                  LinkButton _ApproveBtn = (LinkButton)e.Item.FindControl("btnDelete");
                 _ApproveBtn.Attributes.Add("onClick", "return confirm('Do you wish to delete this order?')");
                   
     }
}
0
YZlatCommented:
sorry, I meant

void MyDataList_ItemDataBound(Object Sender,  System.Web.UI.WebControls.DataListItemEventArgs e)
{
     if (e.Item.ItemType == ListItemType.EditItem)
     {
                  LinkButton _ApproveBtn = (LinkButton)e.Item.FindControl("btnDelete");
                 _ApproveBtn.Attributes.Add("onClick", "return confirm('Do you wish to delete this order?')");
                   
     }
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smolamCommented:
I am assuming that you are not using a Template Comlumn in which case the syntax would be as follows, if not then I apologize.

OrderList.ItemCreated += new DataListItemEventHandler(this.Item_Created);

private void Item_Created(Object Sender,  System.Web.UI.WebControls.DataListItemEventArgs e)
{
     if (e.Item.ItemType == ListItemType.EditItem)
     {
                  LinkButton _ApproveBtn = e.Item.Cells[0].Controls[0];
                 _ApproveBtn.Attributes.Add("onClick", "return confirm('Do you wish to delete this order?')");
                   
     }
}

Assuming that the delete column is the first column in your datagrid if not then you need to change the Cells[0] to the index of the column that you have the delete button in?
0
glorfindalAuthor Commented:
YZlat,

Thanks, That did it. I was close.

 By the way smolam it was a template column but I appreciate the reply

Take care,
Glorfindal

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.