Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET
Article

Javascript confirmation/alert in a DataGrid ASP.net 1.1

Rate me:
Please Sign up or sign in to vote.
4.64/5 (10 votes)
16 Jan 20071 min read 58.7K   18   6
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">
                        <ItemTemplate>
                            <asp:LinkButton ID="lkDelete2" Runat="server" CommandName="Delete2" OnClient="return confirm('Are you sure you want to delete ?');">
                        Delete
                    </asp:LinkButton>
                        </ItemTemplate>
                    </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
C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
Hi my name is Michel, most know me as Jedi_aka.
I have a bachelor degree in CS and a Master degree in Computer science ( AI ).

Comments and Discussions

 
GeneralWonderful article. Thank you <eom></eom> Pin
bala1758217-Sep-08 4:46
bala1758217-Sep-08 4:46 
Questionwhere i write server side coding? Pin
sri baski26-Mar-07 1:43
sri baski26-Mar-07 1:43 
AnswerRe: where i write server side coding? Pin
jediaka2-Apr-07 10:35
jediaka2-Apr-07 10:35 
GeneralRe: where i write server side coding? Pin
sri baski2-Apr-07 18:01
sri baski2-Apr-07 18:01 
GeneralRe: problem in my coding help me!!!!? Pin
sri baski2-Apr-07 18:32
sri baski2-Apr-07 18:32 
GeneralRe: problem in my coding help me!!!!? Pin
jediaka5-Apr-07 6:48
jediaka5-Apr-07 6:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.