|
-
Dec 20th, 2002, 09:08 AM
#1
Thread Starter
Frenzied Member
confirmation message from datagrid button
I can't get my datagrid delete button to pop up this message. I have it placed so it will run when the onDeleteCommand is fired. I debug and it does indeed go thru the code, but no message box...any suggestions?
Code:
Public Sub DeleteBid(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
'check which row has been selected
Dim selectedRow As String = DataGrid1.DataKeys(e.Item.ItemIndex)
Dim btnDelete As Button
btnDelete = e.Item.Cells(4).Controls(0) '<---5th column, first control
btnDelete.Attributes("onclick") = "javascript:return confirm('Are you sure you want to delete \n Bid Record " & selectedRow & "?')"
-
Feb 13th, 2004, 04:02 PM
#2
Frenzied Member
I believe you can only add to the Attributes of a Button during Page_Load()
-
Feb 13th, 2004, 09:51 PM
#3
Hyperactive Member
You need to add your javascript code to the delete button during the ItemCreated event:
PHP Code:
<%@ Import Namespace="System.Data" %>
<script language="C#" runat="server">
protected override void OnInit(System.EventArgs e)
{
if (!Page.IsPostBack)
{
bindGrid();
}
}
private void bindGrid()
{
TheGrid.DataSource = getFakeData();
TheGrid.DataBind();
}
private DataTable getFakeData()
{
DataTable myTable = new DataTable();
myTable.Columns.Add(new DataColumn("ID"));
myTable.Columns.Add(new DataColumn("FirstName"));
DataRow newRow = myTable.NewRow();
newRow["ID"] = 1;
newRow["FirstName"] = "Chris";
myTable.Rows.Add(newRow);
newRow = myTable.NewRow();
newRow["ID"] = 2;
newRow["FirstName"] = "Joe";
myTable.Rows.Add(newRow);
return myTable;
}
protected void TheGrid_ItemCreated(object sender, DataGridItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lbtnDelete = (LinkButton)args.Item.FindControl("lbtnDelete");
if (lbtnDelete != null)
{
lbtnDelete.Attributes.Add("onclick","return window.confirm('Are you sure you want to delete this item?');");
}
}
}
protected void TheGrid_DeleteCommand(object sender, DataGridCommandEventArgs args)
{
int deleteID = System.Convert.ToInt32(TheGrid.DataKeys[args.Item.ItemIndex]);
lblMessage.Text = String.Format("Deleting {0}", deleteID);
}
protected void TheGrid_EditCommand(object sender, DataGridCommandEventArgs args)
{
lblMessage.Text = "Editing...";
TheGrid.EditItemIndex = args.Item.ItemIndex;
bindGrid();
}
protected void TheGrid_UpdateCommand(object sender, DataGridCommandEventArgs args)
{
lblMessage.Text = "Updating...";
TheGrid.EditItemIndex = -1;
bindGrid();
}
protected void TheGrid_CancelCommand(object sender, DataGridCommandEventArgs args)
{
lblMessage.Text = "Cancelling...";
TheGrid.EditItemIndex = -1;
bindGrid();
}
</script>
<html>
<body>
<form runat="server">
<asp:DataGrid ID="TheGrid" Runat="server" AutoGenerateColumns="False" DataKeyField="ID"
OnItemCreated="TheGrid_ItemCreated" OnDeleteCommand="TheGrid_DeleteCommand"
OnEditCommand="TheGrid_EditCommand" OnUpdateCommand="TheGrid_UpdateCommand"
OnCancelCommand="TheGrid_CancelCommand">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" />
<asp:BoundColumn DataField="FirstName" HeaderText="First Name" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" Runat="server"
CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbtnSave" Runat="server"
CommandName="Update" Text="Update" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lbtnDelete" Runat="server"
CommandName="Delete" Text="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbtnCancel" Runat="server"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Label ID="lblMessage" Runat="server" EnableViewState="False"/>
</form>
</body>
</html>
Note the DataKeyField, use that guy to reference the correct piece of data to remove.
-
Feb 17th, 2004, 05:27 PM
#4
Frenzied Member
How do you know the return value of the confirm() to tell whether you want to delete the row or not?
-
Feb 17th, 2004, 07:00 PM
#5
Hyperactive Member
If you clicked Cancel on the confirm dialog, you won't post the form and thus won't trigger the DeleteCommand eventhandler. Clicking the OK button would post the form and trigger the DeleteCommand eventhandler indicating that the row is to be deleted. You should be able to cut and paste that code into a page and demonstrate the behavior.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|