• Embed Doc
  • Readcast
  • Collections
  • 6
    CommentGo Back
Download
 
IntroductionIn many situations we want to bind a fully, in-place operational GridView with adynamically changing data source. This article shows how GridView can be templated programmatically at run time and how is it possible to do edit, insert and deleteoperations on it. Although just binding the GridView with some dynamically changingdatatsource is a simple and straight job which does not need templated GridView, itcannot facilitate us with the aforementioned operations. To make the GridViewsimultaneously an operational one, we need to template it dynamically which alsorequires dynamically generated queries for corresponding operations according to theGridView's data source.It is useful in many applications especially those that require data manipulationfunctionality of Enterprise Manager on client side i.e. displaying any table of anydatabase with options for all possible operations on client side. This solution provided inthis article will be the best and probably the only way to make it all possible. This articleshows how ITemplate is implemented effectively to achieve all of this.Figure 1How it works
 
First, we should know what a template is. Generally, a template is description of how a particular item will be rendered at run time. It determines the layout and binding of ASP.NET server control(s) contained by it. We may put this description at design time or at run time according to our need. At design time we can define templates declarativelyusing inline tags in aspx source of GridView (the following listing shows this). Since weare going for a case where we don’t know the number of fields of a table and their description (Name, Data Type etc) in advance therefore we need to create the templatesdynamically according to fields of the particular table. This is where the concept of dynamically templated GridView comes. This is a GridView that can be bound to anytable of any database of any server, providing Insert, Edit and Delete optionsimultaneously. As long as we know the name of fields in advance we don’t need ,even,to know about ITemplate but to create templates dynamically we have to implementITemplate interface. We need to generate ItemTemplate and EditItemTemplatedynamically for each field of the table, plus a field for buttons required for eachcommand. The former lets to specify how an item will look like in normal mode (usuallydisplayed in labels) while the later lets to specify how an item will change when it is putinto Edit mode (displayed in text boxes).ITemplate has one method named InstantiateIn, its explanation will come later. The classDynamicallyTemplatedGridViewHandler which implements this interface, exposes anytemplate property of GridView as its own property, hence the default layout of a templateis overridden.The following listing shows how to create template fields at design time. It shows that wemust know number of fields and their name in advance to create their templates. This is NOT required in our case.Listing 1
<asp:GridView ID="TableGridView"  runat="server" AutoGenerateColumns="False" >  <Columns>  <asp:TemplateField HeaderText="Name">  <ItemTemplate>  <asp:Label id="lblName" Runat="Server"  Text='<%# Eval("pub_name") %>'/> </ItemTemplate>  <EditItemTemplate>  <asp:TextBox id="tbName" Runat="Server"  Text='<%# Bind("pub_name") %>'/> </EditItemTemplate>  </asp:TemplateField> ....</Columns</asp:GridView>
 I have implemented ITemplate in a separate class,DynamicallyTemplatedGridViewHandler, for reusability and better understanding. I willexplain working and usage of this class since they play a key role in achieving all of this.We can divide the whole working in two parts:
 
 
Writing a class which implements ITemplate
 
Using the class which implements ITemplateWriting a class which implements ITemplateHere is the skeleton of class DynamicallyTemplatedGridViewHandler implementingITemplate with a list of data members and methods.Listing 2
public classDynamicallyTemplatedGridViewHandler : ITemplate {ListItemType ItemType; stringFieldName; stringInfoType; publicDynamicallyTemplatedGridViewHandler(ListItemType Item_type, stringfield_name,stringcontrol_type);  public voidInstantiateIn(System.Web.UI.Control Container);  private voidOnDataBinding(objectsender, EventArgs e); }
This class has three data members:
ListItemType ItemType; stringFieldName; stringInfoType;
ItemType keeps the type of a list item type: Item, EditItem, Header, Footer,AlternatingItem, Pager, SelecetdItem or Separator. In this demo version we need onlythree of these; we need Header (For heading of each column), Item (for showing fieldswhen GridView will be in normal mode) and EditItem (for showing fields whenGridView will be in Edit mode).FieldName keeps the name of each template field that will be displayed in the header.InfoType keeps an indicator in string form for a type of information within a templatefield i.e. whether a template field has information of "Command" or "String" so that later data retrieval and data binding of that particular child control will be made accordingly. A"Command" (Edit, Delete, and Insert) requires instantiation in the Button control whilehe "String" requires a Label or TextBox. Now, coming to the member methods listed above, there is a constructer which simplysets the aforementioned data members with those passed as parameters.Listing 3
publicDynamicallyTemplatedGridViewHandler(ListItemType item_type,stringfield_name,  stringcontrol_type){ItemType=item_type;FieldName=field_name;InfoType=info_type;}
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...

Logged in and still no dl link, please help

hey..there is no downloadlink on title can u help me to download

Login to the scribd and u can get download link on the document title

how to download this 14 pages m,aterial there is no link here

You must be to leave a comment.
Submit
Characters: ...