/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu




Friday, September 23, 2011

Using data list in asp.net

How to use datalist in asp.net ?

Dynamically adding row in datalist using asp.net.

Here i am just showing you the code for that. Here we go for some brief description about it.
We have a datalist and a input control as inside the . Now we want add some more row at runtime with a footer option add more on it. So what to do now and how to do ?
Here is the code for that.

*********************** H e r e i s t h e d a t a l i s t ************************************

********************************************************************************
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmdatalist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["tblsession"] == null)
{
SetInitialRow();
}
}
}

///
/// First Set Data List Initial Row on page loading
///


private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("University", typeof(string)));
dr = dt.NewRow();
dr["University"] = string.Empty;
dt.Rows.Add(dr);

//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
DatalistUniversity_name.DataSource = dt;
DatalistUniversity_name.DataBind();
}


private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{

//extract the TextBox values
TextBox box1 = (TextBox)DatalistUniversity_name.Items[0].FindControl("txtUniversityName");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["University"] = box1.Text;
rowIndex++;
}

//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Data List with the current data
DatalistUniversity_name.DataSource = dtCurrentTable;
DatalistUniversity_name.DataBind();
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks.
SetPreviousData();
}
///
/// Set Previous Data in Data List.
///

private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
//Creating a new table to store the data of current table.
DataTable dtnew = (DataTable)ViewState["CurrentTable"];
if (dtnew.Rows.Count > 0)
{
for (int i = 1; i < dtnew.Rows.Count; i++)
{
TextBox box1 = (TextBox)DatalistUniversity_name.Items[i].FindControl("txtUniversityName");
box1.Text = dtnew.Rows[i]["University"].ToString();
rowIndex++;
}
}
}
}

protected void btnAddUniversityName_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
int rowindex = 0;
Int32 total = 0;
for (int i = 1; i < DatalistUniversity_name.Items.Count; i++)
{
TextBox box1 = (TextBox)DatalistUniversity_name.Items[i].FindControl("txtUniversityName");
total = Convert.ToInt32(box1.Text) + Convert.ToInt32(total);
rowindex++;
}
txtsum.Text = Convert.ToString(total);
}
}

You Just Downlaod the code and review it you can do this my buddy you are the champ. you can do anything.
File1
File2

No comments:

Post a Comment

Recent Posts

My Blog List