簡體   English   中英

如何在駐留在ASP.NET GridView中的ASP.NET LinkBut​​ton的click事件中顯示消息?

[英]How to disply message upon the click event of ASP.NET LinkButton residing inside ASP.NET GridView?

我是ASP.NET Web Forms的新開發人員,我正在嘗試使用ObjectDataSource和Repository Pattern開發一個簡單的應用程序。 我現在正在努力解決與ASP.NET GridView控件內的ASP.NET LinkBut​​ton相關的兩個問題。 這些問題與位於GridView內的Delete LinkBut​​ton的click事件有關。

這是我的ASP.NET代碼:

<asp:UpdatePanel ID="upView" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
<asp:Label ID="lblMessage" runat="server" Text="" CssClass="lead text-info"></asp:Label>
                        <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false"
                            DataSourceID="odsProduct" DataKeyNames="Id"
                            CssClass="table table-bordered table-striped">
                            <Columns>
                                <asp:TemplateField ShowHeader="False">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lbtnEdit"
                                            runat="server"
                                            CssClass="btn btn-info btn-sm"
                                            CommandName="Edit"
                                            Text="Edit" />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:LinkButton ID="lbtnUpdate"
                                            runat="server"
                                            CssClass="btn btn-success btn-sm"
                                            CommandName="Update"
                                            Text="Update" />
                                        <%--&nbsp;--%>
                                        <asp:LinkButton ID="lbtnCancel"
                                            runat="server"
                                            CssClass="btn btn-default btn-sm"
                                            CommandName="Cancel"
                                            Text="Cancel" />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <span onclick="return confirm('Are you certain you want to delete this 
                                                product?');">
                                            <asp:LinkButton ID="lbtnDelete" runat="server"
                                                CssClass="btn btn-danger btn-sm"
                                                CommandName="Delete">
                                            <span class="glyphicon glyphicon-trash"></span> Delete
                                            </asp:LinkButton>
                                        </span>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" ItemStyle-VerticalAlign="Top" />
                                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-VerticalAlign="Top" />
                                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ItemStyle-VerticalAlign="Top" />
                                <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="UnitPrice" />
                            </Columns>
                        </asp:GridView>
                        <asp:ObjectDataSource ID="odsProduct" runat="server"
                            TypeName="ThinkSafetyFirst_DatabaseFirst.BLL.ProductBL"
                            DataObjectTypeName="ThinkSafetyFirst_DatabaseFirst.Models.TTSF_Product"
                            SelectMethod="GetProducts"
                            DeleteMethod="DeleteProduct"
                            UpdateMethod="UpdateProduct">
                        </asp:ObjectDataSource>
                </ContentTemplate>
            </asp:UpdatePanel>
  1. 用戶單擊“刪除”按鈕后,我想顯示一個Javascript Alert消息。 代碼如上所示,但是當用戶單擊按鈕並且我不知道為什么時,消息不會出現。

  2. 刪除功能運行良好,但是我想在駐留在GridView控件外部的ASP.NET Label控件上顯示成功失敗的消息。

您能否告訴我如何解決這兩個問題?

更新:

如您在ASP.NET代碼中所看到的,我正在將ObjectDataSource與存儲庫模式一起使用。 這是產品模型的業務邏輯的C#代碼:

public void DeleteProduct(TTSF_Product product)
        {
            try
            {
                productRepository.DeleteProduct(product);
            }
            catch (Exception ex)
            {
                //Include catch blocks for specific exceptions first,
                //and handle or log the error as appropriate in each.
                //Include a generic catch block like this one last.
                throw ex;
            }
        }

那么, 如何顯示有關“刪除”方法的消息? 請注意,我有一個ASP.NET Label控件位於GridView控件之外。

感謝您的幫助。

要在GridView上進行確認,可以使用Javascript Confirm並使用OnClientClick事件調用客戶端腳本。 您可以刪除span元素。 itemtemplate的代碼應如下所示。

OnClientClick獲取或設置在引發Button控件的Click事件時執行的客戶端腳本。

<asp:TemplateField>
  <ItemTemplate>
      <asp:LinkButton ID="lbtnDelete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this Product?');"
          CssClass="btn btn-danger btn-sm" CommandName="Delete">
        <span class="glyphicon glyphicon-trash"></span>Delete</asp:LinkButton>
  </ItemTemplate>
</asp:TemplateField>

用於在GridView控件之外的ASP.NET Label控件上顯示失敗成功的錯誤消息。 您可以使用GridView RowCommand事件。

protected void GridView1_RowCommand(object sender,  GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                if (success) { lblResult.Text = "Success"; }
                else { lblResult.Text = "Failure"; }
            }
        }

編輯2:更新為顯示消息。 使用存儲庫模式無關緊要,只需將Label代碼放置在DeleteProduct方法上即可。 修改了DeleteProduct方法的代碼,將LabelControlID替換為Label Control的實際ID。

public void DeleteProduct(TTSF_Product product)
        {
            try
            {
                productRepository.DeleteProduct(product);
                LabelControlID.Text = "Success";
            }
            catch (Exception ex)
            {
                //Include catch blocks for specific exceptions first,
                //and handle or log the error as appropriate in each.
                //Include a generic catch block like this one last.
               LabelControlID.Text = "Failure"; 
               throw ex;
            }
        } 

處理YourOjbectDataSource_Selected事件。

在您的存儲庫中:

        if (error)
        {
            throw new YourException(ErrorMessage);
        }

在您的aspx中:

protected void YourOjbectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.Exception != null)
        {
            if (e.Exception.InnerException is YourException)
            {
                e.ExceptionHandled = true;
                lblErrorMessage.Text = e.Exception.InnerException.Message;
            }
        }
    }

您可以使用GridView的RowCommand事件捕獲正在觸發的命令(“更新”,“取消”等),並在該方法中對其進行處理。 https://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM