c# - 更新面板内的中继器 - ItemComand 代码问题 - 在代码隐藏中使用 ScriptManager 调用 javascript 函数

标签 c# javascript asp.net updatepanel scriptmanager

<分区>

我在 aspx 区域的中继器如下所示:
此中继器位于母版页内 - 页面基于母版页和内容页

<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;
                }
        }
    }
}

我的问题与这些行有关:从 ScriptManager.RegisterStartupScript 开始
为什么这些行在更新面板内不起作用? - 没有更新面板,一切都很好。

提前致谢

最佳答案

大概 ScriptManager.RegisterStartupScript 试图在“启动”时运行一些东西,即。当页面加载或类似时。使用 UpdatePanel,服务器只是向客户端发回一个 HTML block ,并将其插入到已加载的页面中。

如果没有 UpdatePanel,您将发回一个全新的页面,该页面将由浏览器以正常方式加载,完成整个页面加载过程。

因此,您可能需要在页面中已有脚本,并处理更新面板刷新的客户端事件。不幸的是,不记得如何做到这一点 - 也许是这样的? http://msdn.microsoft.com/en-us/library/bb397499.aspx

关于c# - 更新面板内的中继器 - ItemComand 代码问题 - 在代码隐藏中使用 ScriptManager 调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7737659/

相关文章:

javascript - 使用 JavaScript 在同一 GridView 中启用或禁用复选框更改事件上的文本框

c# selenium 通过导航验证所有链接 StaleElementReferenceException 控制台应用程序

javascript - 在 Javascript 中访问 TimePicker 值

javascript - 如果键存在,则使用键和值形成 JSON

javascript - 如何统计元素中JS、CSS、LESS、HTML文件的总代码行数?

asp.net - 我们可以在 asp.net aspx 页面中继承更多的类吗?

c# - C# worker 线程唤醒竞争条件

c# - System.Data.dll 附加信息 : Invalid object name 中发生类型 'System.Data.SqlClient.SqlException' 的异常

c# - Blazor wasm,网络核心托管,具有身份验证、应用程序用户和共享项目中我的模型之间的关系

asp.net - SignalR-ObjC 库的 SignalR 服务器错误 "The ConnectionId is in the incorrect format."