This is a migrated thread and some comments may be shown as answers.

[Solved] How to force user to click insert/edit button or cancel button ?

4 Answers 275 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anna Gao
Top achievements
Rank 1
Anna Gao asked on 29 Jan 2013, 11:37 PM
I use EditMode="EditForms" for user to insert/edit item of the RedGrid. Once in the Edit/Insert form, how to force the user to either click  edit/insert button or Cancel button before leaving the Edit/Insert form ? Or at least providing a warning of losing data ?

Thanks a lot in advance !


4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Jan 2013, 04:32 AM
Hi,

Please take a look into the following code snippet I tried to make sure that either edit or insert form is open at a time. If edit button is clicked while the grid in insert mode, it will show an  alert to insert/cancel the insert operation first and then proceed.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName)
    {
        if (edit == true)
        {
            e.Canceled = true;
                Response.Write("<script>alert('Please update or cancel the edit operation');</script>");
        }
        if (RadGrid1.MasterTableView.IsItemInserted == true)
        {
            e.Canceled = true;
            Response.Write("<script>alert('Please insert or cancel the insert operation first');</script>");
        }             
    }
    else if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        if (RadGrid1.EditItems.Count > 0)
        {
            e.Canceled = true;
            Response.Write("<script>alert('Please update or cancel the edit operation');</script>");
        }
    }
}

Please elaborate your scenario if it doesn't help.

Thanks,
Shinu.
0
Anna Gao
Top achievements
Rank 1
answered on 31 Jan 2013, 01:42 AM
Thanks so much for your reply. It works great for Insert and Edit commend on the grid. Is there a way for the delete command on the grid  or when user tries to navigate away from the grid page such as clicking another tab of the RadTabStrip,  a button on the page or a link on the master page ?

Thanks !!!
0
Accepted
Shinu
Top achievements
Rank 2
answered on 31 Jan 2013, 06:43 AM
Hi,

Please take a look into the following code snippet I tried.

C#:
public string proceed = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
        if (RadGrid1.MasterTableView.IsItemInserted == true)
        {
            proceed = "NotInsert";             
        }
        else if (RadGrid1.EditItems.Count > 0)
        {
            proceed = "NotEdit";
        }      
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName)
    {
        if (proceed == "NotInsert")
        {
            e.Canceled = true;
            Response.Write("<script>alert('Please insert or cancel the insert operation first');</script>");
        }             
    }
    else if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        if (proceed == "NotEdit")
        {
            e.Canceled = true;
            Response.Write("<script>alert('Please update or cancel the edit operation');</script>");
        }
    }      
}
protected void Radtab1_TabClick(object sender, RadTabStripEventArgs e)
{
    if (proceed == "NotEdit")
    {
        Response.Write("<script>alert('Please update or cancel the edit operation');</script>");
        // to cancel the navigation to another pageview.
        Radtab1.Tabs[0].Selected = true//select the RadTab in which the Radgrid edit/insert operation is going on..
        PageViewID1.Selected = true//select the PageViewID in which the Radgrid edit/insert operation is going on..
    }
    if (proceed == "NotInsert")
    {
        Response.Write("<script>alert('Please insert or cancel the insert operation first');</script>");
        Radtab1.Tabs[0].Selected = true;
        PageViewID1.Selected = true;
    }
         
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (proceed == "NotEdit")
    {
        Response.Write("<script>alert('Please update or cancel the edit operation');</script>");
        Radtab1.Tabs[0].Selected = true;
        PageViewID1.Selected = true;
    }
    if (proceed == "NotInsert")
    {
        Response.Write("<script>alert('Please insert or cancel the insert operation first');</script>");
        Radtab1.Tabs[0].Selected = true;
        PageViewID1.Selected = true;
    }
}

Thanks,
Shinu.
0
Anna Gao
Top achievements
Rank 1
answered on 01 Feb 2013, 08:20 PM
It works well. Thank you so much !!!
I wish there was a similar function like we handle the windows unload message, but apparently there is not such thing :)

 window.onbeforeunload = unloadMessage;
        function unloadMessage() {
            message = "You have attempted to leave this accident insert/update page. Any unsaved data will be lost."
            if (needToConfirm)
                return message;
        }
Tags
Grid
Asked by
Anna Gao
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Anna Gao
Top achievements
Rank 1
Share this question
or