Reputation: 185
I am new ASP.NET Web Forms developer and I am trying to develop a simple application using ObjectDataSource with Repository Pattern. I am struggling now with two issues related to the ASP.NET LinkButton inside ASP.NET GridView control. The issues are related to the click event of a Delete LinkButton that resides inside the GridView.
Here's my ASP.NET Code:
<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" />
<%-- --%>
<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>
I would like to show a Javascript Alert message once the user click on Delete button. 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.
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.
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. And here's the C# code for the Business Logic for the Product model:
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.
Thanks for your help in advance.
Upvotes: 0
Views: 1616
Reputation: 11
Handle the YourOjbectDataSource_Selected event.
In your repository:
if (error)
{
throw new YourException(ErrorMessage);
}
In your 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;
}
}
}
Upvotes: 1
Reputation: 9878
For a confirmation on GridView, you can use the Javascript Confirm and call the client Script using OnClientClick event. You can remove the span element. Code for itemtemplate should look something like below.
OnClientClick Gets or sets the client-side script that executes when a Button control's Click event is raised.
<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. You may use the GridView RowCommand Event.
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. It doesn't matter if you are using the Repository Pattern, you just need to place code for Label at DeleteProduct Method. Modified code for DeleteProduct Method, replace LabelControlID with actual ID for Label Control.
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;
}
}
Upvotes: 2
Reputation: 504
You can use the RowCommand event of the GridView to capture which command is firing ("Update", "Cancel", etc.) and handle it within that method. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx
Upvotes: 0