我想要一个带有 FormView 的 RadWindow 对话框,我可以在其中根据 FormView 的模式执行更新或插入操作。我创建了一个 RadWindow 对话框,添加了控件 (FormView) 作为内容,并通过数据绑定填充了 FormView 的内容。在我尝试执行保存、插入或取消操作之前,一切似乎都很好。不会触发相应的事件。
在我更改其模式后,FormView 似乎不会调用 ItemCommand 或任何其他事件。有人有一些见解吗?
我设法将此功能提取到一个简单的示例中,其中我只有默认页面和 RadWindow。
这是我的标记:
<%@ 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>
和代码隐藏:
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);
}
}