Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

PreRender Event

In this article I will explain asp.net GridView.PreRender Event with example.

PreRender Event:

PreRender Event Occurs after the GridView Control is loaded but prior to rendering.

OnPreRender Method:

OnPreRender method raises asp.net gridview PreRender Event.

Example:

ASPX Code:

<asp:GridView ID="GrdProducts" runat="server AutoGenerateColumns="False"
OnPreRender="GrdProducts_PreRender"
OnRowUpdating="GrdProducts_RowUpdating" >          
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate> 
<asp:label ID="lblProductId" runat="Server" Text='<%# Eval("ProductID") %>' ></asp:label>  
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>      
<asp:label ID="txtProductName" runat="Server" Text='<%# Eval("ProductName") %>' ></asp:label>
</ItemTemplate>  
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>      
<asp:label ID="txtProductPrice" runat="Server" Text='<%# Eval("Price") %>' ></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C# Code:

protected void GrdProducts_PreRender(object sender, EventArgs e)       
{
    //We don’t want some products get edited, deleted we can
    //perform this logic in this event
   GrdProducts.Rows[1].Enabled = false;
   GrdProducts.Rows[2].Enabled = false;
   GrdProducts.Rows[4].Enabled = false;
        }
 

 

GridView Init Event

In this article I will explain asp.net GridView.Init Event with example.

Init Event:

Init event occurs when the gridview server control is initialized, which is the first step in its lifecycle.

OnInit Method:

OnInit method raises asp.net gridview Init Event.

Example:

ASPX Code:

<asp:GridView ID="GrdProducts" runat="server AutoGenerateColumns="False"
OnInit="GrdProducts_Init" >          
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate> 
<asp:label ID="lblProductId" runat="Server" Text='<%# Eval("ProductID") %>' ></asp:label>  
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>      
<asp:label ID="txtProductName" runat="Server" Text='<%# Eval("ProductName") %>' ></asp:label>
</ItemTemplate>  
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>      
<asp:label ID="txtProductPrice" runat="Server" Text='<%# Eval("Price") %>' ></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C# Code:

  protected void GrdProducts_Init(object sender, EventArgs e)
        {
        //check if gridview data source is null , if null assoicate it and bind data
            if (GrdProducts.DataSource == null)
            {
                GrdProducts.DataSource = BindGridView();
                GrdProducts.DataBind();
            }
        }

 

Sorted Event

In this article I will explain asp.net GridView.Sorted Event with example.

Sorted Event:

Sorted Event occurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation.

OnSorted Method:

OnSorted method raises asp.net gridview Sorted Event.

In Detail:

The Sorted event is raised when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation. This enables you to provide an event-handling method that performs a custom logic, such as custom sorting, whenever this event occurs.

Example:

ASPX Code:

<asp:GridView ID="GrdProducts" runat="server AutoGenerateColumns="False"
            AllowSorting="true"
             OnSorted="GrdProducts_Sorted"
             OnSorting="GrdProducts_Sorting">          
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate> 
<asp:label ID="lblProductId" runat="Server" Text='<%# Eval("ProductID") %>' ></asp:label>  
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>      
<asp:label ID="txtProductName" runat="Server" Text='<%# Eval("ProductName") %>' ></asp:label>
</ItemTemplate>  
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>      
<asp:label ID="txtProductPrice" runat="Server" Text='<%# Eval("Price") %>' ></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C# Code:

private DataTable BindGridView()    
{
            var data = Dal.GetProductDetails();
            return data.Tables[0];   }


protected void GrdProducts_Sorted(object sender, EventArgs e)
        {
            // Display the sorting details with direction.
            lblMessage.Text = "Sorting by " +
             GrdProducts.SortExpression.ToString() +
              " in " + GrdProducts.SortDirection.ToString() +
              " order.";
        }

 

        protected void GrdProducts_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortingDirection = string.Empty;
            if (direction == SortDirection.Ascending)
            {
                direction = SortDirection.Descending;
                sortingDirection = "Desc";
             
            }
            else
            {
                direction = SortDirection.Ascending;
                sortingDirection = "Asc";
               
            }
            DataView sortedView = new DataView(BindGridView());
            sortedView.Sort = e.SortExpression + " " + sortingDirection;
            Session["SortedView"] = sortedView;
            GrdProducts.DataSource = sortedView;
            GrdProducts.DataBind();
        }


public SortDirection direction
        {
            get
            {
                if (ViewState["directionState"] == null)
                {
                    ViewState["directionState"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["directionState"];
            }
            set
            {
                ViewState["directionState"] = value;
            }
        }