ModyncDialog上的UpdatePanel内的Gridview在AsyncFileUpload之后没有更新

时间:2012-07-06 18:28:13

标签: asp.net gridview updatepanel postback asyncfileupload

我在更新面板中有一个GridView,以及我用来上传图像的AsyncFileUpload(更新面板外部)。所有这些都在使用SHOWMODALDIALOG调用的ASPX页面中。

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

我面临的问题:
1.即使文件上传到数据库后我有一个Databind事件,Gridview也不会刷新。更新面板通过__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

1 个答案:

答案 0 :(得分:1)

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

ParentPage.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);
            }
        }
    }
}