简体   繁体   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?

I am new ASP.NET Web Forms developer and I am trying to develop a simple application using ObjectDataSource with Repository Pattern. 我是ASP.NET Web Forms的新开发人员,我正在尝试使用ObjectDataSource和Repository Pattern开发一个简单的应用程序。 I am struggling now with two issues related to the ASP.NET LinkButton inside ASP.NET GridView control. 我现在正在努力解决与ASP.NET GridView控件内的ASP.NET LinkBut​​ton相关的两个问题。 The issues are related to the click event of a Delete LinkButton that resides inside the GridView. 这些问题与位于GridView内的Delete LinkBut​​ton的click事件有关。

Here's my ASP.NET Code: 这是我的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. I would like to show a Javascript Alert message once the user click on Delete button. 用户单击“删除”按钮后,我想显示一个Javascript Alert消息。 The code is there as shown above but the message doesn't show up when the user clicks on the button and I don't know why. 代码如上所示,但是当用户单击按钮并且我不知道为什么时,消息不会出现。

  2. The delete function works well but I would like to show a message of success of failure on an ASP.NET Label control that resides outside the GridView control. 删除功能运行良好,但是我想在驻留在GridView控件外部的ASP.NET Label控件上显示成功失败的消息。

Could you please tell me how I can fix these two issues? 您能否告诉我如何解决这两个问题?

UPDATE: 更新:

As you can see in my ASP.NET code, I am using ObjectDataSource with the Repository Pattern. 如您在ASP.NET代码中所看到的,我正在将ObjectDataSource与存储库模式一起使用。 And here's the C# code for the Business Logic for the Product model: 这是产品模型的业务逻辑的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;
            }
        }

So how can I be able to display a message upon the Deletion method? 那么, 如何显示有关“删除”方法的消息? Please note that I have an ASP.NET Label control resides outside the GridView Control. 请注意,我有一个ASP.NET Label控件位于GridView控件之外。

Thanks for your help in advance. 感谢您的帮助。

For a confirmation on GridView, you can use the Javascript Confirm and call the client Script using OnClientClick event. 要在GridView上进行确认,可以使用Javascript Confirm并使用OnClientClick事件调用客户端脚本。 You can remove the span element. 您可以删除span元素。 Code for itemtemplate should look something like below. itemtemplate的代码应如下所示。

OnClientClick Gets or sets the client-side script that executes when a Button control's Click event is raised. 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>

For displaying the error message of success of failure on an ASP.NET Label control that resides outside the GridView control. 用于在GridView控件之外的ASP.NET Label控件上显示失败成功的错误消息。 You may use the GridView RowCommand Event. 您可以使用GridView RowCommand事件。

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

EDIT 2: Update for Showing message. 编辑2:更新为显示消息。 It doesn't matter if you are using the Repository Pattern, you just need to place code for Label at DeleteProduct Method. 使用存储库模式无关紧要,只需将Label代码放置在DeleteProduct方法上即可。 Modified code for DeleteProduct Method, replace LabelControlID with actual ID for Label Control. 修改了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;
            }
        } 

Handle the YourOjbectDataSource_Selected event. 处理YourOjbectDataSource_Selected事件。

In your repository: 在您的存储库中:

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

In your aspx: 在您的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;
            }
        }
    }

You can use the RowCommand event of the GridView to capture which command is firing ("Update", "Cancel", etc.) and handle it within that method. 您可以使用GridView的RowCommand事件捕获正在触发的命令(“更新”,“取消”等),并在该方法中对其进行处理。 https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx 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