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

Use of RadAjaxManager ResponseScripts

1 Answer 435 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leonard Jones
Top achievements
Rank 1
Leonard Jones asked on 22 Feb 2011, 04:21 PM
I have a RadGrid which uses the folowing settings

<ClientSettings EnablePostBackOnRowClick="True">
    <Selecting EnableDragToSelectRows="False" AllowRowSelect="True" />
</ClientSettings>

The highlighting of the selected  dataItem or row provides the user with a visual cue to the dataItem he wishes to use without requiring screen real estate to be occupied by a dedicated Select button column. I use the ItemCommand and PreRender events of the grid to do the highlighting. The grid also contains an Edit button column and a Delete button column and this method of row selection works when either of the buttons is clicked e.g. if row 4 is currently selected and I click on the Edit button of row 2, then row2 is rendered in edit mode and is also selected.

My problem lies with the Delete button processing. I am trying to use the technique mentioned in Delete row in grid and prompt alert message to show a javascript Confirm dialog box before proceeding with the Delete operation however the ResponseScripts.Add

statement in the grid's ItemCommand function are not working for me - there are no errors but no Confirm dialog either.

.aspx

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FDCSTest.aspx.cs" Inherits="FDCSDev.FDCSTest" %>
  
  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
  
  
  
<head id="Head1" runat="server">
  
  
  
<title></title>
  
  
  
<link href="StyleSheet.css" type="text/css" />
  
</head>
  
<body class="body">
  
  
  
<form id="form1" runat="server">
  
  
  
<!-- Page Content Area -->
  
  
  
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
  
  
  
<AjaxSettings>
  
  
  
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
  
  
  
<UpdatedControls>
  
  
  
<telerik:AjaxUpdatedControl ControlID="gridFlightSchedule" />
  
  
  
</UpdatedControls>
  
  
  
</telerik:AjaxSetting>
  
  
  
<telerik:AjaxSetting AjaxControlID="gridFlightSchedule">
  
  
  
<UpdatedControls>
  
  
  
<telerik:AjaxUpdatedControl ControlID="gridFlightSchedule" />
  
  
  
</UpdatedControls>
  
  
  
</telerik:AjaxSetting>
  
  
  
</AjaxSettings>
  
  
  
</telerik:RadAjaxManager>
  
  
  
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
  
  
  
</telerik:RadScriptManager>
  
  
  
<div style="width: 800px; height: 800px; margin-left: auto; margin-right: auto;">
  
  
  
<asp:Label ID="lblInfo" runat="server" Text="Label"></asp:Label>
  
  
  
<div style="width: 100%; height: 400px;">
  
  
  
<!-- Flight Schedule Grid Area -->
  
  
  
<telerik:RadGrid ID="gridFlightSchedule" runat="server"
  
  
  
AutoGenerateColumns="False" 
  
DataSourceID="FlightScheduleDataSource" GridLines="None" 
  
Height="380px"
  
  
  
AllowSorting="True" 
  
ShowStatusBar="True"
  
  
  
onitemcreated="gridFlightSchedule_ItemCreated"
  
  
  
onitemcommand="gridFlightSchedule_ItemCommand" 
  
onprerender="gridFlightSchedule_PreRender"
  
  
  
>
  
  
  
<ClientSettings EnablePostBackOnRowClick="True">
  
  
  
<Selecting EnableDragToSelectRows="False" AllowRowSelect="true" />
  
  
  
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
  
  
  
<ClientEvents />
  
  
  
</ClientSettings>
  
  
  
<MasterTableView Name="FlightScheduleTableView" DataKeyNames="fltID,carID" DataSourceID="FlightScheduleDataSource"
  
  
  
NoMasterRecordsText="No flights defined." CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add a new Flight"
  
  
  
GridLines="Horizontal" EditMode="InPlace" >
  
  
  
<CommandItemSettings AddNewRecordText="Add a new Flight" ExportToPdfText="Export to Pdf"></CommandItemSettings>
  
  
  
<Columns>
  
  
  
<telerik:GridBoundColumn DataField="fltID" DataType="System.Int32" 
  
HeaderText="fltID" ReadOnly="True" SortExpression="fltID" UniqueName="fltID" Display="false">
  
  
  
<HeaderStyle />
  
  
  
</telerik:GridBoundColumn>
  
  
  
<telerik:GridBoundColumn DataField="carID" DataType="System.Int32" 
  
HeaderText="carID" ReadOnly="True" SortExpression="carID" UniqueName="carID" Display="false">
  
  
  
<HeaderStyle />
  
  
  
</telerik:GridBoundColumn>
  
  
  
<telerik:GridDropDownColumn 
  
HeaderText="Carrier" SortExpression="carDesc" UniqueName="carDesc" ListTextField="carDesc"
  
  
  
ListValueField="carID" DataField="carID" DataSourceID="CarriersDataSource"
  
  
  
DropDownControlType="DropDownList" AllowVirtualScrolling="true" ShowMoreResultsBox="true"
  
  
  
ItemsPerRequest="10" >
  
  
  
<HeaderStyle Width="12em" />
  
  
  
</telerik:GridDropDownColumn>
  
  
  
<telerik:GridBoundColumn DataField="FltNo" HeaderText="Flight No" 
  
SortExpression="FltNo" UniqueName="FltNo" >
  
  
  
<HeaderStyle Width="6em" />
  
  
  
</telerik:GridBoundColumn>
  
  
  
  
<telerik:GridDropDownColumn 
  
HeaderText="Aircraft" SortExpression="atyDescription" UniqueName="atyDescription" ListTextField="atyDescription"
  
  
  
ListValueField="atyID" DataField="atyID" DataSourceID="AircraftDataSource"
  
  
  
DropDownControlType="DropDownList" >
  
  
  
