Click here to Skip to main content
Click here to Skip to main content

Javascript confirmation/alert in a DataGrid ASP.net 1.1

, 16 Jan 2007
Rate this:
Please Sign up or sign in to vote.
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
        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

Share

About the Author

jediaka

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> Pinmemberbala1758217-Sep-08 4:46 
Questionwhere i write server side coding? Pinmembersri baski26-Mar-07 1:43 
AnswerRe: where i write server side coding? Pinmemberjediaka2-Apr-07 10:35 
GeneralRe: where i write server side coding? Pinmembersri baski2-Apr-07 18:01 
GeneralRe: problem in my coding help me!!!!? Pinmembersri baski2-Apr-07 18:32 
Hi,
-I have creeated item command event.and i write my own coding there.
- Then i have set datakey value for datagrid property.
- But iam not able to get my deletion id and also am not able display my testing string(Before deletion)
 
html coding:
------------
<asp:TemplateColumn HeaderText="Delete">

<asp:ImageButton ImageUrl="Images/ed.gif" CommandName="Delete2" AlternateText="Delete" Runat="server" ID="Img_Delete">


 

server side coding
-------------------
private void OnItemDeletion(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write("Before Deletion");
if(e.CommandName == "Delete2")
{
int ids=int.Parse(Dgrid_Product.Items[e.Item.ItemIndex].Cells[0].Text);
Response.Write("Delete Id:"+ids);
return;
}

}
GeneralRe: problem in my coding help me!!!!? Pinmemberjediaka5-Apr-07 6:48 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 16 Jan 2007
Article Copyright 2007 by jediaka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid