Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Note: issue resolved, see 2 posts down as I've included my fix for others to find

 

In pre-ASP.Net days if one wanted to add a popup that made a user confirm a delete action all one had to do was add the following Javascript to the item...

onclick="Javascript:return confirm('You are sure you want to permenatly delete this record, is this ok?')"

 

But it seems when I paste this into ASP.Net I get a very generic error

Compiler Error Message: CS1026: ) expected

 

Even though THERE IS NOT A PROBLEM WITH MY ()s anywhere, I've even had a 2nd developer double check my code. A quick google search reveals at least 2 other people have had this same problem but no formal resolution/explanation was given.

 

I also cannot use a btnDelete.Parameters.Add() to add an Javascript OnClick event in my Page_Load because this isn't a normal form button, the button is part of a DataGrid column using the Edit Template. I even tried adding the OnClick parameter to the ItemDataBound event like so (because it's the only place that I know of to access datagrid template buttons on a per button level)

private void dgResults_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

e.Item.FindControl("btnDelete").Attributes.Add("onclick","javascript:if(confirm('Are you sure?')== false) return false;");

}

But this didn't work either

 

So does anybody know how to add a "Are you sure" Javascript dialog to an ImageButton being generated by a DataGrid's template column? I know I could hack up a work-around using a new webform but I don't want to go that extreme.

 

I've already tried these random things, which didn't not work:

1. Changing the quotes from ' to " (and vice versa)

2. Removing the () from the javascript

3. Rearranging the OnClick="" more ways than you can imagine

Edited by travisowens
Experience is something you don't get until just after the moment you needed it
Posted

I explained above that Visual Studio doesn't even allow for .Parameters.Add() at all. But the control does exist because it's auto generated plus I'm accessing it earlier up in the code in another part of an IF check.

 

// Check if the current DeptID row is NOT the same as the current user's DeptID

if (Convert.ToInt32(drv.Row["DeptID"].ToString()) != Common.EmployeeDeptID())

{

// Hide the Edit and Delete buttons

e.Item.FindControl("btnEdit").Visible = false;

e.Item.FindControl("btnDelete").Visible = false;

 

}

else

{

e.Item.FindControl("btnDelete").Attributes.Add("onclick","javascript:if(confirm('Are you sure everything is correct?')== false) return false;");

}

Experience is something you don't get until just after the moment you needed it
Posted

ISSUE RESOLVED!

 

Here's the code that allowed me to add my Javascript popup to each button in a datagrid..

 

// Check if the current DeptID row is NOT the same as the current user's DeptID

if (Convert.ToInt32(drv.Row["DeptID"].ToString()) != Common.EmployeeDeptID())

{

// Hide the Edit and Delete buttons

e.Item.FindControl("btnEdit").Visible = false;

e.Item.FindControl("btnDelete").Visible = false;

 

}

else

{

ImageButton thisButton = (ImageButton)e.Item.FindControl("btnDelete");

thisButton.Attributes.Add("onclick","javascript:if(confirm('Are you sure you to delete this record?')== false) return false;");

}

 

I was forgetting to cast the object (when found) to an ImageButton, as you can only do the .Parameters.Add() to the image button, and not the FindControl() function... silly me.

Experience is something you don't get until just after the moment you needed it
Posted

Didnt see where you said it doesnt add the .Attributes.add, all i see is that you said it doesnt work, which could mean anything:)

 

1. You should check for an ItemType in your itemdatabound. Becuase you could be going thru the header and attempting find a control which is not there.

 

2. Your dvRow will now become a e.item.dataitem becuase the .bind of your datagrid is firing off this sub for every row in your datasource

 

3. Cast your findcontrol to the type, CType(e.item.findcontrol("btnSubmit"), ImageButton).Attributes.Add("","")

Or

Dim btnSubmit As ImageButton = e.item.findcontrol("btnSubmit")

Posted

3. CType is slow, if you know what it's going to be use DirectCast.

 

As a matter of practice you should check to make sure the control is found too. IMO

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...