I want to store multiple values in single key and i'm using this logic
Hashtable ht = new Hashtable();
int[] intArr = new Integer[2];
intArr [0] = 10;
intArr [1] = 11;
ht.Add("1","intArr");
but i don't know how to retrieve or fetch the value from it
i want something like this
ht[1].intArr[1],ht[1].intArr[2]
Please help me out guys.
Regards
Nitin Yad
Subhendu DePosted Dec 30, 2010, 5:00 AM
public class Product
{
public string ProductName { get; set; }
public string ProductDescription { get; set; }
}
Next, I create a custom HashTable class and re-define the indexer to return Product instance.
public class ProductHashTable:Hashtable
{
public Product this[string key]
{
get
{
if (base.ContainsKey(key))
return base[key] as Product;
else
return null;
}
}
}
Next, I load the hashtable and show it on the page.
aspx.cs code
========
public partial class WebForm2 : System.Web.UI.Page
{
public ProductHashTable productCollection;
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
productCollection = new ProductHashTable();
productCollection.Add("1", new Product { ProductName = "Monitor", ProductDescription = "This is Monitor" });
productCollection.Add("2", new Product { ProductName = "Keyboard", ProductDescription = "This is Keyboard" });
productCollection.Add("3", new Product { ProductName = "Mouse", ProductDescription = "This is Mouse" });
productCollection.Add("4", new Product { ProductName = "CPU", ProductDescription = "This is CPU" });
productCollection.Add("5", new Product { ProductName = "WebCam", ProductDescription = "This is WebCam" });
}
}
aspx code
=======
Here you can pass the hashtable key from querystring to get the specific Product instance.
nitin yadavPosted Dec 31, 2010, 4:44 AM
Subhendu DePosted Dec 31, 2010, 3:37 AM
BTW, Wish you a Happy New Year 2011.........
nitin yadavPosted Dec 31, 2010, 3:19 AM
Actually i was calling Product Detail page through JavaScript and this is not good for SEO point of view.
For SEO we should show url in the View Page Source.Now i passed the URL through anchor tag and it is working fine for me.
Thanks for your code that you gave me.Because of that Code my functionality is working smoothly.Thanks a lot mate :)
Subhendu DePosted Dec 31, 2010, 12:58 AM
nitin yadavPosted Dec 30, 2010, 11:03 PM
Please help me out.
nitin yadavPosted Dec 30, 2010, 7:11 AM
nitin yadavPosted Dec 30, 2010, 4:20 AM
Actually i want something like this
I have no DB so i want to hard-coded Products with HashTable like
// get your product collection instantiated
HashTable productCollection = new HashTable();
// then define your data items one-by-one
productCollection.Add("unique_ProductIdentifer_1", new object {
productName = "some product name 1",
productDescription = "some description 1",
otherData = "some other data 1 and so on..."
});
productCollection.Add("unique_ProductIdentifer_2", new object {
productName = "some product name 2",
productDescription = "some description 2",
otherData = "some other data 2 and so on..."
});
// etc etc etc
and then i have on eprodust detail.aspx page.The Detail of the product on this product detail shows by QueryString that i passed when i click on producy and i would like to show this something like
Have it take query string parameter of the product ID then have simple detail page that has inline
<%=productCollection[productID].productName %>
Please help me.
Subhendu DePosted Dec 30, 2010, 3:47 AM
public class clsHash
{
public Hashtable HT{get;set;}
public clsHash()
{
HT = new Hashtable();
int[] intArr = new int[2] { 10, 11 };
HT.Add("1", intArr);
}
public int[] getArr
{
get
{
return (int[])HT["1"];
}
}
}
This is aspx.cs code or server side code
public partial class WebForm2 : System.Web.UI.Page
{
public clsHash _obj = new clsHash();
protected void Page_Load(object sender, EventArgs e)
{
}
}
This is scripting i.e, aspx
This is how you can do.... There are other methods tooo..... If this method doesn't work for you, let me know with full requirement.
nitin yadavPosted Dec 30, 2010, 3:05 AM
Can i use HashTable with Classes also...
I want to put getArr value in to the Class Property and access this Class Property in Client side like
<%=Propertyname %>
Can i use Class property in Client side?Please help me out.
Subhendu DePosted Dec 30, 2010, 1:08 AM
Hashtable ht = new Hashtable();
int[] intArr = new int[2]{10,11};
ht.Add("1", intArr);
int[] getArr = (int[])ht["1"];
foreach (int _val in getArr)
{
Console.WriteLine(_val);
}