Tutorials
ASP.NET
ASP.NET Using the DataList and Repeater, Datagrid Controls
ASP.NET Using the DataList and Repeater, Datagrid Controls - Page 2In this tutorial of Week 6 we will be learning about Overview of List-Bound Controls, Creating a Repeater Control, Creating a DataList Control, Introduction to the DataGrid, Setting Up the DataGrid , Using Advanced DataGrid Features and Adding Advanced Features.
Overview of List-Bound Controls
Creating a Repeater Control(Sample1.aspx)
Let's start with the Repeator control. Also keep in mind that all these controls are also known as the templated controls since their design is based on templates. Repeator control is used when we like to do our own formatting which means it is used in the cases where we like to make our own databound design control which may contain controls that we like.
In order to use the Repeator control you can just drag and drop the control on the webform. When you drop it will look something like this:
Now as you see that the repeator control is also telling you that if you don't like what you see now you can always change it by using the html view of the page
Let's see how we can do that so I went in the html view and changed the Repeator html code to this.
ItemTemplate tells the repeator control that what each item will be like in the repeator control. In the ItemTemplate we declared a button. Now switch back to the design view and you will see this:
So you see it just created several buttons for you since they were placed in the item template. There are also other types of templates like AlternatingItem and HeaderTemplate and FooterTemplate. Let's see an example where we use all of them:
Now let's see when I switch back to the design view what will I see:
Pretty cool right :) I know
Lets see some other types of databound controls.
{mosgogole}
Creating a DataList Control
DataList is basically a control which you would like to use it when you are making a shopping cart or maybe when you are developing a photo album. Let me show you an image and than you will have better idea of what I am talking about.
Let's make a simple DataList control (Sample2.aspx)
Just drag and drop a datalist control on the form. Right click the control and select Edit Template link at the bottom and select Item templates. Now you will see a visual designer where you can add anything in the item template section. You can even drag any control on the item template field. Let's drag a textbox and see what happens. When you drag a textbox and switch back to the design view you will see 5 textboxes in a flowlayout. You can also set them in horizontal layout by using the RepeatDirection property.
Since, this control is similar to the repeator control I will not talk about this more and switch to DataGrid control which is perhaps the most widely used Data Bound control
Creating and using a datagrid control:
Setting up the Datagrid:
Lets first set up our datagrid.
Okay your datagrid is set up, lets add some columns.
Adding the Bound Columns:
Adding the bound colums in the datagrid is pretty simple.
Note: Please also note that the button type should be link button or else it wont work.
Storing the database connection
In this demo I am storing the database connection in the Web.config file. The database name is DBSnippets, which has one table known as tblPerson. Here is the web.config file.
Okay till now we have made the Datagrid and also saved the connection string in the web.config file. Now the time has come to code and handle the events.
Lets first make the BindData method which will retrieve the contents from the database and bind it on the screen. This will be one of the most important methods since it will be called whenever the page is loaded for the first time.
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
As you see the BindData method is called when the page is not posted back. Now lets see the BindData method in details.
public void BindData()
{
SqlCommand myCommand = new SqlCommand("SP_SELECT_PERSONS",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds,"tblPerson");
myConnection.Open();
myCommand.ExecuteNonQuery();
myDataGrid.DataSource = ds;
myDataGrid.DataBind();
myConnection.Close();
}
Explanation of the BindData method:
Now Lets see the stored procedure.
Stored Procedure:
CREATE PROCEDURE SP_SELECT_PERSONS
AS
SELECT * FROM tblPerson GO
As you can see that the above Stored Procedure is pretty simple. All we are doing is we are just selected all the columns from the table person.
Lets now make the Edit method which will display textboxes inside the datagrid so that a user can insert data. This sort of editing is also known as Inline editing.
Making datagrid editable is pretty simple. All you to do is to code few lines in the EditCommand event of the datagrid. You can view all the events supported by DataGrid by selecting properties and than selecting the Thunder/Flash yellow sign at the top of the properties window.
Lets call our Edit DataGrid event Edit_DataGrid.
private void Edit_DataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// We use CommandEventArgs e to get the row which is being clicked
// This also changes the DataGrid labels into Textboxes so user can edit them
myDataGrid.EditItemIndex = e.Item.ItemIndex;
// Always bind the data so the datagrid can be displayed.
BindData();
}
Next Page: ASP.NET Using the DataList and Repeater, Datagrid Controls - Page 2
| ::) ::) :) that helped |

| what is the code to apply paging to GridView in c# .net 2005.I tried with GridView1.AllowPaging=true, but o use |