message box in webapp

I need help.

I need a message box OK/Cancel button in a web application (JavaScript ) to confirm a deleting row

the problem is that i have not button "Delete" but i have a datagrid with a button column.

Thx

PS-could you post code and explanation




Answer this question

message box in webapp

  • David Moye

    The ItemCreated event will be called whenever there is a new Item created. Here you just place this code:

    void Item_Created(Object Sender, DataGridItemEventArgs e)

    {

       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

       {

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

           _myButton.Attributes.Add("onclick", "return confirm('Are your sure ');");

       }

    }


    Then make sure you have set the ID attribute in you HTML:

    <asp:DataGrid id="MyDataGrid" AutoGenerateColumns="false" runat="server">

    <columns>

        <asp:TemplateColumn>

            <itemtemplate>

                <asp:ImageButton ID="btnDelete" ImageUrl="btnDelete.jpg" runat="server" />

              </itemtemplate>

            </asp:TemplateColumn>

            <asp:BoundColumn DataField="Name" HeaderText="Name" runat="server" />

            <asp:BoundColumn DataField="Phone" HeaderText="Phone Number" runat="server" />

            <asp:BoundColumn DataField="Address" HeaderText="Address" runat="server" />

            <asp:BoundColumn DataField="City" HeaderText="City" runat="server" />

        </columns>
    </asp:DataGrid>



  • Lightworker

    The same command will be executes as normally but only when the user click 'yes'.

    And please lett me know when you tried it and give some response.



  • almpirat

    Why do you dislike it The ItemCreated is called whenever a Item (Line/Row) is rendered, you never know how many Item's a DataGrid will contain. That's why you write put it in the ItemCreated event.

    You say you are a newby, so don't screem that you dont like it.. first give it a try and when there are any problems or disadvantage please lett me know.

    I don't see why thit is a bad solution

    When you put it directly in you template, you can't add extra information. So in the ItemCreated event you can put:

    Control myControl = (Control)e.Item.FindControl("MyControlID");
    myControl.Attributes.Add(
    "OnClick", return confirm('Are you sure you want to delete order# " + _order.Id + " ');");



  • /\gurra\/

    Pls i'm a newbye...

    1. Why in ItemCreate event I'm using it to delete...

    2. It's a button column...

    What's my controlID i have to replace

    3. i need an ok/cancel box, not only ok



  • heykac

    Hi,

    Item_Created() event is not called in my page tell me how can i debug or how can i set break point over this.

    in my .aspx file i have following code

    <asp:ImageButton ID="imgDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" ImageUrl="images/icon-delete.gif" />

    in datagrid(as a column of templete column)

    and in code behind i write following code.

    void Item_Created(Object Sender, DataGridItemEventArgs e)

    {

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

    {

    ImageButton _myButton = (ImageButton)e.Item.FindControl("imgDeleteButton");

    _myButton.Attributes.Add("onclick", "return confirm('Are your sure ');");

    }

    }but it is not working tell me problem



  • psydney

    I dislike it...maybe because i'm newbie... i can't understand the item_created...

    i have it:

    private void DeleteCommand(object source,

    System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {

    TableCell cell = e.Item.Cells[0];

    GestoreDB gestoreDB = new GestoreDB(DBConnection, DBTableName, Utente);

    gestoreDB.DeleteRow(Convert.ToInt32(cell.Text));

    BindData();

    }

    and in BLOCKED SCRIPT

    <script language="">

    function MsgOkCancel() {

    var fret;

    fret=confirm('are you sure');

    if (fret==true)

    {

    }

    }

    </script>

     

    <asp:TemplateColumn>< XML:NAMESPACE PREFIX = O />

            <itemtemplate>

                <asp:LinkButton onclick="msgokcancel" Commandname="delete" text="<img.../> runat="server" />

              </itemtemplate>

            </asp:TemplateColumn>



  • Dwarrel

    In the ItemCreate event from the DataGrid put this:

    Control myControl = (Control)e.Item.FindControl("MyControlID");
    myControl.Attributes.Add(
    "OnClick", return confirm('Are you sure ');");



  • Patrick F

    You are right...i have to try..

    another question:

    in this way (i refer your second message, not last) when i click yes, it executes the command in ItemCommand="xxx"



  • message box in webapp