Results 1 to 5 of 5

Thread: confirmation message from datagrid button

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    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 & "?')"

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    I believe you can only add to the Attributes of a Button during Page_Load()

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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 senderDataGridItemEventArgs 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 senderDataGridCommandEventArgs args)
    {
        
    int deleteID System.Convert.ToInt32(TheGrid.DataKeys[args.Item.ItemIndex]);
        
    lblMessage.Text String.Format("Deleting {0}"deleteID);
    }
    protected 
    void TheGrid_EditCommand(object senderDataGridCommandEventArgs args)
    {
        
    lblMessage.Text "Editing...";
        
    TheGrid.EditItemIndex args.Item.ItemIndex;
        
    bindGrid();
    }
    protected 
    void TheGrid_UpdateCommand(object senderDataGridCommandEventArgs args)
    {
        
    lblMessage.Text "Updating...";
        
    TheGrid.EditItemIndex = -1;
        
    bindGrid();
    }
    protected 
    void TheGrid_CancelCommand(object senderDataGridCommandEventArgs 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.

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    How do you know the return value of the confirm() to tell whether you want to delete the row or not?

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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
  •  



Click Here to Expand Forum to Full Width