65.9K
CodeProject is changing. Read more.
Home

Javascript confirmation/alert in a DataGrid ASP.net 1.1

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (9 votes)

Jan 16, 2007

1 min read

viewsIcon

59197

There is a lot of good code tutorials out there that teach you how to enable javascript confirmation dialog box in asp.net 1.1 ( easily done in asp.net 2.0 ). However there is none that show you how to do it if your button/link is in a datagrid. This article addresses this gap.

Introduction

There is a lot of good code tutorials out there that teach you how to enable javascript confirmation dialog box in asp.net 1.1 ( easily done in asp.net 2.0 ). However there is none that show you how to do it if your button/link is in a datagrid. This article addresses this gap. Let m_dg1 be the datagrid that will contain a list of linkbutton (lkDelete2) in our case used to delete a specific row of data in your table. Add the following column in your Datagrid.
<asp:datagrid id="m_dg1" style="Z-INDEX: 111; LEFT: 208px; POSITION: absolute; TOP: 224px"
                runat="server" >
                <Columns>

                    <asp:TemplateColumn HeaderText="Delete Referral 1">
                        
                            <asp:LinkButton ID="lkDelete2" Runat="server" CommandName="Delete2" OnClient="return confirm('Are you sure you want to delete ?');">
                        Delete
                    </asp:LinkButton>
                        
                    </asp:TemplateColumn>
                </Columns>
            </asp:datagrid>
You can then go on to add an ItemCreated Event handler for that datagrid. For the sake of our exercise we will call this hander OnItemCreate If you don't know how to add an event handler for your datagrid Do the following: - Select your DataGrid in Design mode ( Not HTML) - In the Property window click on the event Button ( the Lightening buttonn Yellow on top ). - Type the name of the function that you want to handle any specific event ( in our case the event is ItemCreated and the function name is OnItemCreate. - Hit Enter. We are almost done. Now in the code behind fill in the OnItemCreate function with the following code
        private void OnItemCreate(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            Control clb = (Control)e.Item.FindControl("lkDelete2");
            if(clb!=null && clb.GetType() != typeof(LinkButton))
                return;
            LinkButton lb = (LinkButton)clb;
            if(lb != null)
            {
                lb.Attributes.Add("onClick", "return confirm('Are you sure you want to delete ?');");
            }
        }
Done. Explanations ? Well when the Control is rendered during the Databind an ItemCreated event is fired for each row. When ever that happens I fetch for my control name when I find it , I add the appropriate javascript to it. Not that magic actually. What is annoying is Y wont VS let me directly add it in the ASP code. But then again who am I to discuss Microsoft ways. This issue if fixed in asp.net 2.0.