In all my grid's I have to set many properties, I would like to know if there is any way to set properties only once in a single place.
Grid example:
In all my grid's I have to set many properties, I would like to know if there is any way to set properties only once in a single place.
Grid example:
1 - Creating a class library
to inherit from class GridView
and put your settings by default
Create a class library project and place this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Default.WebControl
{
[ControlValuePropertyAttribute("SelectedValue")]
[ToolboxData("GridViewDefault")]
public class GridViewDefault : GridView
{
public GridViewDefault()
{
this.GridLines = GridLines.Both;
this.CellPadding = 5;
this.CellSpacing = 6;
this.Width = new Unit("100%");
}
}
}
After this creation of the library class, refer to your web project through the Add Reference
Indicatingaiyourclasslibrary
.Nowconfigurethepolicyinyourprojectasfollows:
<%@RegisterNamespace="Default.WebControl" Assembly="Default.WebControl" TagPrefix="asp" %>
And then you can usually use the new GridView created realize:
CompletecodeAspx:
<%@PageTitle="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="WebApplicationForms.About" Debug="true" %>
<%@ Register Namespace="Default.WebControl" Assembly="Default.WebControl" TagPrefix="asp" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2>
<h3>Your application description page.</h3>
<p>Use this area to provide additional information.</p>
<asp:GridViewDefault ID="GridViewDefault1" runat="server"></asp:GridViewDefault>
<asp:Button ID="BtnAtualizar" OnClick="BtnAtualizar_Click" runat="server" />
</asp:Content>
With extensive method you can even put together several configurations and make a call in Page Load.
2 - Create a class with modifier static
with a method also with modifier static
like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.UI.WebControls
{
public static class Methods
{
public static void RenderConfiguration(this GridView grid)
{
grid.GridLines = GridLines.Both;
grid.CellPadding = 5;
grid.CellSpacing = 6;
grid.Width = new Unit("100%");
}
}
}
Note: Follow this code example only by changing the part of the internal configuration using your own settings.
No Load:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplicationForms
{
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.RenderConfiguration(); // chamando configuração padrão!
GridView1.DataSource = new object[]{
new {Id = 1, Nome = "GridView 1"},
new {Id = 2, Nome = "GridView 2"},
new {Id = 3, Nome = "GridView 3"},
new {Id = 4, Nome = "GridView 4"},
new {Id = 5, Nome = "GridView 5"}
};
GridView1.DataBind();
}
}
}
}
3 - You can also use a static
method by reference, like this:
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.UI.WebControls
{
public static class Methods
{
public static void RenderConfiguration(ref GridView grid)
{
grid.GridLines = GridLines.Both;
grid.CellPadding = 5;
grid.CellSpacing = 6;
grid.Width = new Unit("100%");
}
}
}
No Load:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplicationForms
{
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Methods.RenderConfiguration(ref GridView1); // chamando por referencia
GridView1.DataSource = new object[]{
new {Id = 1, Nome = "GridView 1"},
new {Id = 2, Nome = "GridView 2"},
new {Id = 3, Nome = "GridView 3"},
new {Id = 4, Nome = "GridView 4"},
new {Id = 5, Nome = "GridView 5"}
};
GridView1.DataBind();
}
}
}
}