更新面板中的中继器- itemcommand代码问题-调用javascript函数与ScriptManager在代码后面

本文关键字:代码 javascript 函数 ScriptManager 调用 问题 中继器 itemcommand 更新 | 更新日期: 2023-09-27 18:10:58

我在aspx区域的中继器如下:
this repeater is inside a master page - pages are base on master and content pages

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
        <ItemTemplate>
            <asp:Image ID="imgArrowIconInsideRepeater" runat="server" ImageUrl="~/Images/Login/ArrowIcon.png"
                />
            <asp:HiddenField ID="hfFilePath" runat="server" Value='<%# Eval("FilePath")%>' />
            <asp:HiddenField ID="hfFileName" runat="server" Value='<%# Eval("FileName")%>' />
            <asp:HiddenField ID="hfFileSize" runat="server" Value='<%# Eval("FileSize")%>' />
            <asp:HiddenField ID="hfFileCreationDate" runat="server" Value='<%# Eval("FileCreationDate")%>' />
            <asp:LinkButton ID="lbFile" runat="server" CommandName="lbFile_Click" CssClass="lbFileInRepeater"
                ><%# Eval("FileName")%></asp:LinkButton>
            <br />
            <asp:Label ID="lblFileCreationDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FileCreationDate", "{0:yyyy/MM/dd - tt h:m:s}") %>'
                CssClass="lblFileCreationDateInRepeater" ></asp:Label>
            |
            <asp:Label ID="lblFileSize" runat="server" Text='<%# GetFileSize(Eval("FileSize"))%>'
                CssClass="lblFileSizeInRepeater"></asp:Label>
            <div class="EmptyDiv">
            </div>
        </ItemTemplate>
    </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>

我也有一个脚本管理器在我的主页如下:

<telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="True">
</telerik:RadScriptManager>

我的c#代码后面的代码如下:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //The Below Line Does Not Work - Always Is Null
        //NewAddedFiles currentItem = (NewAddedFiles)e.Item.DataItem;
        HiddenField hfFilePath = (HiddenField)e.Item.FindControl("hfFilePath");
        HiddenField hfFileName = (HiddenField)e.Item.FindControl("hfFileName");
        HiddenField hfFileSize = (HiddenField)e.Item.FindControl("hfFileSize");
        HiddenField hfFileCreationDate = (HiddenField)e.Item.FindControl("hfFileCreationDate");
        switch (e.CommandName)
        {
            case "lbFile_Click":
                {
                    if (Session["User_ID"] != null)
                    {
                        DataSet dsDownload = DataLayer.Download.Size_By_UserID_Today(int.Parse(HttpContext.Current.Session["User_ID"].ToString()), DateTime.Now);
                        if (dsDownload.Tables["Download"].Rows.Count > 0)
                        {
                            DataRow drDownload = dsDownload.Tables["Download"].Rows[0];
                            int SumOfFileSize4Today = int.Parse(drDownload["FileSizSum"].ToString());
                            if (SumOfFileSize4Today + int.Parse(hfFileSize.Value) <= 1073741824)//1 GB = 1024*1024*1024 bytes = 1073741824 bytes
                            //if (SumOfFileSize4Today + int.Parse(hfFileSize.Value) <= 100000)
                            {
                                DataLayer.Download.InsertRow(
                                           int.Parse(HttpContext.Current.Session["User_ID"].ToString()),
                                           DateTime.Now,
                                           hfFilePath.Value,
                                           hfFileName.Value,
                                           hfFileSize.Value,
                                           DateTime.Parse(hfFileCreationDate.Value)
                                         );
                                Response.Redirect("~/HandlerForRepeater.ashx?path=" + hfFilePath.Value);
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "YouCanNotDownloadAnyMore_SizeOverload", "YouCanNotDownloadAnyMore_SizeOverload();", true);
                            }
                        }
                        else
                        {
                            if (int.Parse(hfFileSize.Value) <= 1073741824)
                            //if (int.Parse(hfFileSize.Value) <= 100000)
                            {
                                DataLayer.Download.InsertRow(
                                           int.Parse(HttpContext.Current.Session["User_ID"].ToString()),
                                           DateTime.Now,
                                           hfFilePath.Value,
                                           hfFileName.Value,
                                           hfFileSize.Value,
                                           DateTime.Parse(hfFileCreationDate.Value)
                                         );
                                Response.Redirect("~/HandlerForRepeater.ashx?path=" + hfFilePath.Value);
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "YouCanNotDownloadAnyMore_SizeOverload", "YouCanNotDownloadAnyMore_SizeOverload();", true);
                            }
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "plzLoginFirst_ForDownload", "plzLoginFirst_ForDownload();", true);
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
}

我的问题是关于这些行:start with ScriptManager.RegisterStartupScript
为什么这些行不能在更新面板中工作?-没有更新面板,一切正常。

thanks in advance

更新面板中的中继器- itemcommand代码问题-调用javascript函数与ScriptManager在代码后面

假定ScriptManager.RegisterStartupScript试图在"启动"上运行某些东西,即。当页面加载或类似时。使用UpdatePanel,服务器只是向客户端发回一段HTML,将其插入到已经加载的页面中。

如果没有UpdatePanel,你将发送回一个全新的页面,它将由浏览器以正常的方式加载,通过整个页面加载过程。

因此,您可能需要在页面中已经有脚本,并处理更新面板刷新的客户端事件。不幸的是,我一时想不起来怎么做这个——也许是这样的?http://msdn.microsoft.com/en-us/library/bb397499.aspx

这个问题通过以下更改解决:

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), 
    "plzLoginFirst_ForDownload", "plzLoginFirst_ForDownload();", true);