PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

Dynamic Creation of Hashtable inside Custom TemplateField Class (Complex)

 
 
pbd22
Guest
Posts: n/a
 
      29th Oct 2007
Hi.

I have a somewhat complex problem (at least for me).

I need on help on Databinding a hashtable in a custom TemplateField
class.

I would really appreciate it if you stick with me here, as I think I
am close but
need some fresh suggestions.

Ok. I have a GridView.
The GridView has three visible rows.

In the same enclosing table of each given visible column,
I want to add related hidden fields.

The data for the hidden fields are stored using a
Hashtable.


for (int i = 0; products != null && i < products.Length; i++)
{

if (products[i] != null)
{

//Build the visible column rows
DataRow drow = dt.NewRow();

// THESE ARE THE COLUMNS

drow[NAME] = GetSKU(products[i].data,
NAME);
drow[REGION] =
GetTitle(GetSKU(products[i].data, REGION));
drow[TYPE] = "Local";

// HIDDEN INPUTS AS SIBLINGS OF THE
ABOVE DATA

htable = new Hashtable();

htable.Add(DESCRIPTION,
GetSKU(products[i].data, DESCRIPTION));
htable.Add(Size,
GetSKU(products[i].data, SIZE));
htable.Add(Price,
GetSKU(products[i].data, PRICE));

dt.Rows.Add(drow);

}

}

#endregion


OK. I hope you are still with me.
Now, In the GridView, I have created a templatefield with two
controls:

1) <asp:LinkButton>
2) <asp:HtmlInputHidden>

The below loop accesses the three columns and builds its associated
controls. I have
a custom class called GridViewHyperlinkTemplate which creates the
linkbutton and
htmlinputhidden tags inside a TemplateField:

//Iterate through the columns of the datatable
to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{

switch (col.ColumnName)
{
case "Name":

TemplateField tf = new
TemplateField();

tf.ItemTemplate = new
GridViewHyperlinkTemplate(DataControlRowType.DataRow,
col.ColumnName,
col.ColumnName,

htable[DESCRIPTION].ToString(),
htable[SIZE].ToString(),
htable[PRICE].ToString());

// add to the GridView
vGridView.Columns.Add(tf);

break;

default:

BoundField bfield = new
BoundField();
bfield.DataField =
col.ColumnName;


vGridView.Columns.Add(bfield);

break;

}
}


OK. Finally, below is the GridViewHyperlinkTemplate class. THis is
where I am really lost.
How do I a get my hashtable to output the name/value pairs necessary
to create my hidden input
fields alongside the linkbuttons? I feel like I got close but am
messed up with passing the hashtable data.

public class GridViewHyperlinkTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
public string clientID;
private string text;
private string hiddenDESC;
private string hiddenDUR;
private string hiddenGID;

public GridViewHyperlinkTemplate(DataControlRowType type,
string colname, string Text, string desc, string dur, string
gid)
{
templateType = type;
columnName = colname;
hiddenDESC = desc;
hiddenDUR = dur;
hiddenGID = gid;
text = Text;
}

public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{

case DataControlRowType.DataRow:

LinkButton lb = new LinkButton();
lb.CssClass = "ReportNoWrap";
lb.DataBinding += new
EventHandler(this.lb_DataBinding);

lb.ID = (new Random().NextDouble() * 100).ToString();
lb.OnClientClick = "javascript:PlaySelection(this.id);
return false;";
lb.Attributes.Add("onmouseover",
"javascript:ShowVidDetails(); return false;");
lb.Attributes.Add("onmouseout",
"javascript:HideVidDetails(); return false;");

container.Controls.Add(lb);

HtmlInputHidden hih = new HtmlInputHidden();
hih.DataBinding += new
EventHandler(this.hih_DataBinding);

hih.ID = (new Random().NextDouble() * 100).ToString();

container.Controls.Add(hih);
break;

default:
break;
}
}

private void lb_DataBinding(Object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow row = (GridViewRow)lb.NamingContainer;
//hl.NavigateUrl = DataBinder.Eval(row.DataItem,
url).ToString();
lb.Text = DataBinder.Eval(row.DataItem, text).ToString();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////// I GET PRETTY LOST HERE. HOW SHOULD I BE ATTATCHING
DATA??/////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void hih_DataBinding(Object sender, EventArgs e)
{

HtmlInputHidden hih = (HtmlInputHidden)sender;
GridViewRow gvr = (GridViewRow)hih.NamingContainer;

hih.Name = DataBinder.Eval(gvr.DataItem,
"Description").ToString();
hih.Value = DataBinder.Eval(gvr.DataItem,
hiddenDESC).ToString();

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The final result, which should sit in the same DOM level as the
LinkButton elements, should (roughly) look like
like this. I say roughly because I haven't figured out the naming
conventions yet:

<INPUT id=27.8669160920507 type=hidden value=\"Yummy Potatos\"
name=27.8669160920507><INPUT id=27.8669160920507 type=hidden value=0
name=27.8669160920507><INPUT id=27.8669160920507 type=hidden value=200
name=27.8669160920507>

I REALLY APPRECIATE YOUR HELP!

thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boundcolumn or TemplateField when to use Boundcolumn and when TemplateField ???? mesut Microsoft ASP .NET 0 29th Jun 2007 11:17 AM
Raising an event of a control inside Templatefield within GridView =?Utf-8?B?TmFt?= Microsoft VB .NET 1 8th Jul 2006 06:32 PM
REPOST: TemplateField in 2.0 // Disappears on Postback when TemplateField is added thru code sloan Microsoft ASP .NET 0 26th Jun 2006 06:28 PM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry Microsoft ASP .NET 0 2nd Mar 2004 12:48 AM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry Microsoft Dot NET 0 2nd Mar 2004 12:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:02 PM.