javascript confirm with editable datagrid?

meeps

Newcomer
Joined
Jul 28, 2003
Messages
13
I'm trying cause a javascript confirmation box to pop up when a user clicks the 'update' button in my editable data field. I searched through the other posts on the topic, but I'm still having problems.

One method I've seen suggested is to use FindControl("btnName") in the ItemDataBound event and then use Attributes.Add.etc. my update button is in an EditCommandColumn and I see no way to name it. Is it possible to name the button so that I can use FindControl("...")?

The other method I read about was to use e.Item.Cells[x].Controls[1] instead of FindControl. I tried this, but it gives me an out of range error for Cells[x]. (if i use Cells[7], i get the cell before the button, but Cells[8] gives the out of range error). Doesn't an editCommandColumn count as a column???

I would like to keep the buttons in an EditCommandColumn if possible. Can someone help me out?

Thanks.
 
One method I've seen suggested is to use FindControl
("btnName") in the ItemDataBound event and then use
Attributes.Add.etc. my update button is in an
EditCommandColumn and I see no way to name it. Is it possible
to name the button so that I can use FindControl("...")?

The method I've used relies on the column index. I created
constants representing each column...
Visual Basic:
  Private Const DG_COL_CHECK As Integer = 0
  Private Const DG_COL_EDIT As Integer = 1
  Private Const DG_COL_DELETE As Integer = 2
  Private Const DG_COL_CLASS As Integer = 3
  Private Const DG_COL_ASSIGN As Integer = 4
  Private Const DG_COL_DUE As Integer = 5
... and then, in the ItemDataBound event, you can get the button
in the column (here it's the delete button for me)...
Visual Basic:
Dim btn As Button = DirectCast(e.Item.Cells(DG_COL_DELETE).Controls(0), Button)
... and then set the button's onClick attribute:
Visual Basic:
      btn.Attributes("onclick") = "javascript:return confirm('Are you sure you want to delete the assignment?')"

[edit]Of course, when you put the onClick attribute in, "javascript:"
will be one word; it looks like vBilletin parses out javascript so that
people don't put scripts in posts[/edit]
 
thanks for your help. I must be doing something wrong, but I can't figure out what it is. When I try using this line:

e.Item.Cells[column_num].Controls[0]

it will let me reference any column EXCEPT for the one I want. Cells[7] gives me the column before the one I want, but Cells[8] gives me an out of range error (as if the last column doesn't exist). Any idea on what I might be doing that would cause this?

Thank you.
 
I am not even sure if this will help, but one of the things you might be able to do is at the html code right to the button when you declare it.

For instance:
If you are using a asp:button on your edit column
For the text attribute you could use the following.

text="<div onclick='javascript:alert("Confirm Delet")'>EDIT</div>"

This is a nice way to add javascript to you code.

Not sure it will help.
 
Command Columns not part of DataGridItemEventArgs

I would love for someone to tell me I am doing something wrong but.... It is my observation that the Command and Button Columns are not part of the DataGridItemEventArgs (e parameter).

I have followed Bucky's code and get the same out of range error that Meeps speaks of. On another grid I have I needed to have the delete column disapear at certain times and found that the e.items.cells(#).controls(#) always failed on the delete command column I created in the property builder with out of range.

However, I found that grid.columns(#).visible = False worked.
Unfortunately the grid.columns collection does not appear to be able to give me the cells and controls array I need to add my delete confirmation code the Bucky has.

Bucky, does your code run on a Command or Button Column?

I have not tried slitzi's approach yet because I would like to avoid modifying the HTML page if possible.

Kevin
 
Last edited:
Command Columns not part of DataGridItemEventArgs

I have been playing around with this and discovered I could do this in code this way.... EXTREMELY DISGUSTING REALLY.... I hope this is not the only way to do this via code behind for command and button contorls in a datagrid.

ctl = grdEvent.Controls(0)
Dim i As Integer
Dim ii As Integer
Dim iii As Integer
For i = 0 To ctl.Controls.Count - 1
For ii = 0 To ctl.Controls(i).Controls.Count - 1
For iii = 0 To ctl.Controls(i).Controls(ii).Controls.Count - 1
If ctl.Controls(i).Controls(ii).Controls(iii).GetType.ToString = "System.Web.UI.WebControls.DataGridLinkButton" Then
Dim l As LinkButton
l = DirectCast(ctl.Controls(i).Controls(ii).Controls(iii), LinkButton)
If l.Text = "Delete" Then
l.Attributes("onclick") = "javascript:return confirm('Are you sure you want to delete the Event Code?');"
End If
End If
Next
 
If you don't want to modify the html page then Response.Write the javascript in the onclick event of the update button.
why does it matter what you do with the html? just curious.

-lp
 
Got it figured out....

In the ItemDataBound Event....

e.Item.Cells(3).Attributes("onclick") = "javascript:return confirm('Are you sure you want to delete the Event Code?')"

3 being the index value for my delete column

Dropping the .Controls(0) fixes the issue. I guess the linkbutton doesn't exist in this context.
 
Anyone interested in the html code to do the same thing:

Add the following to the edit column text property
Text =
"<span onclick="javascript:return confirm('Are you sure you want to delete the assignment?')" style='color:blue;font-size:10px'>Edit</span>
 
I may be wrong, but wasn't the question how to add a confirmation box to the update linkbutton and not a delete or Edit button? The update button will be generated after the Edit button has been clicked.

I am struggling with the same problem. Any advice would eb appreciated.
 
I don't remember exactly what my original problem was, but I know it was something dumb. Here's how I ended up doing it:

protected void datagrid_ItemDataBound(object...,...)
{
LinkButton linkButton = new LinkButton();

foreach(DataGridItem item in datagrid.Items)
{
// x is the number of the cell that contains
// your buttons.
linkButton = (LinkButton)item.Cells[x].Controls[0];

// check to see if button is save.
// otherwise, edit buttons will also be affected.
if(linkButton.Text == "Save")
{
linkButton.Attributes.Add("onclick","return confirm('Are you sure you want to save?');");
}
}
}
 
Thanks,

I've used something similar:

Sub DgSrchresults_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) _
Handles DgSrchResults.ItemDataBound

Select Case e.Item.ItemType

Case ListItemType.EditItem
Dim myUpdateButton As LinkButton = New LinkButton
myUpdateButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
myUpdateButton.Attributes.Add("onclick", "return confirm('Are you sure you want to update this Post Code?');")
End Select
End Sub

Brian
 
Guys it is bit tricky
where I have found a way of adding JS confirmation to Edit, Update And Cancel

Since these link buttons are created at within the dataGrid you cannot accesss them using the FindControl() method

What you guys have to do is to find the link button at ItemDataBound event of the DataGrid

since there are 3 (Edit, Update, Cancel) dynamic controls in one cell use a loop, capture the control type and identify the control by its text value

finally add linkbutton Attributes

foreach (Control ctrl in e.Item.Cells[x].Controls)
{
if (ctrl.GetType().ToString( == "System.Web.UI.WebControls.DataGridLinkButton")
{
LinkButton lnkBttn = new LinkButton();
lnkBttn = (LinkButton)ctrl;

if (lnkBttn.Text == "Edit")
{
lnkBttn.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to Edit?');");

else if (lnkBttn.Text == "Update")
{
lnkBttn.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to Update?');");

else if (lnkBttn.Text == "Cancel")
{
lnkBttn.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to Cancel?');");

}
}
}


Cheers :):):)
 
Back
Top