0

我在更新面板中有一个 GridView,还有一个用于上传图像的 AsyncFileUpload(在更新面板之外)。所有这些都在一个使用SHOWMODALDIALOG.

我要实现的目标:
1. 文件上传完成后,更新面板就会更新,gridview 会显示用户刚刚添加的图像。

我面临的问题:
1. Gridview 不刷新,即使文件上传到数据库后我有一个 Databind 事件。更新面板正在通过__doPostBack('UpdatePanelID',' ')调用的 JS 进行刷新OnClientUploadComplete(我正在通过在更新面板中显示当前时间的标签进行检查)
2. 每当我单击“保存更改”或“取消”时,ModalDialog 都会打开一个新页面' 添加图像后的按钮。如果我只是打开窗口并单击这些按钮,它们就可以正常工作。我确实尝试包含<base target="_self" />在 中<head>,最后解决了window.name="xxx" onload body

任何帮助表示赞赏。我的代码:

ASPX

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="RCMT0032.aspx.vb" Inherits="RCWKSHEET.RCMT0032" EnableEventValidation="false"%>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>

<html>
<head>
<base target="RCMT0032" />
  <title>RCMT0032</title>
    <script type="text/javascript">

        function readpasseddata() {
            window.name = 'RCMT0032';
            var rpt = window.dialogArguments;
            document.getElementById("HiddenReport").value = rpt;

        }
        function UploadComplete(sender, args) {
            __doPostBack('gvupd', '');
        }         
    </script>
</head>
<body onload="readpasseddata()" >
    <form id="Form1" method="post" runat="server" target="RCMT0032" >
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
    <asp:AsyncFileUpload id="BrowserHidden" Width="1" runat="server" OnClientUploadComplete="UploadComplete" OnUploadedComplete="BrowserHidden_UploadedComplete"/>
    <button class="ActionButton" id="btnSave" runat="server">Save & Exit</button>
    <button class="ActionButton" id="btnClose" runat="serverCancel</button>

    <asp:UpdatePanel id="gvupd" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
            <asp:Label runat="server" ID="Label1" />

            <asp:GridView id="GridView1" Runat="server" AutoGenerateColumns="False"
                    AllowPaging="false" EnableViewState="true" datakeynames="Seq">
                <Columns>
                <asp:TemplateField HeaderText="Comments">
                    <ItemTemplate><asp:TextBox ID="comments" Enabled="true" MaxLength="249" TextMode="MultiLine" Text='<%# Eval("Comments") %>'/>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Picture">
                    <ItemTemplate><asp:HyperLink id="PictHyper" runat="server" Target="_blank" ImageUrl='<%# String.Format("RCMT0033.aspx?report={0}&seq={1}", Eval("ReportNumber"), Eval("Seq"))%>' NavigateUrl='<%# String.Format("RCMT0034.aspx?report={0}&seq={1}", Eval("ReportNumber"), Eval("Seq"))%>' /></ItemTemplate></asp:TemplateField>         
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate><asp:CheckBox runat="server" ID="DeleteCB" /></ItemTemplate>
                </asp:TemplateField></Columns>
        </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    </div>
    </form>
 </body>
 </html>


背后的代码

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoaD
        If Not IsPostBack Then
            LoadData()
            DataBind()
        Else
            With ViewState
                _intReportNumber = CInt(.Item("Report"))
                _strVendorNumber = CStr(.Item("VendorNumber"))
                _strStatus = CStr(.Item("Status"))
            End With
        End If
    End Sub

    Public Sub LoadData()
    //GET DATA INTO DATATABLE DT
            GridView1.DataSource = dt
            GridView1.DataBind()
        Catch err As Exception
            Throw err
        End Try
    End Sub

Protected Sub UploadData()
        If BrowserHidden.PostedFile IsNot Nothing AndAlso BrowserHidden.PostedFile.FileName <> "" Then
            Try
                //UPLOAD STUFF
                GridView1.DataBind()
            Catch ex As Exception
            End Try
        End If
    End Sub

    Protected Sub btnClose_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.ServerClick
        canceladd()
        ScriptManager.RegisterStartupScript(Page, Me.GetType(), "onclick", "window.open('','_self',''); window.close();", True)
    End Sub


    Protected Sub BrowserHidden_UploadedComplete(ByVal sender As System.Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs)
        UploadData()
    End Sub


    Protected Sub gvupd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvupd.Load
        Label1.Text = DateTime.Now.ToString()
    End Sub

    Protected Sub btnSave_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.ServerClick
        FinalizeAdd()
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "window.open('','_self',''); window.close();", True)
    End Sub
4

1 回答 1

1

对于您的 #2 问题,我认为您不能使用客户端或服务器端脚本重新加载/刷新模式窗口。要归档相同或相似的结果,您可以让上传控件关闭模式窗口并返回一个值以指示需要重新打开。然后在父窗口中,您可以根据需要使用循环打开它。如果用户单击模态窗口上的关闭按钮或右上角的 X 按钮,模态将不会重新打开,因为它不会将“重新打开”值发送回父级。

父页面.aspx

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>
    <script type="text/javascript">
        function popModal(){
            var val = window.showModalDialog('ModalChild.aspx', '', '');
            while (val == "reopen") {
                val = window.showModalDialog('ModalChild.aspx', '', '');
            }
        }
    </script>
</head>
<body>
    <input type='button' value='Pop Modal' onclick='popModal();' />
</body>
</html>

ModalChild.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function closeAndReOpen(){
            window.returlValue = "reopen";
            window.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <div>
        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="closeAndReOpen();"
            OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />

        <input type='button' value='Close' onclick='window.close();' />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <img runat="server" id="img" /></ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="AsyncFileUpload1" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

ModalChild.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
    public partial class ModalChild: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadGrid();
            }
        }

        protected void LoadGrid()
        {
            var files = new DirectoryInfo(Server.MapPath("Data")).GetFiles("*.png");
            GridView1.DataSource = files;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var fi = e.Row.DataItem as FileInfo;
                var img = e.Row.FindControl("img") as HtmlImage;
                img.Src = @"Data\" + fi.Name;
            }
        }

        protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            if (AsyncFileUpload1.PostedFile != null)
            {
                var savePath = Server.MapPath(@"Data\" + Guid.NewGuid().ToString() + ".png");
                AsyncFileUpload1.SaveAs(savePath);
            }
        }
    }
}
于 2012-07-09T16:06:46.683 回答