1

大家好,我在我的应用程序中使用 RadGrid,因为我希望我的 RadGrid 不刷新我在更新面板中有我的 RadGrid,如下所示

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="RadGrid1" EventName="ItemCommand" />
                <%--<asp:PostBackTrigger ControlID="RadGrid1" />--%>
            </Triggers>
            <ContentTemplate>
                <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
                    OnNeedDataSource="RadGrid1_NeedDataSource">
                    <MasterTableView Width="950" AutoGenerateColumns="false" DataKeyNames="EmpID" GridLines="None"
                        TableLayout="Auto">
                        <Columns>
                            <telerik:GridBoundColumn DataField="EmpID" HeaderText="Emp ID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left"
                                ItemStyle-HorizontalAlign="Left" UniqueName="EmpID" FilterControlWidth="30px"
                                AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
                            <telerik:GridButtonColumn DataTextField="ButtonName" ItemStyle-ForeColor="Blue" CommandName="Generate"
                                ConfirmTextFields="ButtonName" ConfirmTextFormatString="Would you like to  {0} ACH file ?"
                                ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="ButtonName" ConfirmTitle="ACH File">
                            </telerik:GridButtonColumn>
                            <telerik:GridBoundColumn DataField="EmployeeName" HeaderText="Employee Name" ReadOnly="true"
                                HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" UniqueName="EmployeeName"
                                FilterControlWidth="60px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </ContentTemplate>
        </asp:UpdatePanel>

当我单击下载按钮时,我无法下载文件,这是我在 Itemcommand 中的代码

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Generate")
        {
            Response.ContentType = "text/plain";
            Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
            Response.AddHeader("content-length", "0");
            Response.Flush();
            Response.End();
        }
    }

有人可以帮我解决这个问题吗AsyncPostBackTrigger

4

1 回答 1

2

您不能使用 AsyncPostBack 调用 Response。这是 Ajax 框架的限制。

2013 年 1 月 28 日更新

由于您使用的是 Telerik,我更喜欢使用 RadAjaxManager。基本上,当单击生成按钮时,它使用常规回发而不是 ajax。在我的示例中,排序仍然使用 ajax。

<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="false" AllowSorting="True" runat="server" 
    OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="EmpID">
        <Columns>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:Button runat="server" CommandName="Generate" ID="GenerateButton" 
                        Text="Generate" OnClientClick="Generate(this, event); return false;" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="EmpID" HeaderText="Emp ID" UniqueName="EmpID" SortExpression="EmpID" />
            <telerik:GridBoundColumn DataField="EmployeeName" HeaderText="Employee Name" UniqueName="EmployeeName" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<%-- RadAjaxManager --%>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<%-- RadAjaxLoadingPanel --%>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
        function Generate(sender, e) {
            $find("<%= RadAjaxManager1.ClientID %>").__doPostBack(sender.name, "");
        }
    </script>
</telerik:RadCodeBlock>

public class Employee
{
    public int EmpID { get; set; }
    public string EmployeeName { get; set; }
}

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = new List<Employee>
        {
            new Employee {EmpID = 1, EmployeeName = "John"},
            new Employee {EmpID = 2, EmployeeName = "Marry"},
            new Employee {EmpID = 3, EmployeeName = "Eric"}
        };
}

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "Generate")
    {
        Response.ContentType = "text/plain";
        Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
        Response.AddHeader("content-length", "0");
        Response.Flush();
        Response.End();
    }
}

在此处输入图像描述

于 2013-01-25T18:00:45.167 回答