Emailid
Password
         
  
    Forgot password

New user Sign Up
 

Data Grid ASP.net 1.1 and JavaScript

       Current Rating:  40%                                                     Total Members Rated:  1
                                                                     Send To Friend

  

Data Grid ASP.net 1.1 and JavaScript

 

 

Beginning

 

Some good code tutorials instruct you to enable javascript confirmation dialog box in asp.net 1.1 . But there is nothing to show you how to do it if your link is in a datagrid. Here in this article we see 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. Insert the following column in your Datagrid. And it is good resulting.

 

Column

 

<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>

 

 

Later, continue 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. Now in the code behind fill in the OnItemCreate function with the following code.

 

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 ?');");

            }

        }

 

Conclusion

 

When the Control is rendered during the Databind an ItemCreated event is fired for each row. When ever that happens fetch for the control name when it is found. Also add the appropriate javascript to it. The bothersome fact is Y wont VS let me directly add it in the ASP code. This issue if fixed in asp.net 2.0. Hope, it is helpful. All the best!


                           Rate This Article:   

Author is Offline
  Author: Hesus Oliver Pecce
       


Comments Posted
Label
Subject Author Status Date

Good!

Boney Fairholm 26/06/2008

 

Post Comment

Related Articles
N-Tier in ASP.NET or other .NET apps
Make your web site ‘perfect and Search Engine Friendly for Google and Yahoo
On making your content neat and tidy
Web site templates and its uses
Web Traffic and Higher Page Rank



Home | About Us | Site Map | Privacy Policy | Submit Links