Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class ItemAddrepeter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
//public class SessionDataDisplay1
//{
// private string _quantity;
// private string _price;
// public SessionDataDisplay1(string quantity, string price)
// {
// this._quantity = quantity;
// this._price = price;
// }
// public string Quantity
// {
// get {return _quantity;}
// }
// public string Price
// {
// get { return _price; }
// }
//}

public class SessionDataDisplay
{
private string _name;
private string _value;

//public string Quantity { get; set; }
//public string Price { get; set; }
//public string total { get; set; }
public SessionDataDisplay(string name, string value)
{
this._name = name;
this._value = value;
//this.Quantity = quantity;
}
public string Name
{
get { return _name; }
}
public string Value
{
get { return _value; }
}
//public string Quantity1
//{
// get { return Quantity; }
//}
}
protected void RefreshRepeater()
{
// Use the GetEnumerator method to
// iterate through the session-state.
ArrayList values = new ArrayList();
System.Collections.IEnumerator ie = Session.GetEnumerator();
string currentSessionItemName;
while (ie.MoveNext())
{
currentSessionItemName = (string)ie.Current;
values.Add(new SessionDataDisplay(currentSessionItemName,
Session[currentSessionItemName].ToString()));

}
// Bind values ArrayList to Repeater control.
Repeater1.DataSource = values;
Repeater1.DataBind();

}


//protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
//{

// // Determine which item to remove and
// // use the Remove method to remove it.
// RepeaterItem itemToRemove = e.Item;
// string sessionItemToRemove =
// ((Label)itemToRemove.FindControl("lblSno")).Text;
// Session.Remove(sessionItemToRemove);
// // Refresh the Repeater control.
// RefreshRepeater();
//}



protected void btnAdd_Click(object sender, EventArgs e)
{
filldatatable();
// If both name and value are specified
// use the Add method to add the item to session-state.
//if (!String.IsNullOrEmpty(txtSno.Text) &
// !String.IsNullOrEmpty(txtItems.Text))
//{
// string itemName = Server.HtmlEncode(txtSno.Text);
// string itemValue = Server.HtmlEncode(txtItems.Text);
// Session.Add(itemName, itemValue);
// //Session["itemName"] = txtSno.Text;
// // Refresh the Repeater control.
// RefreshRepeater();
//}
}
public void filldatatable()
{
DataTable dt = new DataTable();
if (!dt.Columns.Contains("ItemSno"))
{
dt.Columns.Add("ItemSno");
}
if (!dt.Columns.Contains("ItemName"))
{
dt.Columns.Add("ItemName");
}
if (!dt.Columns.Contains("ItemQuantity"))
{
dt.Columns.Add("ItemQuantity");
}

if (!dt.Columns.Contains("ItemPrice"))
{
dt.Columns.Add("ItemPrice");
}
if (!dt.Columns.Contains("ItemTotalPrice"))
{
dt.Columns.Add("ItemTotalPrice");
}
if (Session["datatable"] != null)
{
dt = (DataTable)Session["datatable"];
}
DataRow dr = dt.NewRow();

dr[0] = txtSno.Text;

dr[1] = txtItems.Text;

dr[2] = txtQuantity.Text;

dr[3] = txtPrice.Text;
float price;
float quantity;
float totalprice;
price = float.Parse(txtPrice.Text);
quantity = float.Parse(txtQuantity.Text);
totalprice = price * quantity;
txtTotalPrice.Text = totalprice.ToString();
dr[4] = txtTotalPrice.Text;
//////////////////////////////////////


dt.Rows.Add(dr);

if (Session["datatable"] == null)
{
Session["datatable"] = dt;
}

Repeater1.DataSource = dt;
Repeater1.DataBind();
///////////////////////////////////////////
//float firstno;
//float secondno;
//float thirdno;
//firstno = float.Parse(txtfirstno.Text);
//secondno = float.Parse(txtsecondno.Text);
//thirdno = firstno + secondno;
//MessageBox.Show(thirdno.ToString());

}

int Total = 0;
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//gives the sum in string Total.

Total += Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ItemTotalPrice"));

}
else if (e.Item.ItemType == ListItemType.Footer)
{

// The following label displays the total
lblGrandTotal.Text = Total.ToString();
}

}
protected void SaveRecords_Click(object sender, EventArgs e)
{
for (int index = 0; index < Repeater1.Items.Count; index++)
{

Label lblSno = Repeater1.Items[index].FindControl("lblSno") as Label;
Label lblItemName = Repeater1.Items[index].FindControl("lblItemName") as Label;
Label lblItemQuantity = Repeater1.Items[index].FindControl("lblItemQuantity") as Label;
Label lblItemPrice = Repeater1.Items[index].FindControl("lblItemPrice") as Label;
Label lblItemTotalPrice = Repeater1.Items[index].FindControl("lblItemTotalPrice") as Label;
//HiddenField ItemName = Repeater1.Items[index].FindControl("ItemName") as HiddenField;


String strConnString = ("Data Source=.;Initial Catalog=PRODUCT;Integrated Security=True");
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "insert into tbl_Product (Sno,ItemName,ItemQuantity,ItemPrice,Total)values('" + lblSno.Text + "','" + lblItemName.Text + "','" + lblItemQuantity.Text + "','" + lblItemPrice.Text + "','" + lblItemTotalPrice.Text + "')";
SqlCommand cmd = new SqlCommand(strQuery, con);
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Data Successfully Saved');</script>");

}
}
protected void btnremove_Click(object sender, EventArgs e)
{
}
}
Posted

1 solution

You can hide a column in a GridView but not in a repeater. In repeater, there are no columns as such. You use controls in your ItemTemplate. You can hide these controls. See the details at:

http://stackoverflow.com/questions/11296197/hide-a-column-in-repeater-control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900