Question

I want to have a RadWindow dialog with FormView inside it where I could perform update or insert operations depending on FormView's mode. I created a RadWindow dialog, added controls (FormView) as a content and populated FormView's content via databinding. Everything seems fine until I try to perform Save, Insert or Cancel operations. Corresponding events are not fired.

It would seem like FormView does not call ItemCommand or any other event after I change its mode. Does anybody have some insights?

I managed to extract this functionality into trivial example where I have only Default page and RadWindow.

Here is my markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript">
        function CloseDialog() {
            alert("Close Dialog func");
            var window = $find("<%=dialogWnd1.ClientID %>");
            window.close();
        }
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <telerik:RadButton runat="server" ID="openEditDialogBtn" Text="Edit Dialog" OnClick="OnEditModeClick">
    </telerik:RadButton>
    <telerik:RadButton runat="server" ID="openInsertDialogBtn" Text="Insert Dialog" OnClick="OnInsertModeClick">
    </telerik:RadButton>
    <telerik:RadWindowManager runat="server" ID="DialogWindowManager">
        <Windows>
            <telerik:RadWindow runat="server" ID="dialogWnd1">
                <ContentTemplate>
                    <asp:UpdatePanel runat="server" ID="updatePanel1" UpdateMode="Conditional" OnUnload="UpdatePanel_Unload_Workaround">
                        <ContentTemplate>
                            <div>
                                <asp:FormView ID="formView1" runat="server" DefaultMode="ReadOnly" DataSourceID="FormViewDataSourceID"
                                    OnItemCommand="formView1_OnItemCommand">
                                    <ItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                    Data Id (ITEM)
                                                </td>
                                                <td>
                                                    <telerik:RadTextBox ID="txtDataId" runat="server" Text='<%# Bind("DataId") %>' Enabled="False" />
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                    Data Id (EDIT)
                                                </td>
                                                <td>
                                                    <telerik:RadTextBox ID="txtDataId" runat="server" Text='<%# Bind("DataId") %>' />
                                                </td>
                                            </tr>
                                        </table>
                                        <div>
                                            <telerik:RadButton runat="server" CommandName="Update" Text="Save">
                                            </telerik:RadButton>
                                            <telerik:RadButton runat="server" CommandName="Cancel" Text="Cancel">
                                            </telerik:RadButton>
                                        </div>
                                    </EditItemTemplate>
                                    <InsertItemTemplate>
                                        <table>
                                            <tr>
                                                <td>
                                                    Data Id (INSERT)
                                                </td>
                                                <td>
                                                    <telerik:RadTextBox ID="txtDataId" runat="server" Text='<%# Bind("DataId") %>' />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Description
                                                </td>
                                                <td>
                                                    <telerik:RadTextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' />
                                                </td>
                                            </tr>
                                        </table>
                                        <div>
                                            <telerik:RadButton runat="server" CommandName="Insert" Text="Insert">
                                            </telerik:RadButton>
                                            <telerik:RadButton runat="server" CommandName="Cancel" Text="Cancel">
                                            </telerik:RadButton>
                                        </div>
                                    </InsertItemTemplate>
                                </asp:FormView>
                            </div>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </ContentTemplate>
            </telerik:RadWindow>
        </Windows>
    </telerik:RadWindowManager>
    <asp:ObjectDataSource runat="server" ID="FormViewDataSourceID" TypeName="FormViewDataSource"
        DataObjectTypeName="DataModel" SelectMethod="Select" UpdateMethod="Update" InsertMethod="Insert">
        <SelectParameters>
            <asp:Parameter runat="server" Name="id" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <div>
    </div>
    </form>
</body>
</html>

And code-behind:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void UpdatePanel_Unload_Workaround(object sender, EventArgs e)
    {
        var methods = typeof(ScriptManager).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
        var methodInfo = methods.Where(i => i.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")).First();
        methodInfo.Invoke(ScriptManager.GetCurrent(Page), new object[] { sender as UpdatePanel });
    }

    protected void OnEditModeClick(object sender, EventArgs e)
    {
        FormViewDataSourceID.SelectParameters[0].DefaultValue = "2";
        formView1.ChangeMode(FormViewMode.Edit);

        OpenDialog();
    }

    protected void OnInsertModeClick(object sender, EventArgs e)
    {
        formView1.ChangeMode(FormViewMode.Insert);

        OpenDialog();
    }

    private void OpenDialog()
    {
        StringBuilder windowShowScript = new StringBuilder();
        windowShowScript.Append("var oWindow = $find(\"").Append(dialogWnd1.ClientID).Append("\");");
        windowShowScript.Append("oWindow.show();");

        RadAjaxManager.GetCurrent(Page).ResponseScripts.Add(windowShowScript.ToString());
    }

    protected void formView1_OnItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Cancel") || e.CommandName.Equals("Insert") || e.CommandName.Equals("Update"))
        {
            // close rad window
            ScriptManager.RegisterStartupScript(updatePanel1, updatePanel1.GetType(), "cancelScript", "CloseDialog();", true);
        }
    }
}

public class DataModel
{
    public string DataId { get; set; }
    public string Description { get; set; }
}

public class FormViewDataSource
{
    private List<DataModel> models = new List<DataModel>()
            {
                new DataModel()
                    {
                        DataId = "1",
                        Description = "First Event"
                    },
                new DataModel()
                    {
                        DataId = "2",
                        Description = "Second Event"
                    }
            };

    public IEnumerable<DataModel> Select(int id)
    {
        return models.Where(i => i.DataId.Equals(id.ToString()));
    }
    public void Update(DataModel model)
    {
        var foundModel = models.Find(i => i == model);
        if (foundModel != null)
        {
            foundModel.DataId = model.DataId;
            foundModel.Description = model.Description;
        }
    }

    public void Insert(DataModel model)
    {
        models.Add(model);
    }
}
Was it helpful?

Solution

Move your RadWindows where their ContentTemplates are used out of a RadWindowManager. Then, you will not need the reflection workaround for the update panel anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top