从 GridTemplateColumn 中的超链接字段打开 RadWindow

Opening a RadWindow from a hyperlink field in a GridTemplateColumn

我的父页面上有一个 RadGrid,其中包含一个包含 asp 超链接字段的 GridTemplateColumn。在网格的 ItemDataBound 事件中,我正在执行以下操作以在 RadWindow 中打开一个页面(window 中的页面负责所有逻辑):

hyperlink.Attributes.Add("onclick", string.Format("openDialogWindow({0}, '{1}', '{2}');", productId, categoryId, typeId));

这是JavaScript函数,用来打开window里面的页面:

function openDialogWindow(productId, categoryId, typeId) {
    openRadWindow("DialogPage.aspx?ProductId=" + productId + "&CategoryId=" + categoryId + "&TypeId=" + typeId, "dialog title", 800, 600);
}

我更喜欢在我的父页面上添加一个 RadWindow,并在其内容模板内添加一个用户控件,而不是这样做。用户控件将显示 DialogPage.aspx 所做的一切。父页面将设置控件的所有必要属性(productId、categoryId、typeId),因此当 window 打开时,将显示正确的信息,具体取决于他们在网格的哪个单元格上单击超链接按钮。

这样的事情可能吗?或者创建一个单独的页面,在 RadWindow 中打开并将查询字符串参数传递给它是最好的方法。我现在就是这样。

请尝试使用以下代码片段。

WebForm1.aspx

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

<%@ Register TagPrefix="uc" TagName="usercontrol" Src="~/WebUserControl1.ascx" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">

        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="RadGrid1">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                            <telerik:AjaxUpdatedControl ControlID="RadWindow1" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                </AjaxSettings>
            </telerik:RadAjaxManager>
            <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server"></telerik:RadAjaxLoadingPanel>
            <telerik:RadWindowManager ID="RadWindowManager1" runat="server">
            </telerik:RadWindowManager>
            <telerik:RadWindow ID="RadWindow1" runat="server">
                <ContentTemplate>
                    <uc:usercontrol ID="usercontrol1" runat="server" />
                </ContentTemplate>
            </telerik:RadWindow>
            <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
                AutoGenerateColumns="false">
                <MasterTableView>
                    <Columns>
                        <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn>
                            <ItemTemplate>
                                <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
        </div>
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TelerikWebApp1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object source, System.EventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
        new { ID = 1, Name ="Name1"},
        new { ID = 2, Name ="Name2"} };
            RadGrid1.DataSource = data;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button Button1 = sender as Button;
            GridDataItem item = Button1.NamingContainer as GridDataItem;
            string strID = item["ID"].Text;
            (usercontrol1.FindControl("Label1") as Label).Text = strID;
            RadWindow1.VisibleOnPageLoad = true;
        }


    }
}

WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="TelerikWebApp1.WebUserControl1" %>
<asp:Label ID="Label1" runat="server"></asp:Label>

如有任何疑问,请告诉我。