<HeaderStyle Width="12em" />
  
  
  
</telerik:GridDropDownColumn>
  
  
  
<telerik:GridEditCommandColumn>
  
  
  
<HeaderStyle Width="8em" />
  
  
  
</telerik:GridEditCommandColumn>
  
  
  
<telerik:GridTemplateColumn>
  
  
  
<ItemTemplate> </ItemTemplate>
  
  
  
</telerik:GridTemplateColumn>
  
  
  
<telerik:GridButtonColumn UniqueName="DeleteCol" Text="Delete" CommandName="Delete"></telerik:GridButtonColumn
  
</Columns>
  
  
  
</MasterTableView>
  
  
  
</telerik:RadGrid>
  
  
  
</div>
  
  
  
<asp:SqlDataSource ID="FlightScheduleDataSource" runat="server" 
  
ConnectionString="<%$ ConnectionStrings:FDCSWebFltConnectionString %>"
  
  
  
SelectCommand="SELECT fltID, licID, carID, FltNo, StartDate, EndDate, Mon, Tue, Wed, Thu, Fri, Sat, Sun, atyID FROM fdcs_FltSched WHERE (licID = @licID) ORDER BY FltNo">
  
  
  
<SelectParameters>
  
  
  
<asp:SessionParameter Name="licID" SessionField="licID" />
  
  
  
</SelectParameters>
  
  
  
</asp:SqlDataSource>
  
  
  
<asp:SqlDataSource ID="CarriersDataSource" runat="server" 
  
ConnectionString="<%$ ConnectionStrings:FDCSWebFltConnectionString %>" 
  
SelectCommand="SELECT carID, carDesc FROM fdcs_Carriers WHERE (carID IN (SELECT carID FROM fdcs_LicIdCarNumCode WHERE (licID = @licID)))">
  
  
  
<SelectParameters>
  
  
  
<asp:SessionParameter DefaultValue="" Name="licID" SessionField="licID" />
  
  
  
</SelectParameters>
  
  
  
</asp:SqlDataSource>
  
  
  
<asp:SqlDataSource ID="AircraftDataSource" runat="server" 
  
ConnectionString="<%$ ConnectionStrings:FDCSWebFltConnectionString %>" 
  
SelectCommand="SELECT [atyID], [atyCode], [atyDescription] FROM fdcs_Aircraft WHERE (carID IN (SELECT carID FROM fdcs_LicIdCarNumCode WHERE (licID = @licID)))">
  
  
  
<SelectParameters>
  
  
  
<asp:SessionParameter Name="licID" SessionField="licID" />
  
  
  
</SelectParameters>
  
  
  
</asp:SqlDataSource>
  
  
  
</div>
  
  
  
</form>
  
</body>
  
</html>

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
  
namespace FDCSDev
{
    public partial class FDCSTest : System.Web.UI.Page
    {
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserLogInName"] == null)
                {
                    Session["UserLogInName"] = "len";
                    Session["UserID"] = "cc3b700d-b6a7-42ac-b1f0-c2de6e3d0fd9";
                    Session["HomeStation"] = "ABB";
                    Session["HandlerCode"] = "ABC";
                    Session["licID"] = 1;
                }
            }
            lblInfo.Text = " ";
        }
  
        protected void gridFlightSchedule_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item.OwnerTableView.Name == "FlightScheduleTableView" && e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem editItem = e.Item as GridEditableItem;
  
                DropDownList ddList = editItem["carDesc"].Controls[0] as DropDownList;
                if (!e.Item.OwnerTableView.IsItemInserted)
                {
                    //item is about to be edited
                    // don't allow the Carrier or Flight No to be changed once the flight has been created
                    ddList.Enabled = false;
  
                    TextBox tb = editItem["FltNo"].Controls[0] as TextBox;
                    tb.Width = 50;
                    tb.Enabled = false;
                }
                else
                {
                    //item is about to be inserted
                    ddList.AutoPostBack = true;
                    ddList.SelectedIndexChanged += new System.EventHandler(this.Carrier_SelectedIndexChanged);
                }
            }
        }
  
        private void Carrier_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            DropDownList ddl = sender as DropDownList;
            string strCarID = ddl.SelectedValue;
        }
  
        protected void gridFlightSchedule_PreRender(object sender, EventArgs e)
        {
            if (Session["SelectedFlightIndex"] != null)
            {
                Int32 selectedIndex = Convert.ToInt32(Session["SelectedFlightIndex"]);
                gridFlightSchedule.MasterTableView.Items[selectedIndex].Selected = true;
            }
  
        }
  
        protected void gridFlightSchedule_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if ((e.CommandName == RadGrid.EditCommandName) || (e.CommandName == RadGrid.DeleteCommandName) || (e.CommandName == "RowClick"))
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                //dataItem.Selected = true;
                Session["SelectedFlightIndex"] = dataItem.ItemIndexHierarchical;
  
                if (e.CommandName == "Delete")
                {
                    e.Canceled = true;
                    string script = String.Format("if (confirm('Do you wish to Delete the flight ?')) clientDeleteFlight({0});", dataItem.ItemIndexHierarchical);
                    RadAjaxManager1.ResponseScripts.Add(script);
                    //ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", "alert('Help!!');", true);   
                }
  
            }
        }
  
    }
}

 

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 25 Feb 2011, 12:13 PM
Hi Leonard,

You can show a javascript prompt on delete right before the grid postbacks, actually. You do not need to postback only to show a javascript prompt. You can use a GridTemplateColumn as follows:

<telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:LinkButton ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete"
            OnClientClick="return confirm('Do you wish to Delete the flight ?');">
        </asp:LinkButton>
    </ItemTemplate>
</telerik:GridTemplateColumn>


Veli
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
Grid
Asked by
Leonard Jones
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or