The article about the custom grid control is finished. The post contains links to multiple page explaining the control and the source code has been added (with an example how to use the control).
Regards, Jan
Posted by Jan on June 23, 2007
In case you want to have a custom image for your control in the Visual Studio toolbox, you can do this by adding the following directive to your user control:
namespace CustomControlsLibrary
{
[System.Drawing.ToolboxBitmap(@"c:\CustomControl\Resources\toolbox.bmp")]
public class CustomControl : Control, INamingContainer
{ ….
Posted in asp, custom control | Leave a Comment »
Posted by Jan on June 22, 2007
In case you use delete buttons in a GridView (like below) it would be nice to ask the user for confirmation (message box).
There are two ways to achieve this:
The first options is the easiest to implement (only design, no code-behind). The second the most flexible: you can add information about the record in your question.
1. By using the ConfirmButtoExtender in Ajax
<%@ Register Assembly=”AjaxControlToolkit” Namespace=”AjaxControlToolkit”
TagPrefix=”ajaxToolkit” %>
<asp:GridView ID=”gridOverview” runat=”server” AutoGenerateColumns=”False”
AllowSorting=”false”
DataKeyNames=”selectcolId” DataSourceID=”odcColumns” AllowPaging=”false”
OnRowCommand=”gridOverview_OnRowCommand”
OnRowDataBound=”gridOverview_OnRowDataBound”>
<Columns>
<asp:BoundField DataField=”bookTitle” HeaderText=”Title” >
ItemStyle CssClass=”grid-column-small” />
<asp:TemplateField HeaderText=”Delete” >
<ItemStyle CssClass=”grid-column-buttons” />
<ItemTemplate>
<asp:ImageButton ID=”ibDelete” runat=”server”
CausesValidation=”False” CommandName=”Delete”
ImageUrl=”~/Images/icon-delete.gif” />
<ajaxToolkit:ConfirmButtonExtender ID=”ConfirmBtExt1″
runat=”server” TargetControlID=”ibDelete”
ConfirmText=’Are you sure about deleting this record?’ />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType=”Image”
SelectImageUrl=”~/Images/icon-pencil.gif” HeaderText=”Edit”
ShowHeader=”True” ShowSelectButton=”True” CausesValidation=”False”
sItemStyle-CssClass=”grid-column-buttons” />
<asp:TemplateField HeaderText=”Order”>
<ItemTemplate>
<asp:ImageButton ID=”ibColDown” runat=”server”
CommandName=”ColDown” ImageUrl=”~/Images/down.gif”
CommandArgument=”<%# Container.DataItemIndex %>“ />
<asp:ImageButton ID=”ibColUp” runat=”server”
CommandName=”ColUp” ImageUrl=”~/Images/up.gif”
CommandArgument=”<%# Container.DataItemIndex %>“ />
</ItemTemplate>
<ItemStyle CssClass=”grid-column-buttons” />
</asp:TemplateField>
</Columns>
</asp:GridView>
For more information, see ConfirmButton
2. By adding a confirmation box to each row when the rows are bound
OnRowDataBound=”gridOverview_OnRowDataBound”
The delete column (ItemTemplate) should be an (image) button in a template field (replace the squared area in the example above with this code):
<asp:TemplateField HeaderText=”Delete” >
<ItemStyle CssClass=”grid-column-buttons” />
<ItemTemplate>
<asp:ImageButton ID=”ibDelete” runat=”server”
CausesValidation=”False” CommandName=”Delete”
ImageUrl=”~/Images/icon-delete.gif” />
</ItemTemplate>
</asp:TemplateField>
Create the event handler ‘gridOverview_OnRowDataBound’ in the code behind. When each row is bound, this procedure will add a message box to the image button.
protected void gridOverview_OnRowDataBound(object sender, GridViewRowEventArgs e)
//Add delete msgbox to each row containing field
//information to identify the row
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete ImageButton
ImageButton ib = (ImageButton)e.Row.FindControl(“ibDelete”);
ib.Attributes.Add(“onclick”,“javascript:return ” +
confirm(‘Are you sure about deleting ” +
DataBinder.Eval(e.Row.DataItem, “bookTitle”) + “?’);”);
}
}
Posted in GridView, asp, confirmation, delete | 17 Comments »
Posted by Jan on June 22, 2007
I cannot count the hours I have spend searching for solutions to my ASP.NET problems. Too many. And as developing is not my daily work, I sometimes forget the how-to’s.
That’s why I started this blog: too gather this info in one place so that I do not have to look for it again. And maybe it wil help others too. Leave a comment if it is of any help.
PS: All the code is in C#
Posted in Uncategorized | Leave a Comment »