【问题标题】:Why doesn't row command properly work?为什么行命令不能正常工作?
【发布时间】:2016-10-09 16:45:07
【问题描述】:

我在 gridview 的 ROWCOMMAND 中调用了一个下载函数。当我单击它时它可以工作,但如果我单击(调用)rowcommand 中的另一个函数,它就会停止工作。它不会抛出异常。我尝试调试每一步,但没有运气。

为什么?

下载功能:

public void DownloadFile(string FileName)
{
    try
    {
        string filePath = FileName;
        string fullFilePath = Server.MapPath("../../SiteImages/BPA/" + filePath);
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(fullFilePath) + "\"");
        Response.ContentType = ContentType;
        Response.TransmitFile(fullFilePath);
    }

行命令:

protected void grdViewUploadedMaterialOther_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {

        if (e.CommandName == "Download")
        {
            mdlImgPreview.Hide();

            string FileName = Convert.ToString(e.CommandArgument);

            DownloadFile(FileName);

            return;
        }

        if (e.CommandName == "View")
        {
            imgPreviewed.ImageUrl = "../../SiteImages/BPA/" + e.CommandArgument.ToString();
            mdlImgPreview.Show();
        }

    }
    catch (Exception ex)
    {
        ResultLabel.ResultLabelAttributes(ex.Message, ProjectUserControls.Enums.ResultLabel_Color.Red);
    }
    finally
    {
        ResultPanel.Controls.Add(ResultLabel);
    }

Rowdatabound:注册按钮

protected void grdViewUploadedMaterialOther_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            LinkButton lb = e.Row.FindControl("btnLinkDownload") as LinkButton;
            RegisterDownloadButton(lb);
        }
        catch (Exception ex)
        {
            ResultLabel.ResultLabelAttributes(ex.Message, ProjectUserControls.Enums.ResultLabel_Color.Red);
        }
        finally
        {
            ResultPanel.Controls.Add(ResultLabel);
        }
    }

注册功能:

public void RegisterDownloadButton(LinkButton lb)
    {
        try
        {

            if (lb != null)
                ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);


        }
        catch (Exception ex)
        {
            ResultLabel.ResultLabelAttributes(ex.Message, ProjectUserControls.Enums.ResultLabel_Color.Red);
        }
        finally
        {
            ResultPanel.Controls.Add(ResultLabel);
        }
    }

网格视图:

<asp:GridView runat="server" ID="grdViewUploadedMaterialOther" Width="100%" OnRowDataBound="grdViewUploadedMaterialOther_RowDataBound"
    OnRowCommand="grdViewUploadedMaterialOther_RowCommand" HeaderStyle-BackColor="#99CC99"
    DataKeyNames="Pk_UploadedMaterialOther_ID"
    AutoGenerateColumns="false"
    CssClass="table table-condensed table-bordered table-striped table-responsive">
    <PagerSettings Mode="Numeric" />
    <PagerStyle HorizontalAlign="Center" CssClass="gvwCasesPager" />
    <Columns>

        <asp:TemplateField HeaderText="View Attachment">
            <ItemTemplate>
                <asp:LinkButton ID="btnLnkViewAttachment" runat="server" Text="View" CommandArgument='<%# Eval("MaterialPath") %>' CommandName="View"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Download">
            <ItemTemplate>
                <asp:LinkButton ID="btnLinkDownload" runat="server" Text='<%# Eval("MaterialPath") %>'
                    CommandArgument='<%# Eval("MaterialPath") %>' CommandName="Download"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/assets/global/images/delete.png"
                    CommandName="cmdDelete" CommandArgument='<%# Container.DataItemIndex %>'
                    ControlStyle-Width="20px"
                    ControlStyle-Height="20px" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

【问题讨论】:

    标签: c# asp.net c#-4.0 file-upload rowcommand


    【解决方案1】:

    尝试将 updatepanel 控件添加到 gridview 以解决您的问题。

    参考下面的代码,这可能对你有帮助。

    <asp:TemplateField HeaderText="Download">
            <ItemTemplate>
              <asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="fileUploadPanel">
            <ContentTemplate>
                        <asp:LinkButton ID="btnLinkDownload" runat="server" Text='<%# Eval("MaterialPath") %>'
                                CommandArgument='<%# Eval("MaterialPath") %>' CommandName="Download"></asp:LinkButton>
            </ContentTemplate>
    
                         <Triggers>
                            <asp:PostBackTrigger ControlID="btnLinkDownload" />
                         </Triggers>
        </asp:UpdatePanel> 
            </ItemTemplate>
        </asp:TemplateField>
    

    【讨论】:

    • 上面的代码可能会被解决,因为它保存了每一行的controlID..您是否尝试过代码中的所有模板字段..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2016-07-16
    • 2019-01-04
    • 2020-09-03
    • 2016-10-10
    相关资源
    最近更新 更多