This series of directories
I think you have liked the layout of listview. Defining the display mode in layouttemplate and defining data binding in Other templates makes it easier to design the data presentation page.
However, I recently thought about a problem. What should I do if the columns to be displayed in listview are not fixed or we want to rearrange the columns in it? This article describes how to dynamically create a template for listview.
The first simple method is this. listview1.layouttemplate = This. loadtemplate ("mytemplate. ascx "); this is the simplest method, but it is not flexible enough. It can only provide predictable centralized templates for our listview. It cannot be changed during user operations. So what should we do?
To create a dynamic template, you must first create a template class and then instantiate the class as needed.
Create a new class that implements the itemplate interface and implement the instantiatein method of the interface.
This method inserts a text instance or control instance into a container.
Take layouttemplate as an example: public class mylayouttemplate: system. Web. UI. itemplate
{
Public void instantiatein (system. Web. UI. Control container)
{
Placeholder pH = new placeholder ();
Table T = new table ();
Tablerow r = new tablerow ();
R. cells. Add (New tablecell () {text = "title1 "});
R. cells. Add (New tablecell () {text = "title2 "});
R. cells. Add (New tablecell () {text = "title3 "});
T. Rows. Add (R );
Tablerow itemplaceholderrow = new tablerow ();
Table itemplaceholdertable = new table ();
Itemplaceholdertable. ID = "itemplaceholder ";
Itemplaceholderrow. cells. Add (New tablecell ());
Itemplaceholderrow. cells [0]. Controls. Add (itemplaceholdertable );
T. Rows. Add (itemplaceholderrow );
Ph. Controls. Add (t );
Container. Controls. Add (ph );
}
}
Then we only need to create an instance of this class and assign it to the layouttemplate attribute.
Mylayouttemplate = new mylayouttemplate ();
This. listview1.layouttemplate = mylayouttemplate;
Next, we will create an itemtemplate. Here we have to handle more databinding events.
The result code is as follows: public class myitemtemplate: system. Web. UI. itemplate
{
Public void instantiatein (system. Web. UI. Control container)
{
Placeholder pH = new placeholder ();
Tablerow ROW = new tablerow ();
Row. cells. Add (New tablecell () {id = "cell1 "});
Row. cells. Add (New tablecell () {id = "cell2 "});
Ph. Controls. Add (ROW );
Ph. databinding + = new eventhandler (ph_databinding );
Container. Controls. Add (ph );
}
Void ph_databinding (Object sender, eventargs E)
{
Placeholder pH = (placeholder) sender;
Idataitemcontainer rI = (idataitemcontainer) Ph. namingcontainer;
Object itemvalue1 = databinder. eval (Ri. dataitem, "Field 1 ");
(Tablecell) Ph. findcontrol ("cell1"). Text = itemvalue1.tostring ();
Object itemvalue2 = databinder. eval (Ri. dataitem, "Field 2 ");
(Tablecell) Ph. findcontrol ("cell2"). Text = itemvalue2.tostring ();
}
}
Similarly, use this. listview1.itemtemplate = new myitemtemplate (); to edit the itemtemplate.
If you are willing to spend more time, you can definitely handle other types of templates.
(For more information, see the source)