I need to do a dropdownlist with optgroup.
I found lots of guides and all foresee the use of WebControlAdapter
this is the guide that I'm fllowing
I've added the class to my App_Code folder project:
namespace admin.App_Code
{
public class DropDownListAdapter :
System.Web.UI.WebControls.Adapters.WebControlAdapter
{
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
// The current control being "adaptered" is available within context from the Control property
DropDownList dropDownList = (DropDownList)Control;
ListItemCollection items = dropDownList.Items;
// Retrieve Optgrouping using LinQ
var groups = (from p in items.OfType<ListItem>()
group p by p.Attributes["Group"] into g
select new { Label = g.Key, Items = g.ToList<ListItem>
() });
foreach (var group in groups)
{
if (!String.IsNullOrEmpty(group.Label))
{
writer.WriteBeginTag("optgroup");
writer.WriteAttribute("label", group.Label);
writer.Write(">");
}
int count = group.Items.Count();
if (count > 0)
{
bool flag = false;
for (int i = 0; i < count; i++)
{
ListItem item = group.Items[i];
writer.WriteBeginTag("option");
if (item.Selected)
{
if (flag)
{
throw new HttpException("Multiple selected items not allowed");
}
flag = true;
writer.WriteAttribute("selected", "selected");
}
if (!item.Enabled)
{
writer.WriteAttribute("disabled", "true");
}
writer.WriteAttribute("value", item.Value, true);
if (this.Page != null)
{
this.Page.ClientScript.RegisterForEventValidation(dropDownList.UniqueID, item.Value);
}
writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
if (!String.IsNullOrEmpty(group.Label))
{
writer.WriteEndTag("optgroup");
}
}
}
private Object _ViewState;
protected override void OnLoad(EventArgs e)
{
if (Page.IsPostBack)
{
if (_ViewState != null)
{
Object[] groups = (Object[])_ViewState;
DropDownList dropDownList = (DropDownList)Control;
// Add saved optgroups to ListItems
for (Int32 i = 0; i < groups.Length; i++)
{
if (groups[i] != null)
{
dropDownList.Items[i].Attributes["Group"] = groups[i].ToString();
}
}
}
}
base.OnLoad(e);
}
protected override void LoadAdapterViewState(object state)
{
// Retrieve existing state
_ViewState = state;
}
protected override object SaveAdapterViewState()
{
DropDownList dropDownList = (DropDownList)Control;
Int32 count = dropDownList.Items.Count;
Object[] values = new Object[count];
// Retrieve Optgrouping from ListItem
for (int i = 0; i < count; i++)
{
values[i] = dropDownList.Items[i].Attributes["Group"];
}
return values;
}
}
}
public static void loadDDLModelli(ref DropDownList ddl, List<dynamic>
objects)
{
Int16 cont = 0;
ddl.Items.Clear();
System.Web.UI.WebControls.ListItem li;
String idModello = "";
String nomeModello = "";
String nomeBrand = "";
String oggetto = "";
List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>();
foreach (var item in objects)
{
oggetto = item.ToString().Replace("{", "").Replace("}", "");
idModello = oggetto.Split(',')[0].Split('=')[1].Trim();
nomeModello = oggetto.Split(',')[1].Split('=')[1].Trim();
nomeBrand = oggetto.Split(',')[2].Split('=')[1].Trim();
li = new System.Web.UI.WebControls.ListItem(nomeBrand+" - "+nomeModello, idModello);
li.Attributes["Group"] = nomeBrand;
items.Add(li);
cont++;
};
ddl.DataSource = items;
ddl.DataBind();
ddl.SelectedIndex = -1;
}
I've added the folder App_Browser to my project (did not exist) and I've added the file BrowserFile.browser
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.DropDownList"
adapterType="admin.App_Code.DropDownListAdapter" />
</controlAdapters>
</browser>
</browsers>
in my page .aspx (that is in the same folder of the class DropDownListAdapter I have
<asp:DropDownList runat="server" ID="ddlModelli" CssClass="form-control multipleSelect"></asp:DropDownList>
that is filled in this way
public static void loadDDLModelli(ref DropDownList ddl, List<dynamic> objects)
{
Int16 cont = 0;
ddl.Items.Clear();
System.Web.UI.WebControls.ListItem li;
String idModello = "";
String nomeModello = "";
String nomeBrand = "";
String oggetto = "";
List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>();
foreach (var item in objects)
{
oggetto = item.ToString().Replace("{", "").Replace("}", "");
idModello = oggetto.Split(',')[0].Split('=')[1].Trim();
nomeModello = oggetto.Split(',')[1].Split('=')[1].Trim();
nomeBrand = oggetto.Split(',')[2].Split('=')[1].Trim();
li = new System.Web.UI.WebControls.ListItem(nomeBrand+" - "+nomeModello, idModello);
li.Attributes["Group"] = nomeBrand;
items.Add(li);
cont++;
};
ddl.DataSource = items;
ddl.DataBind();
ddl.SelectedIndex = -1;
}
the problem is that, when I watch the source code I do not have the optgroup tag but only options tag.
In fact, if I put a breakpoint in the first line of the method RenderContents this doesn't fire.
What I'm doing wrong?
I solved the issue.
The problem was in the method LoadDDLModelli.
Instead of setting DataSource and do the DataBind to the Dropdownlist passed via reference, I have to add ItemList singoularly (I cannot understand the difference)
public static void loadDDLModelli(ref DropDownList ddl, List<dynamic> objects)
{
Int16 cont = 0;
ddl.Items.Clear();
System.Web.UI.WebControls.ListItem li;
String idModello = "";
String nomeModello = "";
String nomeBrand = "";
String oggetto = "";
List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>();
foreach (var item in objects)
{
oggetto = item.ToString().Replace("{", "").Replace("}", "");
idModello = oggetto.Split(',')[0].Split('=')[1].Trim();
nomeModello = oggetto.Split(',')[1].Split('=')[1].Trim();
nomeBrand = oggetto.Split(',')[2].Split('=')[1].Trim();
li = new System.Web.UI.WebControls.ListItem(nomeBrand+" - "+nomeModello, idModello);
li.Attributes["Group"] = nomeBrand;
items.Add(li);
cont++;
};
//ddl.DataSource = items;
//ddl.DataBind();
foreach(ListItem i in items)
ddl.Items.Add(i);
ddl.SelectedIndex = -1;
}
Related
I want these buttons created using a foreach loop from a list of items to access the information that is stored in these items.
I used a foreach loop to create buttons from a list of items that represent files in a directory and those items hold links to these files. My goal is to make pressing these buttons open a dialog with these files to write a line in them.
public List<mov.Movie> movies = new List<mov.Movie>() { };
private void writeReportToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (string file in Directory.EnumerateFiles(#"C:\Users\sloup\OneDrive\Desktop\MovieList", "*.txt"))
{
var lines = File.ReadAllLines(file);
string movieName = lines[0];
if (movies.Select(x => x.Name).Contains(movieName))
{
}
else
{
string movieLength = lines[1];
string movieYear = lines[2];
string movieReport = lines[3];
movies.Add(new mov.Movie(movieName, movieLength, movieYear, movieReport, #"C:\Users\sloup\OneDrive\Desktop\MovieList" + movieName + ".txt"));
}
}
int X = 1;
int Y = 1;
foreach (var movie in movies)
{
var movie1Button = new Button();
movie1Button.Text = movie.Name;
movie1Button.Font = new Font("Calibri", 12);
movie1Button.ForeColor = Color.Black;
movie1Button.Padding = new Padding(6);
movie1Button.AutoSize = true;
this.Controls.Add(movie1Button);
movie1Button.Location = new System.Drawing.Point(20, 50 * X);
X++;
movie1Button.Click += Movie1Button_Click;
var movie1Label = new Label();
movie1Label.Text = movie.Year;
movie1Label.Font = new Font("Calibri", 12);
movie1Label.ForeColor = Color.Black;
movie1Label.Padding = new Padding(6);
movie1Label.AutoSize = true;
this.Controls.Add(movie1Label);
movie1Label.Location = new System.Drawing.Point(200, 50 * Y);
Y++;
}
}
private void Movie1Button_Click(object? sender, EventArgs e)
{
string[] lines1 = File.ReadAllLines();
var lines = File.ReadAllLines();
string movieName = lines[0];
string movieLength = lines[1];
string movieYear = lines[2];
}
public class Movie
{
public string Name;
public string Length;
public string Year;
public string Report;
public string Link;
public Movie(string movieName, string movieLength, string movieYear, string movieReport, string movieLink)
{
Name = movieName;
Length = movieLength;
Year = movieYear;
Report = movieReport;
Link = movieLink;
}
}
You can use the Tag property to attach the movie object to each button in the loop:
movie1Button.Tag = movie;
afterwards in the click event grab the button from the sender and cast the Tag-object back to a Movie
private void Movie1Button_Click(object? sender, EventArgs e)
{
Button button = sender as Button
(if button != null)
{
Movie movie = button.Tag as Movie;
// do what ever you like afterwards
}
}
I want to make a combo box like this:
But the boxes should not be hardcoded they should come from a text file like this:
Addition of data in text file should result in addition of combo Boxes. Also each comboBox should have the same list of options in it which are 1,2,3,4
I made the following class to read and write the text file, but I couldn't find any resources in the internet to turn these text files to combo Box.
public static string ReadFromTextFile(string path)
{
if (File.Exists(path))
{
string data;
using (StreamReader r = new StreamReader(path))
{
data = r.ReadToEnd();
}
if (data != "")
{
data = "[" + data + "]";
}
return data;
}
return null;
}
public static void WriteToTextFile(string path, string data, bool append = true, int count = 1)
{
if (!File.Exists(path))
{
var file = File.Create(path);
file.Close();
}
using (StreamWriter writer = new StreamWriter(path, append: append))
{
if (!append)
{
//remove opening bracket "[" from data passed
data = data.Trim().Substring(1, data.Trim().Length - 1);
//remove last bracket "]" from data passed
data = data.Trim().Substring(0, data.Trim().Length - 1);
}
if (count != 0)
{
data = data + ",";
}
writer.WriteLine(data);
}
}
public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
if (data != null)
{
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
}
return table;
}
I don't have visual studio available now. So, I'll give you the way forward.
Read the line and split it into string array.
string[] arr= line.Split(",");
The first one (say Food) is the heading and the remaining are values.
Loop through the array.
for (int i=1;i<= arr.Length;i++)
{
}
Add it to the combobox items like cbo.Items.Add(arr[i]).
Loop through the lines in the file and you get the desired output.
You can use the string "Food"/"Water" as the Name property of the ComboBox to identify the each ComboBox.
Besides, note that should set a different Location for each ComboBox.
private void buttonCreateComboBox_Click(object sender, EventArgs e)
{
int locationX = 50;
int locationY = 10;
string line;
System.IO.StreamReader file =
new System.IO.StreamReader(#"C:\Users\Administrator\Desktop\test.txt");
while ((line = file.ReadLine()) != null)
{
// Remove the extra ','
string comboName = line.Substring(0, line.Length - 1);
ComboBox comboBox = new ComboBox();
comboBox.Name = comboName;
comboBox.Items.AddRange(new object[] { 1, 2, 3, 4 });
comboBox.Location = new Point(locationX, locationY);
this.Controls.Add(comboBox);
Label label = new Label();
label.Text = comboName;
label.Location = new Point(0, locationY);
this.Controls.Add(label);
locationY += 30;
}
file.Close();
}
If you want to access a specific ComboBox, you can call Control.ControlCollection.Find(String, Boolean) Method to get it.
private void buttonGetComboWaterText_Click(object sender, EventArgs e)
{
ComboBox comboWater = (ComboBox)this.Controls.Find("Water", true)[0];
MessageBox.Show(comboWater.Text);
}
Without going into details (don't know why you need DataTable etc.) I will answer your main question from title.
This is how my textfile looks, no need for comma if you read line by line:
public void ReadFromTextFile(string path)
{
if (File.Exists(path))
{
using (StreamReader r = new StreamReader(path))
{
String line;
while ((line = r.ReadLine()) != null)
{
CreateComboBox(line.ToString());
}
}
}
}
public void CreateComboBox(string definition)
{
var combo = new ComboBox();
combo.Name = definition;
combo.Items.AddRange(new object[] { "1", "2", "3", "4" });
var label = new Label();
label.Text = definition;
this.flowLayoutPanel1.Controls.Add(label);
this.flowLayoutPanel1.Controls.Add(combo);
}
private void Form1_Load(object sender, EventArgs e)
{
ReadFromTextFile(#"c:\temp\MyTest.txt");
}
Use File.ReadAllLines(...) to short the txt read.
Point to control the position.
Attaches a delegate to SelectedIndexChanged that I imagine you will need for the next step.
private void Form1_Load(object sender, EventArgs e)
{
var lines = File.ReadAllLines(#"src.txt").Select(str => str.Replace(",", "")).ToList();
Label lbl, lastLbl = null;
ComboBox combo, lastCombo = null;
for (int i = 0; i < lines.Count(); i++)
{
lbl = new Label();
lbl.Text = lines[i];
if (i > 0) // adjust position
lbl.Location = new Point(lastLbl.Location.X, lastLbl.Location.Y + lastLbl.Height);
this.Controls.Add(lbl);
lastLbl = lbl;
combo = new ComboBox();
combo.DataSource = new List<int>() { 1, 2, 3, 4 };
if (i > 0) // adjust position
combo.Location = new Point(lastCombo.Location.X, lastCombo.Location.Y + lastCombo.Height);
else
combo.Location = new Point(lbl.Width + 5, 0);
//combo.SelectedIndexChanged += (s, a) => { }; // action you may need
this.Controls.Add(combo);
lastCombo = combo;
}
}
i have a problem with visual studio c# listview items that i cant found a solution over googling.
I've made from application with a listview, i can add, remove, update listview items.
I'm saving and loading the listview to/from file correctly with this code:
private void saveListViewItems(string path, ListView lv)
{
var delimeteredListviewData = new List<string>();
string delimeteredItems = string.Empty;
foreach (ListViewItem lvi in lv.Items)
{
foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
{
delimeteredItems += lvsi.Text + "#";
}
delimeteredListviewData.Add(delimeteredItems);
}
System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
}
private void loadListViewItems(string path, ListView lv)
{
foreach (string line in System.IO.File.ReadAllLines(path))
{
lv.Items.Add(new ListViewItem(line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)));
}
}
the problems is i have activated checkbox next to each item. i cant save listview items with checkbox checked. i want to save listview and load with selected items. thanks
Something like this?:
List<string> delimeteredItems = new List<string>();
foreach (ListViewItem lvi in listView1.CheckedItems)
{
string item= String.Join("#", lvi.SubItems.Cast<ListViewItem.ListViewSubItem>().Select(si=>si.Text));
delimeteredItems.Add(item);
}
System.IO.File.WriteAllLines(#"c:\temp\lines.txt", delimeteredItems);
Instead of only saving the string you need to save more information.
You can added add a IsChecked value together with your string.
You need a small wrapper class that holds both values as a property.
Then you can use serilization, it is a term to describe a way to convert an object to something you can store on disk.
There are many formats to pick from, but Json is a good readable format. You can download the Nuget package NewtonSoft.Json via the nuget package manager.
public class StoreListView
{
public void StoreToDisk(string path, List<ItemDataHolder> list)
{
string theListInJsonFormat = JsonConvert.SerializeObject(list);
File.WriteAllText(path, theListInJsonFormat);
}
}
public class ItemDataHolder
{
public string SomeValuesYouWantToSave { get; set; }
public bool IsChecked { get; set; }
}
I've Managed the program to work.
I was wrong about the way reading/writing the text file.
now I'm using ini-parser nuget.
installed ini parser to project.
then:
using IniParser;
using IniParser.Model;
private void saveListViewItems(string path, ListView lv)
{
int i = 0;
string IPFrom;
string IPFromval;
string IPTO;
string IPTOval;
string Comment;
string Commentval;
string CheckState;
string CheckStateval;
int IPSectioncount;
var parser = new FileIniDataParser();
IniData data = parser.ReadFile(path);
while ( i < lv.Items.Count) {
if ((lv.Items[i].Selected) || (lv.Items[i].Checked))
{
CheckStateval = "1";
}
else
{
CheckStateval = "0";
}
CheckState = "";
CheckState = "CheckState";
CheckState += String.Join(CheckState, i);
IPFromval = lv.Items[i].SubItems[0].Text;
IPFromval = "";
IPFrom = "IPFrom";
IPFrom += String.Join(IPFrom, i);
IPFromval = lv.Items[i].SubItems[0].Text;
IPTOval = "";
IPTO = "IPTO";
IPTO += String.Join(IPTO, i);
IPTOval = lv.Items[i].SubItems[1].Text;
Commentval = "";
Comment = "Comment";
Comment += String.Join(Comment, i);
Commentval = lv.Items[i].SubItems[2].Text;
data["IP"][CheckState] = CheckStateval;
data["IP"][IPFrom] = IPFromval;
data["IP"][IPTO] = IPTOval;
data["IP"][Comment] = Commentval;
i++;
}
IPSectioncount = lv.Items.Count;
data["IP"]["IPSectionCount"] = IPSectioncount.ToString();
parser.WriteFile(path, data);
}
private void loadListViewItems(string path, ListView lv)
{
string IPFrom;
string IPTO;
string Comment;
string CheckState;
string IPSectioncount;
string row="";
var parser = new FileIniDataParser();
IniData data = parser.ReadFile(path);
IPSectioncount = data["IP"]["IPSectionCount"];
int m = Int32.Parse(IPSectioncount);
int i = 0;
while ( i < m )
{
IPFrom = "";
IPTO = "";
Comment = "";
row = "";
IPFrom = "IPFrom";
IPFrom += String.Join(IPFrom, i);
IPFrom = data["IP"][IPFrom];
IPTO = "IPTO";
IPTO += String.Join(IPTO, i);
IPTO = data["IP"][IPTO];
Comment = "Comment";
Comment += String.Join(Comment, i);
Comment = data["IP"][Comment];
CheckState = "CheckState";
CheckState += String.Join(CheckState, i);
CheckState = data["IP"][CheckState];
row = String.Join(",",IPFrom,IPTO,Comment);
lv.Items.Add(new ListViewItem(row.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)));
if (CheckState == "1")
{
lv.Items[i].Checked = true;
}
i++;
}
}
I've a requirement in which I've to display the saved users to the SharePoint people editor control. For this, I am saving the user names to People/Group column. And I am using the following code for taking these users to people editor control:
SetPeopleEditor(item, Constants.FieldNames.IT_DIRECTOR, pe_ITDirector, oWeb);
the definition of the above method is shown below:
private PickerEntity SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
{
ArrayList entityArrayList = new ArrayList();
PickerEntity entity = null;
if (item[columnName] != null)
{
char[] to_splitter = { ';' };
string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
entity = new PickerEntity();
entity.Key = user;
entity.IsResolved = true;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
}
}
return entity;
}
But unfortunately, the control always showing empty value. How can I achieve this by populating data to the people editor control?
You can do as follows,
SPFieldUserValueCollection userValueCollection =
new SPFieldUserValueCollection(SPContext.Current.Web, SPContext.Current.Item["ColumnName"] as string);
if (userValueCollection .Count > 0)
{
spPeoplePickerContol.CommaSeparatedAccounts = userValueCollection[0].User.LoginName;
}
You have to include peopleEditor.Entities.Add(entity) after validating entity to add the entity to peopleEditor control.
private void SetPeopleEditor(SPListItem item, string columnName, PeopleEditor peopleEditor, SPWeb web)
{
if (item[columnName] != null)
{
char[] to_splitter = { ';' };
string to_list = item[columnName].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
PickerEntity entity = new PickerEntity();
entity.Key = user;
entity.IsResolved = true;
entity = peopleEditor.ValidateEntity(entity);
peopleEditor.Entities.Add(entity);
}
}
}
}
Try this:
string x = item["SP_Group"] == null ? "" : item["SP_Group"].ToString();
if (x != "")
{
SPFieldUserValue uv = new SPFieldUserValue(web, x);
SPGroup group = mySite.Groups.GetByID(uv.LookupId);
if (group.Users.Count > 0)
{
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPUser sUser in group.Users)
{
PickerEntity entity = new PickerEntity();
entity.Key = sUser.LoginName;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
});
peopleEditor.UpdateEntities(entityArrayList);
peopleEditor.Validate();
}
else
{
peopleEditor.Entities.Clear();
}
}
string x = item["SP_Users"] == null ? "" : item["SP_Users"].ToString();
if (x != "")
{
SPFieldUserValueCollection uvcoll = new SPFieldUserValueCollection(mySite, x);
if (uvcoll.Count > 0)
{
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPFieldUserValue uv in uvcoll)
{
SPUser sUser = uv.User;
PickerEntity entity = new PickerEntity();
entity.Key = sUser.LoginName;
entity = peopleEditor.ValidateEntity(entity);
entityArrayList.Add(entity);
}
});
peopleEditor.UpdateEntities(entityArrayList);
peopleEditor.Validate();
}
else
{
peopleEditor.Entities.Clear();
}
}
I am trying to get the items stored in a sessions variable into a repeater for users to see. However, I am not entirely sure how to do it (I'm new to session variables). Basically, when users enter in quantities for items on once page the hit submit, they are taken to an "order summary" page, which will display what they plan to purchase. I have successfully set up a session variable to contain the sku and quantity of each product the user selects, but I do not know how to get the information out.
I've stored the information in the session variable as [sku],[quantity];[sku],[quantity];[sku],[quantity] and so on. I figure I must do a split or something based on the commas and semicolons but I am not sure how to do so with a session variable.
The code for the product listing page that contains the information to be stored in the session variable:
public partial class GojoptproductlistSublayout : System.Web.UI.UserControl
{
private void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(#"child::*[##templatename='gojoMarketOfficeBuildigProductMap']/*[##templatename='gojoProductList']");
if (ProductGroup != null)
{
Item[] LocationList = ProductGroup.Axes.SelectItems(#"child::*[##templatename='gojoProductLocation' and #Active = '1']");
if (LocationList != null)
{
DataSet ds = new DataSet();
DataTable locations = ds.Tables.Add("locations");
locations.Columns.Add("LocationName", Type.GetType("System.String"));
locations.Columns.Add("LocationID", Type.GetType("System.String"));
foreach (Item LocationItem in LocationList)
{
DataRow dr = locations.NewRow();
dr["LocationName"] = LocationItem.Fields["Header"].Value;
dr["LocationID"] = LocationItem.ID.ToString();
locations.Rows.Add(dr);
}
locationRepeater.DataSource = ds;
locationRepeater.DataMember = "locations";
locationRepeater.DataBind();
}
}
}
}
}
protected void SetInner(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType != ListItemType.Footer) & (e.Item.ItemType != ListItemType.Header))
{
Label refID = (Label)e.Item.FindControl("refID");
Label test = (Label)e.Item.FindControl("test");
Repeater areaRepeater = (Repeater)e.Item.FindControl("areaRepeater");
Database db = Sitecore.Context.Database;
Item LocationAreaItem = db.Items[refID.Text];
if (LocationAreaItem != null)
{
Item[] AreaList = LocationAreaItem.Axes.SelectItems(#"child::*[##templatename='gojoProductLocationArea' and #Active = '1']");
if (AreaList != null)
{
DataSet dset = new DataSet();
DataTable areas = dset.Tables.Add("areas");
areas.Columns.Add("AreaName", Type.GetType("System.String"));
areas.Columns.Add("Sku", Type.GetType("System.String"));
areas.Columns.Add("ProductName", Type.GetType("System.String"));
areas.Columns.Add("masterSku", Type.GetType("System.String"));
areas.Columns.Add("masterName", Type.GetType("System.String"));
areas.Columns.Add("Size", Type.GetType("System.String"));
areas.Columns.Add("SkuID", Type.GetType("System.String"));
areas.Columns.Add("AreaID",Type.GetType("System.String"));
areas.Columns.Add("productID", Type.GetType("System.String"));
foreach (Item AreaItem in AreaList)
{
DataRow drow = areas.NewRow();
drow["AreaName"] = AreaItem.Fields["Header"].Value;
drow["AreaID"] = AreaItem.ID.ToString();
areas.Rows.Add(drow);
Item[] SkuList = AreaItem.Axes.SelectItems(#"child::*[(##templatename='gojoPTRefill' or ##templatename = 'gojoPTAccessories' or ##templatename = 'gojoPTDispenser' or ##templatename = 'gojoPTSelfDispensed') and #Active = '1']");
foreach (Item ChildItem in SkuList)
{
Item MarketProduct = db.Items[ChildItem.Fields["Reference SKU"].Value];
drow["productID"] = ChildItem.ID.ToString();
if (MarketProduct != null)
{
Item MasterProduct = db.Items[MarketProduct.Fields["Master Product"].Value];
if (MasterProduct != null)
{
DataRow newRow = areas.NewRow();
if(MasterProduct.TemplateName == "gojoSKUSelfDispensed" || MasterProduct.TemplateName == "gojoSKURefill")
{
newRow["Size"] = MasterProduct.Fields["Size"].Value;
}
else
{
newRow["Size"] = "-";
}
newRow["Sku"] = MasterProduct.Fields["SKU"].Value;
newRow["productID"] = MasterProduct.ID.ToString();
Item MasterProductName = db.Items[MasterProduct.Fields["Complete Product Name"].Value];
if (MasterProductName != null)
{
newRow["ProductName"] = MasterProductName.Fields["Complete Name"].Value;
}
areas.Rows.Add(newRow);
}
}
}
}
areaRepeater.DataSource = dset;
areaRepeater.DataMember = "areas";
areaRepeater.DataBind();
}
}
}
}
protected bool checkQtys(ref int ItemCnt, ref ArrayList LinesToOrder)
{
Repeater locationRepeater = (Repeater)FindControl("locationRepeater");
bool validQtys = true;
string productID = "";
int qty;
qtyErrorMsg.Text = "";
qtyErrorMsgTop.Text = "";
foreach (RepeaterItem repItem in locationRepeater.Items)
{
if (repItem != null)
{
Repeater areaRepeater = (Repeater)repItem.FindControl("areaRepeater");
if (areaRepeater != null)
{
foreach (RepeaterItem skuItm in areaRepeater.Items)
{
if (skuItm != null)
{
Label SkuID = (Label)skuItm.FindControl("SkuID");
Label qtyID = (Label)skuItm.FindControl("qtyID");
PlaceHolder inner = (PlaceHolder)skuItm.FindControl("ProductTable");
if (inner != null)
{
foreach (Control ct in inner.Controls)
{
if (ct is TextBox)
{
TextBox lineQty = (TextBox)ct;
Label prodID = (Label)inner.FindControl("productID");
if (lineQty.Text != "")
{
try
{
int.Parse(lineQty.Text);
productID = prodID.Text;
qty = int.Parse(lineQty.Text);
if (qty > 0)
{
noItemMsg.Visible = false;
noItemMsgTop.Visible = false;
ItemCnt++; //only count items with valid qty values
LinesToOrder.Add(new LineItem(productID, qty));
}
else
{//Qty is 0 or less error
validQtys = false;
qtyErrorMsg.Text = "Quantity must be a number<br />";
qtyErrorMsgTop.Text = "Quantity must be a number<br />";
}
}
catch
{//NaN - display error msg
validQtys = false;
qtyErrorMsg.Text = "Quantity must be a number<br />";
qtyErrorMsgTop.Text = "Quantity must be a number<br />";
}
}
}
}
}
}
}
}
}
}
return validQtys;
}
class LineItem
{//This class will store the product information
public string SKUID;
public int Qty;
public LineItem(string InSKUID, int InQty)
{
this.SKUID = InSKUID;
this.Qty = InQty;
}
}
protected void orderSubmit(object sender, EventArgs e)
{
int ItemCnt = 0;
bool validQtys = true;
ArrayList LinesToOrder = new ArrayList();
Label lb = FindControl("order") as Label;
if (checkQtys(ref ItemCnt,ref LinesToOrder))
{
if (ItemCnt == 0)
{//make sure at least one item with proper qty amount is entered before submitting the order
validQtys = false;
noItemMsg.Visible = true;
noItemMsg.Text = "You must order at least one item<br />";
noItemMsgTop.Visible = true;
noItemMsgTop.Text = "You must order at least one item<br />";
}
if (validQtys)
{//save the information to a session variable and send users to order review page
try
{
foreach (LineItem WorkLine in LinesToOrder)
{
lb.Text += WorkLine.SKUID + ", " + WorkLine.Qty + ";";
}
Session["orderComplete"] = lb.Text;
}
catch (Exception x)
{
Response.Write(x.Message.ToString());
}
Response.Redirect("/united-states/market/office-buildings/obproductmap/OrderReview");
}
}
}
}
Here is the designer code with the repeater that is meant to hold the order summary:
<asp:Repeater ID="orderRepeater" runat="server" >
<headertemplate>
<tr>
<th>SKU</th>
<th>Quantity</th>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%#Eval("sku") %></td>
<td><%#Eval("qty") %></td>
</tr>
</itemtemplate>
</asp:Repeater>
And here is the code behind:
private void Page_Load(object sender, EventArgs e)
{
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");
if (Session["orderComplete"] != "")
{
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(#"child::*[##templatename='gojoMarketOfficeBuildigProductMap']/*[##templatename='gojoOrderReview']");
if (ProductGroup != null)
{
//this is where I am confused on how to proceed
}
}
}
}
Everything is working and I did a Response.Write test on the session variable to make sure it had the correct information and it did.
Thanks in advance!
Before I try to address your question, lets take a look at some basics. A Session can hold an object, not just a string object. I would change:
if (Session["orderComplete"] != "")
to
if (Session["orderComplete"] != null && Session["orderComplete"] != "")
if you don't, and Session["orderComplete"] is null, Session["orderComplete"] != "" will throw an error object not set to an instance of an object
And now to your question. Setting your session variable to [sku],[quantity];[sku],[quantity];[sku],[quantity], is not a good idea. For one, its not object oriented and 2, its not going to bind to any repeater or data source. You should create an object and bind a list of those objects to your control:
pseudo code:
class Program
{
static void Main(string[] args)
{
List<Order> orders = new List<Order>();
orders.Add(new Order { Sku = "ABC", Qty = 10});
}
}
public class Order {
public String Sku { get; set; }
public int Qty { get; set; }
}
Then you can bind orders to your repeater. For example:
if (Session["orderComplete"] != null && Session["orderComplete"] != ""){
List<Order> orders = Session["orderComplete"] as List<Order>;
myRepeater.DataSource = orders;
myRepeater.DataBind();
}